博客
关于我
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/

你可能感兴趣的文章
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>