1. 程式人生 > >django與資料庫的同步終於成功了!!mark一下

django與資料庫的同步終於成功了!!mark一下

在安裝django的時候,出現錯誤:

connect: _mysql_exceptions.OperationalError??: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)");

客戶端在連線服務端的時候,需要找到mysql.sock

請看這篇文章

解決應用程式呼叫mysql的找不到mysql.sock的問題

新的MT系統,我使用了mysql系統,一直用的很好,不過今天我偶然去使用phpmyadmin時,居然報Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'的錯誤。奇怪,明明mysql正常啟動,但為什麼phpmyadmin不能正常連線mysql,然後又直接去linux機器,本地執行mysql命令,發現mysql確實正常執行起來,

看來是phpmyadmin的問題,馬上去找phpmyadmin的配置檔案,發現確實有設定mysql.sock檔案的位置,由於安裝MT的緣故,我把mysql.sock選擇在/tmp/mysql.sock。修改之下,phpmyadmin正常執行,但隨之我又發現我的另外一個blog系統wp也不能正常呼叫mysql,它也是一個php的blog,但這個程式確沒地方設定mysql.sock的位置,左思右想發現php.ini有修改mysql.sock的設定,只要在這裡設定了,所有的php應用就再也不報以上錯誤。通過以上除錯總結出以下結論:

當出些類似Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'的錯誤時,解決方法:

1.mysql可能沒正常啟動,檢查mysql程式
2.修改/etc/my.cnf 修改mysql.sock的位置,使之滿足php或者cgi程式的需要
3.修改php.ini的mysql.sock的設定,滿足所有php程式的需要
4.修改某些程式中配置檔案中關於mysql.sock的設定。

 我得益於這篇文章,

Problem

I want to use turbo gears, but my machine is setup with mysql not bound to a network socket and I get the following errors when I try to connect: _mysql_exceptions.

OperationalError??: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)");

Solution

There is a practice of not binding MySQL to an ip address and port following the principle that "if you're not connected, you are protected." In this situation, the server has a socket file. By default TurboGears (or SQLObject and the python MySQL driver/client to be more exact) will look for the socket file in /tmp/mysql.sock. Many installations seem to use /var/lib/mysql/mysql.sock. Therefore, to get TurboGears to work, you'll need to specify the unix_socket (if on unix) param in the dburi. You can find out where your mysql socket is by looking at the file my.cnf that usually resides in /etc/my.cnf or /etc/mysql/my.cnf.

Format: sqlobject.dburi="mysql://username:password@/databasename?unix_socket=path-to-mysql.sock"

Example: I have my socket file in /var/lib/mysql/mysql.sock and I want my user foo with password bar to connect to my db test1. sqlobject.dburi="mysql:/foo:bar@/test1?unix_socket=/var/lib/mysql/mysql.sock"

<script type="text/javascript"> addHeadingLinks(document.getElementById("searchable")); </script>