親測有效,解決Can 't connect to local MySQL server through socket '/tmp/mysql.sock '(2) ";

  最近租了一個阿里云云翼服務器,趁着自己還是一個學生,享受一下優惠。我租的是阿里雲Ubuntu16.04版本的服務器,在搭建mysql的時候,一開始是可以運行的,由於某次手抽,把mysql的套接字文件不小心刪除了,然後無論怎麼卸載重裝,都出現了這個問題:

root@iZufkfljcZ:~# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
  網上找了好久,搜到的全部是重複的文章而且也沒講明爲什麼出現這個問題,找了一天才找到一篇講解mysql.sock文件作用的文章,然後分析一下爲什麼出現這個問題,現在講之總結一下,以備後人查閱。

  我出現的問題是:找不到mysql.sock,如果你可以運行

find / -name mysql.sock
這條命令,並且能查到結果的話,只需將查到的結果做一個軟連接到/tmp目錄下即可解決(網上都是這麼解決的)。

但是,我執行了這條語句之後,並沒有任何反應,沒有找到mysql.sock文件。

在這之前,需要明白mysql.sock這個文件有什麼用?

連接localhost通常通過一個Unix域套接字文件進行,一般是/tmp/mysql.sock。如果套接字文件被刪除了,本地客戶就不能連接。這可能發生在你的系統運行一個cron任務刪除了/tmp下的臨時文件。

如果你因爲丟失套接字文件而不能連接,你可以簡單地通過重啓服務器重新創建得到它。因爲服務器在啓動時重新創建它。

如果和我一樣,重啓服務器還是沒有任何變化,你可以先執行下面的語句:

# mysql -uroot -h 127.0.0.1 -p 
不出意外,這句話應該是可以執行的,你現在不能用套接字建立連接因爲它不見了,所以可以建立一個TCP/IP連接
如果套接字文件被一個cron任務刪除,問題將重複出現,除非你修改cron任務或使用一個或使用一個不同的套接字文件,我的解決辦法是重新指定一個不同的套接字,或者說,我現在沒有mysql.sock文件,所以我要想辦法生成一個。

首先,更改my.cnf文件,我的服務器中的目錄爲/etc/my.cnf,如果沒有的話可以用find去查找,



接下來就是保存退出,然後確保這個目錄存在,並且將這個目錄的權限修改一下

# chmod 777 /var/lib/mysql
準備步驟做好,然後就是mysql和mysqld服務重啓

# service mysql  restart
# service mysqld restart
我在重啓mysqld服務的時候,重啓失敗了,顯示如下:
root@iZufkfljcZ:~# service mysqld start
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalc
tl -xe" for details.

這個時候,提示可以輸入systemctl status mysqld.service去查看具體的失敗原因,於是:

****************************************************************************************

root@iZufkfljcZ:~# systemctl status mysqld.service
mysqld.service - LSB: start and stop MySQL
  Loaded: loaded (/etc/init.d/mysqld; bad; vendor preset: enabled)
  Active: failed(Result: exit-code) since 三 2017-12-20 10:38:30 CST; 45s ago
Docs: man:systemd-sysv-generator(8)
 Process: 2154 ExecStart=/etc/init.d/mysqld start (code=exited, status=1/FAILURE)


12月 20 10:38:29 iZufkfljcZ systemd[1]: Starting LSB: start and stop MySQL...
12月 20 10:38:29 iZufkfljcZ mysqld[2154]: Starting MySQL
12月 20 10:38:29 iZufkfljcZ mysqld_safe[2689]:Logging to '/var/log/mysql/error.log'.
12月 20 10:38:29 iZufkfljcZ mysqld_safe[2693]:Directory '/var/run/mysqld' for UNIX socket file don't exists.
12月 20 10:38:30 iZufkfljcZ mysqld[2154]: . * The server quit without updating PID file (/var/run/mysqld/mysqld.pid).
12月 20 10:38:30 iZufkfljcZ systemd[1]: mysqld.service: Control process exited, code=exited status=1
12月 20 10:38:30 iZufkfljcZ systemd[1]: Failed to start LSB: start and stop MySQL.
12月 20 10:38:30 iZufkfljcZ systemd[1]: mysqld.service: Unit entered failed state.
12月 20 10:38:30 iZufkfljcZ systemd[1]: mysqld.service: Failed with result 'exit-code'.

****************************************************************************************

根據提示可知,/var/run/mysqld目錄不存在,也就是說mysqld服務重啓需要這個目錄,那就建一個吧:

root@iZufkfljcZ:~# mkdir /var/run/mysqld
root@iZufkfljcZ:~# chmod 777 /var/run/mysqld/
root@iZufkfljcZ:~# service mysqld start
root@iZufkfljcZ:~# 
建完目錄後,重新運行mysqld服務,發現重啓成功了,那麼我們再來看看爲什麼剛纔要建這個目錄呢?打開這個目錄看看:

root@iZufkfljcZ# ls /var/run/mysqld
mysqld.pid  mysqld.sock  mysqld.sock.lock
發現了一個熟悉的東西,mysqld.sock,但是這個是不是我們需要的東西呢?不管他,先用這個sock文件登下mysql看看行不行:

root@iZufkfljcZ:/var/run/mysqld# mysql -uroot -p -S /var/run/mysqld/mysqld.sock 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

這麼一運行,發現好像可以了,那接下來好辦了,我們把之前改的配置改回來就行了,之前的目錄應該是/tmp/mysql.sock,我們可以建立一個軟連接連上去就可以了,

root@iZufkfljcZ:~# ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
root@iZufkfljcZ:~# ls /tmp/
mysql.sock
這樣,tmp目錄下就有了my.cnf配置文件中需要的mysql.sock文件了,然後把my.cnf改回就行了,


到此爲止,我們的mysql應該已經完全修復了,那麼我們再測試一下吧:

root@iZufkfljcZ:/var/run/mysqld# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
  OK,搞定收工,用了快一天時間在解決這個問題上,感覺收穫還是挺多的,Linux的學習任重而道遠啊!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章