ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
MySQL下mysql.sock丟失丟失的原因一般是因爲配置文件不一致的原因,mysqld 錯誤啓動,mysqld_safe 會清除一次mysql.sock 。解決方法是:
判斷一般人解決故障時沒有切換到mysql用戶,造成權限有問題,無法創建mysql授權表,所以也就無法創建/tmp/mysql.sock 和hostname.pid文件。因此,總結解決方法如下:
[root@tencent ~]# su root //切換到root用戶或者具有數據庫權限的其他用戶
[root@tencent ~]# /bin/mysql_install_db //在bin目錄下運行mysql_install_db重建授權表
[root@tencent ~]# /bin/mysqld_safe &
測試
[root@tencent bin]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.41 Source distribution
Copyright (c) 2000, 2018, 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> exit
Bye
[root@tencent bin]# mysql_install_db
Installing MariaDB/MySQL system tables in '/var/lib/mysql' ...
190426 18:19:33 [Note] /usr/libexec/mysqld (mysqld 5.5.60-MariaDB) starting as process 31241 ...
OK
Filling help tables...
190426 18:19:34 [Note] /usr/libexec/mysqld (mysqld 5.5.60-MariaDB) starting as process 31250 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h tencent password 'new-password'
Alternatively you can run:
'/usr/bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/var/lib/mysql'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
[root@tencent ~]# mysqld_safe &
[1] 3559
[root@tencent ~]# 190426 18:31:34 mysqld_safe Logging to '/usr/local/mysql/data/tencent.err'.
190426 18:31:35 mysqld_safe A mysqld process already exists
文件已經解決,重新生成新的 /tmp/mysql.sock 和 hostname.pid
二、解決mysql.sock文件路徑不對
1.在/var/lib/mysql創建一個link:ln -s /var/lib/mysql/mysql.sock /path/to/mysql/mysql.sock
2.在root權限下修改my.cnf文件(/etc/mysql/my.cnf),指定正確的路徑
3.1和2任選其一就可以