注釋:
前段時間出現過一種情況,localhost本地登入mysql資料庫提示不能連接配接mysql.sock,第三方工具sqlyog可以登入,具體原因如下。
mysql有兩種連接配接方式:
1、tcp/ip
2、socket
mysql.sock的作用是server和client在同一台伺服器,并且使用localhost進行連結的時候,就會使用socket來進行連接配接——僅此而已
也就是:為主機名為localhost建立的mysql連接配接,該連接配接過程通過一個套接字檔案mysql.socket實作的。是以該檔案被删後,用localhost使用者是連接配接不到mysql伺服器的。
必須建立一條tcp/ip連接配接,即使用127.0.0.1而不是localhost作為-h的參數去連接配接mysql伺服器,如:mysqladmin -h 127.0.0.1 -u root -p shutdown,強制地建立一條tcp/ip連接配接;
關閉mysql伺服器,再重新以localhost為主機名啟動mysql伺服器,它就會重新建立一個套接字檔案。
<b>對上文加以測試深入了解;</b>
檢視mysql.sock具體路徑:
[root@wonhigh-test16 ~]# ps -ef | grep mysql.sock|grep -v "grep"
mysql 31108 30650 0 sep10 ? 00:03:17 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/wonhigh-test16.err --pid-file=/var/lib/mysql/wonhigh-test16.pid <b>--socket=/var/lib/mysql/mysql.sock</b> --port=3306
[root@wonhigh-test16 ~]#
轉移套接字檔案 mysql.sock
[root@wonhigh-test16 ~]# mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql1.sock
确認本地登入情況
[root@wonhigh-test16 ~]# mysql -uroot -p123456
warning: using a password on the command line interface can be insecure.
<b>error 2002 (hy000): can't connect to local mysql server through socket '/var/lib/mysql/mysql.sock' (2)</b>
[root@wonhigh-test16 ~]#
嘗試127.0.0.1 tcp/ip連接配接(第三方工具遠端連接配接都可以‘連接配接屬性會顯示為tcp/ip ’)
[root@wonhigh-test16 ~]# mysql -uroot -p123456 -h127.0.0.1
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 127
server version: 5.6.19-log
copyright (c) 2000, 2014, 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>
恢複本地連接配接
[root@wonhigh-test16 ~]# mv /var/lib/mysql/mysql1.sock /var/lib/mysql/mysql.sock
your mysql connection id is 121
server version: 5.6.19-log mysql community server (gpl)
mysql> exit
bye
【源于本人筆記】 若有書寫錯誤,表達錯誤,請指正...