如何连接到Moto MySQL实例?

发布于 2025-02-03 13:06:46 字数 1904 浏览 5 评论 0原文

我使用模拟的BOTO3 RDS客户端创建了RDS DB实例。以下是我在conftest.py中设置它的方式

@pytest.fixture
def aws_credentials():
    """Mocked AWS Credentials for moto."""
    os.environ["AWS_ACCESS_KEY_ID"] = "testing"
    os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
    os.environ["AWS_SECURITY_TOKEN"] = "testing"
    os.environ["AWS_SESSION_TOKEN"] = "testing"

@pytest.fixture
def rds_client(aws_credentials, aws_region):
    with mock_rds():
        client = boto3.client("rds", region_name=aws_region)
        yield client

https://docs.aws.amazon.com/amazonrds/latest/latest/userguide/usingwithrds.iamdbauth.connecting.python.html )我设置了我的mysql连接器

db_instance = rds_client.create_db_instance(DBInstanceIdentifier="TestDBInstanceIdentifier",
                                            DBInstanceClass="db.m4.large", Engine="mysql",
                                            MasterUsername="root", DBName="TestDBName")
print("RDS Instance-----------------------------------------------------")
print(db_instance)

host = db_instance['DBInstance']['Endpoint']['Address']
port = db_instance['DBInstance']['Endpoint']['Port']
user = db_instance['DBInstance']['MasterUsername']
dbname = db_instance['DBInstance']['DBName']
print("Starting connection")
token = rds_client.generate_db_auth_token(DBHostname=host, Port=port, DBUsername=user, Region=aws_region)

mydb = mysql.connector.connect(host=host, database=dbname, user=user, passwd=token, port=port)

:连接器找不到DB:

FAILED test_read_rds_db - mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL server host 'TestDBInstanceIdentifier.aaaaaaaaaa.us-east-1.rds.amazonaws.com' (8)

有人以前能够设置它吗?

I created an RDS DB Instance using a mocked boto3 rds client. Here's how I set it up in my conftest.py

@pytest.fixture
def aws_credentials():
    """Mocked AWS Credentials for moto."""
    os.environ["AWS_ACCESS_KEY_ID"] = "testing"
    os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
    os.environ["AWS_SECURITY_TOKEN"] = "testing"
    os.environ["AWS_SESSION_TOKEN"] = "testing"

@pytest.fixture
def rds_client(aws_credentials, aws_region):
    with mock_rds():
        client = boto3.client("rds", region_name=aws_region)
        yield client

Following the example here (https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.Python.html) I set up my mysql connector like this:

db_instance = rds_client.create_db_instance(DBInstanceIdentifier="TestDBInstanceIdentifier",
                                            DBInstanceClass="db.m4.large", Engine="mysql",
                                            MasterUsername="root", DBName="TestDBName")
print("RDS Instance-----------------------------------------------------")
print(db_instance)

host = db_instance['DBInstance']['Endpoint']['Address']
port = db_instance['DBInstance']['Endpoint']['Port']
user = db_instance['DBInstance']['MasterUsername']
dbname = db_instance['DBInstance']['DBName']
print("Starting connection")
token = rds_client.generate_db_auth_token(DBHostname=host, Port=port, DBUsername=user, Region=aws_region)

mydb = mysql.connector.connect(host=host, database=dbname, user=user, passwd=token, port=port)

However, the connector can't find the DB:

FAILED test_read_rds_db - mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL server host 'TestDBInstanceIdentifier.aaaaaaaaaa.us-east-1.rds.amazonaws.com' (8)

Has someone been able to set this up before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2025-02-10 13:06:46

Moto不提供此功能。它嘲笑AWS API,但没有暴露任何RDBMS功能。

您可以改用LocalStack。它在后台使用Moto来模拟对AWS的呼叫,但除此之外提供了功能,例如连接到RDS实例的功能。
请参阅此处的文档: https://docs.localstack.cloud/aws/aws/rds/rds/rds/

Moto does not offer this functionality. It mocks the AWS API, but does not expose any RDBMS-functionality.

You could look into Localstack instead. It uses Moto in the background to mock calls to AWS, but offers features on top of that such as the ability to connect to an RDS instance.
See the docs here: https://docs.localstack.cloud/aws/rds/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文