ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock '
這個問題發生在賬戶登入的時候,無論密碼正確與否,始終出現2002錯誤。我的解決過程如下
直接刪除臨時資料夾下的4個檔案(這4個檔案許可權是錯的)
[root@instance-20mosyry ~]# rm /tmp/mysql*
rm: remove socket ‘/tmp/mysql.sock’? y
rm: remove regular file ‘/tmp/mysql.sock.lock’? y
rm: remove socket ‘/tmp/mysqlx.sock’? y
rm: remove regular file ‘/tmp/mysqlx.sock.lock’? y
然後重啟mysql服務
[root@instance-20mosyry mysql]# service mysql restart
Redirecting to /bin/systemctl restart mysql.service
再次檢視臨時資料夾,發現這4個檔案又被建立了(因為是mysql自動建立的,許可權已被自動糾正)。
[root@instance-20mosyry mysql]# ll /tmp
total 20
drwxr-xr-x 2 root root 4096 Jun 19 22:41 bcm-agent
srwxrwxrwx 1 mysql mysql 0 Jun 24 14:36 mysql.sock
-rw------- 1 mysql mysql 5 Jun 24 14:36 mysql.sock.lock
srwxrwxrwx 1 mysql mysql 0 Jun 24 14:36 mysqlx.sock
-rw------- 1 mysql mysql 6 Jun 24 14:36 mysqlx.sock.lock
drwxr-xr-x 3 root root 4096 Jun 20 11:05 pear
drwx------ 3 root root 4096 Jun 24 14:33 systemd-private-a68349f1867e4285b26f8
再次使用root賬戶登入時2002錯誤已經不存在了。新的錯誤是密碼錯誤,這個只要找到初始化資料庫時的密碼就可以了。
[root@instance-20mosyry mysql]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: Y
[root@instance-20mosyry mysql]#
[root@instance-20mosyry mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.16
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
輸入顯示資料庫的命令報錯1820,提示需要更改密碼。
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement befo
mysql>
mysql> alter user root@localhost identified by 111111
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
mysql> alter user root@localhost identified by '111111';
Query OK, 0 rows affected (0.01 sec)
更改密碼之後就可以正常操作了。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> quit;
Bye