MySQLdb._exceptions.OperationalError: (2026, 'SSL connection error: SSL_CTX_set_tmp_dh failed')

12 篇文章 0 订阅
5 篇文章 0 订阅

2019.3.1

是anaconda的问题,是openssl包的问题,openssl1.1.1a及以上版本会出现2026bug,降级到1.0.2r及以下可解决。

参考https://github.com/ContinuumIO/anaconda-issues/issues/10646这里。

--------------------------------------------------终极分割线--------------------------------------------------

2019.2.28

接上回,python的DBUtils连接mysql数据库时报错:2006, 'SSL connection error: SSL_CTX_set_tmp_dh failed'这个bug刚解决,又出这么个bug,搜索了好一会,有人讲可能是因为用anaconda的原因。

经过两天搜索+试验,确实是anaconda的问题,不过具体原因仍然未知。

我的环境是:macOS Mojave 10.14.3 + anaconda4.6.7(通过conda -V查看的)对应的Python版本是3.6.7

使用的第三方包是:DBUtils + mysqlclient(MySQLdb)

记录下踩过的坑:

要知道是不是anaconda的问题,先要安装其他版本的Python,Python官网下载安装多版本嫌环境变量设置麻烦,用pyenv。

but 坑1:Mac安装pyenv:https://blog.csdn.net/Daletxt/article/details/88036637

坑2:Mac MySQLdb 的安装:

Python3.X为了解决import MySQLdb,此包安装过程,心中有 三波 无数只草泥马奔腾而过。

第一波,windows上,要去这里边下载那个 mysqlclient‑1.4.2‑cp37‑cp37m‑win_amd64.whl (这里的37是对应的pytohn3.7,其他版本就找对应的.whl)包然后手动安装;

第二波,Linux上,通过

pip install mysqlclient

即可;

这第三波,Mac上,Reason: image not found,此次的bug参考这里

踩过了坑1和坑2,终于运行上了以下代码。

# -*- coding: UTF-8 -*-
import MySQLdb
from MySQLdb.cursors import DictCursor
from DBUtils.PooledDB import PooledDB

# 远程数据库
db_168_3306_config = {
            'host': 'x.x.x.x',
            'port': 3306,
            'user': 'x',
            'password': 'x',
            'charset': 'utf8mb4'
}


class SqlHelper(object):
    __pool = None
    def __init__(self, db_name='', port=3306):
        self._conn = SqlHelper.__getConn(db=db_name, **db_config)

    @staticmethod
    def __getConn(**db_config):
        if SqlHelper.__pool is None:
            __pool = PooledDB(creator=MySQLdb, mincached=0, maxcached=10, blocking=True, maxshared=10, maxusage=10000, **db_config)
        return __pool

    def executeSql(self, command, param=None):
        try:
            conn = self._conn.connection()
            cursor = conn.cursor(cursorclass=DictCursor)
            cursor.execute(command, param)
            records = cursor.fetchall()
            return records
        except:
            raise

    def get(self, table_name, fields=None, cond_dict=None):
        if isinstance(fields, (list, tuple)):
            fields_sql = ",".join(fields)
        elif isinstance(fields, str):
            fields_sql = fields

        if isinstance(cond_dict, dict):
            cond = [key+"=%s" for key in cond_dict.keys()]
            cond_sql = " and ".join(cond)
            sql = 'select {} from {} where {}'.format(fields_sql, table_name, cond_sql)
            return self.executeSql(sql, tuple(cond_dict.values()))
        elif cond_dict is None:
            sql = 'select {} from {}'.format(fields_sql, table_name)
            return self.executeSql(sql)


if __name__ == "__main__":
    db = SqlHelper(db_name='xxx')
    result = db.get(table_name="xxx", fields="xxx")
    mid = [i.get("xxx") for i in result]
    print(len(mid), mid)

此代码在windows上可以连上远程数据库。

Mac上的这玩意就连不上远程数据库,报错如题,但是可以连上本地数据库,但是如果'localhost'换成'127.0.0.1'就报错2006

db_local_config = {
            'host': 'localhost',
            'port': 3306,
            'user': 'x',
            'password': 'x',
            'charset': 'utf8mb4'
}

为什么是anaconda的问题,待继续研究解决。

 

参考资料:

https://www.jianshu.com/p/cea9259d87df

https://blog.csdn.net/Ashimar_a/article/details/80431171

https://github.com/ContinuumIO/anaconda-issues/issues/10646

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值