使用boto3创建与mysql的粘合连接

时间:2019-10-19 00:28:00

标签: python-3.x boto3 aws-glue aws-glue-data-catalog

我正在尝试创建一个新的AWS胶合连接。我正在使用下面的boto3脚本。我可以连接类似的脚本并检索数据目录数据库中表的结构。所以我知道客户在工作。我知道可用区也是us-west-2。我从已经创建的粘合连接中复制了其余信息。我只是尝试测试boto3脚本,以查看是否可以通过该脚本创建连接。有人知道这个问题可能是什么吗?我正在尝试连接到ec2实例上的mysql数据库。

代码:

# create new connection

response = client.create_connection(
    ConnectionInput={
        'Name': 'tst_scrpt',
        'ConnectionType': 'JDBC',
        'MatchCriteria': [
            'string',
        ],
        'ConnectionProperties': {
            'string': 'jdbc:mysql://xxxxx:3306/disxxx',
            'username':'root',
            'password':'ipxxxxx'
        },
        'PhysicalConnectionRequirements': {
            'SubnetId': 'subnet-04xxxxx',
            'SecurityGroupIdList': [
                'sg-xxxxx'
            ],
            'AvailabilityZone': 'us-west-2'
        }
    }
)

错误:

---------------------------------------------------------------------------
InvalidInputException                     Traceback (most recent call last)
<ipython-input-20-c3f33f9c9933> in <module>
     18                 'sg-xxxxx'
     19             ],
---> 20             'AvailabilityZone': 'us-west-2'
     21         }
     22     }

/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

2 个答案:

答案 0 :(得分:1)

Boto3定义的create_connection Glue API没有提及要为“ ConnectionProperties”字段传递哪些参数。它只提到要提供键值对。要传递的值如下。


client = boto3.client('glue', region_name='<region-to-use>')

response = client.create_connection(
    CatalogId='<account-id>',
    ConnectionInput={
        'Name': '<name-of-the-connection>',
        'Description': 'Test connection',
        'ConnectionType':'JDBC',
        'ConnectionProperties': {
            'USERNAME': '<db-username>',
            'JDBC_ENFORCE_SSL': 'false',
            'PASSWORD': '<db-password>',
            'JDBC_CONNECTION_URL': 'jdbc:protocol://host:port/db_name'
        },
        
        'PhysicalConnectionRequirements': {
            'SubnetId': '<subnet-to-use>', 
            'AvailabilityZone': '<availability-zone-to-use>', 
            'SecurityGroupIdList': ['<security-group(s)-to-use>']
    }
    }
)

答案 1 :(得分:0)

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

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