本文共 1775 字,大约阅读时间需要 5 分钟。
错误信息:
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)")
解决方法:
my.cnf 文件,通常位于 /etc/my.cnf 或 ~/.my.cnf。 bind-address 参数,确保允许外部访问。 # bind-address = 127.0.0.1# 修改为:bind-address = 0.0.0.0
sudo /etc/init.d/mysql restart
说明:
默认情况下,MySQL 只绑定本地地址。修改bind-address 为 0.0.0.0 后,MySQL 会监听所有 IP 地址,确保外部可以连接。 错误信息:
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 只允许本地 IP 访问。通过 GRANT 语句,允许来自所有 IP 的访问,确保外部程序可以连接。错误信息:
(1040, 'Too many connections')The reason is: 'NoneType' object has no attribute 'cursor'
上下文:
网络爬虫改为多线程后,数据库连接超过服务器限制。解决方法:
my.cnf,修改 max_connections。 [mysql]max_connections = 10000
-,默认值为 100,建议改为 16384 以支持更多连接。
DBPool 或类似工具管理连接。说明:
MySQL 的连接池机制可以显著提高性能,避免因连接过多导致的错误。通过优化程序结构,减少数据库压力,确保长时间运行稳定。以上错误均与 MySQL 连接配置或程序逻辑相关。通过合理优化配置文件和调整程序结构,可以有效解决连接问题,确保数据库运行稳定。
转载地址:http://jmbr.baihongyu.com/