python 串連mysql錯誤問題總結

來源:互聯網
上載者:User

錯誤一:

 代碼如下 複製代碼

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.12、重啟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插入資料庫。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.