博客
关于我
python 连接 mysql 错误
阅读量:357 次
发布时间: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/

你可能感兴趣的文章
Nginx实现动静分离
查看>>
Nginx实现反向代理负载均衡
查看>>
nginx实现负载均衡
查看>>
Nginx实现限流
查看>>
Nginx将https重定向为http进行访问的配置(附Demo)
查看>>
nginx工作笔记004---配置https_ssl证书_视频服务器接口等
查看>>
nginx工作笔记005---nginx配置负载均衡_在微服务中实现网关集群_实现TCP传输层协议__http协议的负载均衡
查看>>
nginx常用命令及简单配置
查看>>
Nginx常用屏蔽规则,让网站更安全
查看>>
nginx开机启动脚本
查看>>
nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
查看>>
nginx总结及使用Docker创建nginx教程
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
Nginx搭建RTMP服务器+FFmpeg实现海康威视摄像头预览
查看>>
nginx日志分割并定期删除
查看>>
Nginx日志分析系统---ElasticStack(ELK)工作笔记001
查看>>
Nginx日志按天分割
查看>>
Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
查看>>
Nginx映射本地静态资源时,浏览器提示跨域问题解决
查看>>