首页 > 解决方案 > 在 Boto3 中创建新连接时指定 ConnectionProperties

问题描述

我正在尝试使用 boto3 python 脚本创建连接。它是与 ec2 实例上的 mysql 数据库的连接。我正在使用下面的脚本。我收到以下错误消息。我不确定我错过了什么。我已经使用类似的脚本毫无问题地创建了胶水爬虫。除了我在 ConnectionProperties 中添加了用户名和密码外,我几乎都遵循 boto3 文档。我不确定这是否正确。如果我做对了,或者我需要在我的代码中修复什么,有人可以告诉我吗?

代码:

# create new connection

response = client.create_connection(
    ConnectionInput={
        'Name': 'tst_scrpt',
        'ConnectionType': 'JDBC',


        'ConnectionProperties': {
            'string': 'jdbc:mysql://dataxxx:3306/disxxx',
            'username':'root',
            'password':'ip1k5PNCxxxxx'
        }
        ,
        'PhysicalConnectionRequirements': {
            'SubnetId': 'subnet-0436167b7cbxxxx',
            'SecurityGroupIdList': [
                'sg-02c3f45ce51exxxxx'
            ]

        }
    }
)

错误:

---------------------------------------------------------------------------
InvalidInputException                     Traceback (most recent call last)
<ipython-input-18-6ac0bdcfa816> in <module>
     16             'SubnetId': 'subnet-0436167b7cbxxxx',
     17             'SecurityGroupIdList': [
---> 18                 'sg-02c3f45ce51exxxxx'
     19             ]
     20 #             ,

/anaconda3/envs/py36/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
    355                     "%s() only accepts keyword arguments." % py_operation_name)
    356             # The "self" in this scope is referring to the BaseClient.
--> 357             return self._make_api_call(operation_name, kwargs)
    358 
    359         _api_call.__name__ = str(py_operation_name)

/anaconda3/envs/py36/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    659             error_code = parsed_response.get("Error", {}).get("Code")
    660             error_class = self.exceptions.from_code(error_code)
--> 661             raise error_class(parsed_response, operation_name)
    662         else:
    663             return parsed_response

InvalidInputException: An error occurred (InvalidInputException) when calling the CreateConnection operation: Validation for connection properties failed

标签: python-3.xamazon-web-servicesamazon-ec2boto3

解决方案


我的 ConnectionProperties 错误。将“字符串”替换为“JDBC_CONNECTION_URL”。更正如下。

'ConnectionProperties': {
            'JDBC_CONNECTION_URL': 'jdbc:mysql://dataxxx:3306/disxxx',
            'username':'root',
            'password':'ip1k5PNCxxxxx'
        }

推荐阅读