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 @   slnngk  阅读(480)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2022-10-31 使用yum安装部署postgresql13+postgis3.2
2019-10-31 安装golang
点击右上角即可分享
微信分享提示