升級
項目需要使用Mysql8.0, 查看自己的ubuntu22.04上mysql版本為5.7, 使用以下命令自動升級到8.0版本。
sudo apt install Mysql
sock錯誤: Can’t connect to local MySQL server through socket
運行mysql -u -p 報以下錯誤:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysqld.sock'
解決如下: 修改配置文件,增加以下內容:
[mysqld]
socket = /tmp/mysqld.sock[client]
socket = /tmp/mysqld.sock
修改之后 ,重啟mysql服務
sudo systemctl restart mysql
密碼問題: ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’
忘記密碼會提示以下錯誤,以下是重置mysql密碼方法:
#執行以下命令,輸入密碼
mysql -u root -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)#停止服務
(base) moon@moon:~$ sudo service mysql stop#跳過密碼檢查
(base) moon@moon:~$ sudo mysqld_safe --skip-grant-tables &
[1] 27049
(base) moon@moon:~$ 2025-06-19T09:30:30.942961Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2025-06-19T09:30:30.998082Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql#進入mysql,
(base) moon@moon:~$ mysql -u root
# 重新設置密碼
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.06 sec)mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.06 sec)mysql> quit
Bye#重啟服務
(base) moon@moon:~$ sudo service mysql restart#重新進入
base) moon@moon:~$ 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.36-0ubuntu0.22.04.1 (Ubuntu)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>