python 連接 mysql 錯誤


錯誤一:

File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connect
return 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 Connect
return 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插入數據庫。

 

參考:

http://stackoverflow.com/questions/8960965/python-and-mysqldb-connection-to-another-server-on-the-network

 

原文:http://www.cnblogs.com/congbo/archive/2012/08/27/2658039.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM