使用python语言,通过pymysql库访问mysql数据库(别问我为什么不用orm框架,有些时候orm框架真的不如原生态的sql好用),在我本机运行是ok的,但在另一同事机器运行失败,报错如下:
Traceback (most recent call last):File "D:/work//scripts/main.py", line 25, in biz_service.initialize("./config/config.yaml")File "D:\work\\scripts\biz\sync_security_service.py", line 57, in initializepassword=self._config.mysql.password,db=self._config.mysql.database)File "D:\work\scripts\env\lib\site-packages\pymysql\__init__.py", line 94, in Connectreturn Connection(*args, **kwargs)File "D:\work\scripts\env\lib\site-packages\pymysql\connections.py", line 325, in __init__self.connect()File "D:\work\scripts\env\lib\site-packages\pymysql\connections.py", line 599, in connectself._request_authentication()File "D:\work\scripts\env\lib\site-packages\pymysql\connections.py", line 817, in _request_authenticationauthresp = _auth.scramble_native_password(self.password, self.salt)File "D:\work\scripts\env\lib\site-packages\pymysql\_auth.py", line 39, in scramble_native_passwordstage1 = sha1_new(password).digest()File "D:\work\scripts\env\lib\hashlib.py", line 149, in __hash_newreturn _hashlib.new(name, data)
TypeError: object supporting the buffer API required
开始以为是python版本和pymysql版本的问题,后来换成一样的版本,还是报错!!!!从上面的日志其实可以很清楚的看到错误的函数:sync_security_service.py的第57行(不得不说,详细的错误日志及堆栈是个好东东),而57行就是:
self._connect=pymysql.connect(host=self.mysql.ip,port=self.mysql.port,user=self.mysql.user,password=self.mysql.password,db=self.mysql.database)
于是决定跟踪pymysql.connect函数到底干了什么~~~
在跟踪的过程中,偶然发现:self.mysql.password是一个整型,竟然是一个整型,密码应该是字符串类型,怎么会是整型呢?原来同事的密码是一串数字123456,python在读配置文件过程中默认把123456转成了整型,修改如下:
self._connect=pymysql.connect(host=self.mysql.ip,port=self.mysql.port,user=self.mysql.user,password=str(self.mysql.password),db=self.mysql.database)
再次运行,发现问题解决。