python3.4.3、pymysql 0.6.7和0.7.1、mysql 5.5.23和5.5.4x
无法使用ssl选项连接到mysql。
使用mysql workbench和mysql客户端,安全连接可以正常工作。
我在debian和windows上的两个mysql服务器上测试了它
这里是代码和解释
制作证书openssl genrsa 2048 > ca-key.pem; \
openssl req -sha1 -new -x509 -nodes -days 3650 -key ca-key.pem > ca-cert.pem; \
openssl req -sha1 -newkey rsa:2048 -days 730 -nodes -keyout server-key.pem > server-req.pem; \
openssl x509 -sha1 -req -in server-req.pem -days 730 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem; \
openssl rsa -in server-key.pem -out server-key.pem; \
openssl req -sha1 -newkey rsa:2048 -days 730 -nodes -keyout client-key.pem > client-req.pem; \
openssl x509 -sha1 -req -in client-req.pem -days 730 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem; \
openssl rsa -in client-key.pem -out client-key.pem;
用户创建
^{pr2}$
代码from __future__ import print_function
import pymysql
#conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='mysql')
conn = pymysql.connect(host='localhost', port=3306, user='u1', passwd='12345', db='dbname', ssl = {'key': 'ssl/client-key.pem', 'cert': 'ssl/client-cert.pem', 'ca': 'ssl/ca-cert.pem'})
cur = conn.cursor()
#cur.execute("SELECT Host,User FROM user")
cur.execute("SHOW TABLES")
print(cur.description)
print()
for row in cur:
print(row)
cur.close()
conn.close()
错误Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 851, in connect
self._request_authentication()
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 1017, in _request_authentication
ca_certs=self.ca)
File "C:\Python34\lib\ssl.py", line 890, in wrap_socket
ciphers=ciphers)
File "C:\Python34\lib\ssl.py", line 580, in __init__
self.do_handshake()
File "C:\Python34\lib\ssl.py", line 807, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/User/Py/prjct/test.py", line 24, in
conn = pymysql.connect(host='localhost', port=3306, user='u1', passwd='12345', db='dbname', ssl = {'key': 'ssl/client-key.pem', 'cert': 'ssl/client-cert.pem', 'ca': 'ssl/ca-cert.pem'})
File "C:\Python34\lib\site-packages\pymysql\__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 657, in __init__
self.connect()
File "C:\Python34\lib\site-packages\pymysql\connections.py", line 882, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600))")
更新:
我错过了这部分说明:```
Whatever method you use to generate the certificate and key files, the Common Name value used for the server and client certificates/keys must each differ from the Common Name value used for the CA certificate. Otherwise, the certificate and key files will not work for servers compiled using OpenSSL.
```
当OpenSSL提示您输入每个证书的公共名称时,请使用不同的名称。在
但这有助于提出新的错误:
第一个-dhkey不足够长,我将testmysql服务器更新到5.7.11
它有助于并引发新的错误:公共名称不匹配localhost
我用新的通用名localhost重新生成证书
它再次显示错误-([SSL:CERTIFICATE\u VERIFY_FAILED]CERTIFICATE VERIFY FAILED(_SSL.c:600))