[英]peewee mysql SSL: CERTIFICATE_VERIFY_FAILED
python 3.4.3,pymysql 0.6.7和0.7.1,mysql 5.5.23和5.5.4x無法通過ssl選項連接到mysql。 使用mysql workbench和mysql-client,安全連接可以正常工作。 我已經在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;
用戶創建
CREATE DATABASE dbname;
GRANT ALL PRIVILEGES ON dbname.* TO 'u1'@'%' IDENTIFIED BY '12345' REQUIRE SSL;
FLUSH PRIVILEGES;
碼
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 <module>
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不足,我將測試mysql服務器更新到最后5.7.11,它會幫助並引發新的錯誤,即公用名與localhost
不匹配,並且我已使用新的公用名重新生成了證書localhost
並且它再次顯示我錯誤-([SSL:CERTIFICATE_VERIFY_FAILED]證書驗證失敗(_ssl.c:600))
嘗試:
conn = pymysql.connect(host ='localhost',port = 3306,user ='u1',passwd ='12345',db ='dbname',ssl = {'ssl':{'key':'ssl / client -key.pem”,“ cert”:“ ssl / client-cert.pem”,“ ca”:“ ssl / ca-cert.pem”}})
有同樣的問題,它對我有用。
這可能無法直接回答您的問題,但是我相信使用mysql-python存在一些與版本沖突問題相關的問題。 我使用以下Anaconda / Pip版本設法通過peewee(使用ssl)進行連接:
python 2.7.15 h9bab390_6
pymysql 0.9.3 py27_0
mysql-connector-c 6.1.11 h597af5e_0
mysql-connector-python 8.0.12 py27haf6c83e_0
peewee 3.8.2 pypi_0 pypi
為了完整起見,我通過以下方式使用SSL連接到Google Cloud實例:
import pewee as pw
from os.path import expanduser
home = expanduser("~")
perms = {'key': home+'/ssl/client-key.pem',
'cert': home+'/ssl/client-cert.pem',
'ca': home+'/ssl/server-ca.pem',
'check_hostname': False}
mysql_db = pw.MySQLDatabase(database=os.environ['GCP_DBNAME'],
host=os.environ['GCP_HOST'],
user=os.environ['GCP_USER'],
passwd=os.environ['GCP_PASS'],
ssl=perms)
希望這對其他有相同錯誤的人有所幫助。 更新的版本似乎也可以在Python 3.5中使用。 我在Ubuntu 16.04上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.