ERROR 2002(HY000):Can't connect to local MySQL socket '/tmp/mysql.sock'

urgel_babay發表於2016-02-29

公司要使用MySQL,我就開始玩MySQL,可是跟想象中的不一樣,問題頗多,只能慢慢享受了。之前就透過編譯的方式安裝好MySQL5.7。
今天登陸的時候遇見一的問題,記錄一下

[root@testmysql bin]# ./mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)


解決方法:

由於mysql 預設的mysql.sock 是在/usr/local/mysql/data/mysql.sock,但linux系統總是去/tmp/mysql.sock查詢,所以會報錯

1.直接指定mysql通道

據我知道的(ps:我是菜鳥,請糾正),老版本(5.4之前)的是:/var/lib/mysql/mysql.sock

 在新版本中預設是在/usr/local/mysql/data/mysql.sock,具體的可以去檢視my.cnf檔案裡面

[root@testmysql bin]# ./mysql --socket=/usr/local/mysql/data/mysql.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.4-m7 Source distribution
Copyright (c) 2000, 2011, 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>


 

2. 建立符號連線:

 

為mysql.sock增加軟連線(相當於windows中的快捷方式)。

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


eg:
 ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@testmysql bin]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

插曲:有可能你在建立連線的時候出現下面的情況,
[root@testmysql bin]# ln -vs /usr/local/mysql/data/mysql.sock /tmp/mysql.sock
ln: creating symbolic link `/tmp/mysql.sock': File exists
沒關係,加上-f引數就可以了。-f的主要過程:先將同名的軟連結dd刪除,然後再建立硬連結dd

[root@testmysql bin]# ln -f /usr/local/mysql/data/mysql.sock /tmp/mysql.sock
[root@testmysql bin]# ./mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.4-m7 Source distribution
Copyright (c) 2000, 2011, 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>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30936525/viewspace-2016620/,如需轉載,請註明出處,否則將追究法律責任。

相關文章