博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 连接 mysql 错误
阅读量:355 次
发布时间:2019-03-04

本文共 1740 字,大约阅读时间需要 5 分钟。

本文  已收录,有Java程序员进阶技术知识地图以及我的系列文章,欢迎大家Star。

错误一:

File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connectreturn Connection(*args, **kwargs)File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 170, in __init__super(Connection, self).__init__(*args, **kwargs2)_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (111)")

 

解决方法:

1、编辑my.cnf,默认为/etc/mydql/my.cnf,注释掉bind-address = 127.0.0.1或者修改为0.0.0.0;

# Instead of skip-networking the default is now to listen only on# localhost which is more compatible and is not less secure.# bind-address           = 127.0.0.1

2、重启mysql:

sudo /etc/init.d/mysql restart

 

错误二:

File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connectreturn Connection(*args, **kwargs)File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 170, in __init__super(Connection, self).__init__(*args, **kwargs2)_mysql_exceptions.OperationalError: (1130, "Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server")

 

解决方法:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

该语句意思为:允许所有用root用户并且输入root密码的主机登入该mysql Server。

用户名和密码替换为需要的,如果将'%'换成相应主机名那么只有该主机可以登陆。

 

错误三:

(1040, 'Too many connections')The reason is:  'NoneType' object has no attribute 'cursor'

网络爬虫改为多线程后,连接数据库出现以上错误。

 

解决方法一:

编辑my.cnf,默认为/etc/mydql/my.cnf,修改max_connections的值为10000,默认为100,实际MySQL服务器允许的最大连接数16384。

解决方法二:

使用数据库连接池。

解决方法三:

之前的程序结构为抓取到一条数据即产生一条sql语句即插入一条数据,修改程序结构为将各进程线程产生的sql语句put进Queue,用一个单独的进程不断get出sql插入数据库。

 

-- END --

日常求赞:你好技术人,先赞后看养成习惯,你的赞是我前进道路上的动力,对我非常重要。

加油技术人!

简介: 博主从华中科技大学硕士毕业,是一个对技术有追求,对生活有激情的程序员。几年间浪迹于多个一线互联网大厂,具有多年开发实战经验。

微信搜索公众号【爱笑的架构师】,我有技术和故事,等你来。

文章持续更新,在  中可以看到我归档的系列文章,有面试经验和技术干货,欢迎Star。

转载地址:http://jmbr.baihongyu.com/

你可能感兴趣的文章