mysql.sock丟失後如何連接數據庫

在MySQL 服務器本機上連接數據庫時,經常會出現mysql.sock 不存在,導致無法連接的問
題。這是因爲如果指定localhost 作爲一個主機名,則mysqladmin 默認使用UNIX 套接字文
件連接,而不是TCP/IP。而這個套接字文件(一般命名爲mysql.sock)經常會因爲各種原因
而被刪除。從MySQL 4.1 開始,通過—protocol= TCP | SOCKET | PIPE | MEMORY}選項,用戶可
以顯式地指定連接協議 ,下面演示了使用UNIX 套接字失敗後,使用TCP 協議連接成功的
例子:

UNIX 套接字連接:

[[email protected] tmp]# mv /tmp/mysql.sock /tmp/mysql.sock.bak
[[email protected] tmp]# mysql -uroot -proot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

TCP 連接:

[[email protected] tmp]# mysql --protocol=TCP -uroot -p -P3306 -hlocalhost
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or /g.
Your MariaDB connection id is 13
Server version: 10.2.10-MariaDB-log MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章