python使用ssl连接mysql

环境:

Os:Centos 7

mysql:5.7.29

ssl:自制证书

python:3.6.5

 

import pymysql
import ssl

ssl.match_hostname = lambda cert, hostname: True

config = {
    'host': '192.168.1.108',
    'user': 'ssltest',
    'password': 'mysql',
    'database': 'db_test',
    'port':13306,
    'client_flag': pymysql.constants.CLIENT.MULTI_STATEMENTS,
    'ssl': {
        'ca': 'E:\SecureCRT_download\ca.pem',
        'cert': 'E:\SecureCRT_download\client-cert.pem',
        'key': 'E:\SecureCRT_download\client-key.pem'
     }
}

conn = pymysql.connect(**config)
cursor = conn.cursor()

cursor.execute('SELECT * FROM tb_test')

result = cursor.fetchall()
for row in result:
    print(row)

cursor.close()
conn.close()

 

python 3.8的报如下的错误

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '192.168.1.108' ([SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: IP address mismatch, certificate is not valid for '192.168.1.108'. (_ssl.c:1131))")

 

posted @ 2023-10-31 17:58  slnngk  阅读(430)  评论(0编辑  收藏  举报