pymysql无法与SSL连接

时间:2015-11-26 01:52:29

标签: python mysql ssl

我正在尝试使用python连接到MySQL数据库,但我收到一个奇怪的错误。事实上,我可以使用mysql控制台命令中的相同连接值,并且连接没有任何问题。

以下是我正在使用的确切代码:

导入pymysql     来自检查导入AgentCheck

class DelayedJobCheck(AgentCheck):
    def check(self, instance):
        self.log.info("testing connection")
        self.log.info(instance)

        connection = pymysql.connect(**instance)

        cur = cnx.cursor(buffered=True)
        cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
        print(cur.fetchone())
        cur.close()
        cnx.close()

        self.gauge('hello.world', 1)

这是我得到的错误:

Traceback (most recent call last):
  File "/opt/datadog-agent/agent/checks/__init__.py", line 661, in run
    self.check(copy.deepcopy(instance))
  File "/opt/datadog-agent/agent/checks.d/delayed_job.py", line 10, in check
    connection = pymysql.connect(**instance)
  File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/pymysql/__init__.py", line 88, in Connect
    return Connection(*args, **kwargs)
  File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/pymysql/connections.py", line 644, in __init__
    self._connect()
  File "/opt/datadog-agent/embedded/lib/python2.7/site-packages/pymysql/connections.py", line 869, in _connect
    raise exc
OperationalError: (2003, u"Can't connect to MySQL server on '192.168.199.86' ([SSL: SSL_NEGATIVE_LENGTH] dh key too small (_ssl.c:590))")

我在Ubuntu盒子上运行此代码,但我最初认为可能是因为SSL CA是自生成的证书。所以我按照here的步骤进行了操作但是,它没有任何区别。此外,我已经验证运行此代码的进程具有对证书文件的完全访问权限

还有什么想法可能导致这个问题吗?

1 个答案:

答案 0 :(得分:0)

由于错误的信息说dh键太小,一个更大的键可能有帮助。用dh4096.pem

替换默认的dh512.pem文件

sudo wget "https://git.openssl.org/gitweb/?p=openssl.git;a=blob_plain;f=apps/dh4096.pem" -O dh4096.pem

参考:http://www.alexrhino.net/jekyll/update/2015/07/14/dh-params-test-fail.html