linux下mysql串連失敗: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysq

來源:互聯網
上載者:User

最近在linux上面用django寫一個小網站 ,資料庫用的是mysql,但是突然有一天,出現了奇怪的問題,報錯:

mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

然後我去看這個檔案'/var/lib/mysql/mysql.sock' ,但是沒有找到,我就有點不明白了,然後想起可能是mysql的服務mysqld沒有啟動,因為mysql  -u root -p這個命令還是能用的

,所以趕緊查看了一下service mysqld status 是什麼狀態,才看到原來沒有啟動,然後就沒有問題了。

 

 

 

 

啟動mysql 報錯:

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

1、先查看 /etc/rc.d/init.d/mysqld status 看看mysql是否已經啟動.
另外看看是不是許可權問題.

2、確定你的mysql.sock是不是在那個位置,
mysql -u 你的mysql使用者名稱 -p -S /var/lib/mysql/mysql.sock

3、試試:service mysqld start

4、如果是許可權問題,則先改變許可權 #chown -R mysql:mysql /var/lib/mysql

[root@localhost ~]# /etc/init.d/mysqld start
啟動 MySQL: [ 確定 ]
[root@localhost ~]# mysql -u root -p

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
原因是,/var/lib/mysql 的存取權限問題。

shell> chown -R mysql:mysql /var/lib/mysql

接著啟動伺服器

shell> /etc/init.d/mysql start

伺服器正常啟動後察看 /var/lib/mysql 自動產生mysql.sock檔案。

但是我的問題仍然沒有得到解決。

問題終於解決:
方法: 修改/etc/my.conf:
[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/usr/local/mysql

If there is not currently a section called [client], add one at the bottom of the file and copy the socket= line under the [mysqld] section such as:

[client]
socket=/var/lib/mysql/mysql.sock

發現依舊如此,運行/etc/init.d/mysql start報錯:Starting MySQLCouldn't find MySQL manager or server

是mysqld服務沒啟,運行/usr/local/mysql/bin/mysqld_safe &

問題解決。

執行 PHP 時出現類似這樣的錯誤訊息:

Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111) in /home/httpd/html/show.php on line 9

請將 /tmp/mysql.sock link 到 /var/lib/mysql 目錄裡即可:

linux# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.