解決MySQL數據庫問題-ERROR 2002 (HY000): Can't connect to local MySQL server through socket

前言

測試環境的一個小問題,放好久了,今天正好有時間解決,解決方法很簡單,維穩處理,不重啓數據庫服務,不改配置文件

問題

直接用命令登錄數據庫,登不上去

# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

提示訪問不到socket

原因

根據配置文件“/etc/my.cnf”的路徑,未找到相應的socket文件,就會引發此錯誤

解決

這裏就不改配置文件,因爲雖然測試環境,但也是有人用的,等晚上處理沒那耐心,所以按下面方法處理問題

尋找sock文件

先找sock文件,用find命令,從根目錄開始找,/var/lib/mysql/mysql.sock這個這個就是,至於mysqlx.sock是以前裝mysql剩下的,不足爲提

# find / -name "*.sock"
/run/rpcbind.sock
/var/lib/mysql/mysql.sock
/tmp/mysqlx.sock

創建軟連接

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

問題解決

# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3930036
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章