You need to enable JavaScript to run this app. Ubuntu环境下无法重置MySQL root密码的问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu环境下无法重置MySQL root密码的问题求助

Ubuntu 18.04 MySQL root用户登录权限问题

通过SSH连接Ubuntu 18.04服务器,安装MySQL并将root用户认证方式改为mysql_native_password后,出现如下错误:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

尝试多种密码重置方案均未解决,期间遇到其他报错,完整操作流程及错误信息如下:

ubuntu@ip-my-ip-addr:~$ sudo apt update
ubuntu@ip-my-ip-addr:~$ sudo apt install mysql-server
ubuntu@ip-my-ip-addr:~$ sudo systemctl start mysql.service
ubuntu@ip-my-ip-addr:~$ sudo mysql_secure_installation
ubuntu@ip-my-ip-addr:~$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
ubuntu@ip-my-ip-addr:~$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
ubuntu@ip-my-ip-addr:~$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.41-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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
ubuntu@ip-my-ip-addr:~$ sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.41-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             |                                           | auth_socket           | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *D777F42287F9A654F63676430A68018AABBACE40 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ‘my_password-(sameasadminpassword)’;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user             | authentication_string                     | plugin                | host      |
+------------------+-------------------------------------------+-----------------------+-----------+
| root             | *14585CD1271D95EF99B6170050F18E6C49EE2BD2 | mysql_native_password | localhost |
| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *D777F42287F9A654F63676430A68018AABBACE40 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)

mysql> exit
Bye
ubuntu@ip-my-ip-addr:~$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ubuntu@ip-my-ip-addr:~$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ubuntu@ip-my-ip-addr:~$ sudo service mysql status
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2023-02-17 15:03:40 UTC; 24min ago
 Main PID: 2930 (mysqld)
    Tasks: 29 (limit: 1134)
   CGroup: /system.slice/mysql.service
           └─2930 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Feb 17 15:03:40 ip-my-ip-addr systemd[1]: Starting MySQL Community Server...
Feb 17 15:03:40 ip-my-ip-addr systemd[1]: Started MySQL Community Server.
ubuntu@ip-my-ip-addr:~$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ubuntu@ip-my-ip-addr:~$ sudo mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
ubuntu@ip-my-ip-addr:~$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ubuntu@ip-my-ip-addr:~$ sudo systemctl stop mysql
ubuntu@ip-my-ip-addr:~$ sudo mysqld_safe --skip-grant-tables
2023-02-17T15:34:58.081851Z mysqld_safe Logging to syslog.
2023-02-17T15:34:58.085719Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2023-02-17T15:34:58.089306Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
ubuntu@ip-my-ip-addr:~$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ubuntu@ip-my-ip-addr:~$ sudo /etc/init.d/mysql stop
[ ok ] Stopping mysql (via systemctl): mysql.service.
ubuntu@ip-my-ip-addr:~$ sudo mysqld --skip-grant-tables &
[1] 4263
ubuntu@ip-my-ip-addr:~$ mysql -u root mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[1]+  Exit 1                  sudo mysqld --skip-grant-tables
ubuntu@ip-my-ip-addr:~$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ubuntu@ip-my-ip-addr:~$ sudo mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ubuntu@ip-my-ip-addr:~$ sudo systemctl stop mysql
ubuntu@ip-my-ip-addr:~$ sudo usermod -d /var/lib/mysql/ mysql
ubuntu@ip-my-ip-addr:~$ sudo systemctl start mysql
ubuntu@ip-my-ip-addr:~$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ubuntu@ip-my-ip-addr:~$ sudo mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
ubuntu@ip-my-ip-addr:~$ sudo grep 'temporary password' /var/log/mysqld.log
grep: /var/log/mysqld.log: No such file or directory
ubuntu@ip-my-ip-addr:~$ sudo service mysql stop
ubuntu@ip-my-ip-addr:~$ sudo mysqld_safe --skip-grant-tables --skip-networking
2023-02-17T15:42:17.816960Z mysqld_safe Logging to syslog.
2023-02-17T15:42:17.820925Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2023-02-17T15:42:17.824572Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
ubuntu@ip-my-ip-addr:~$ mysql -u root
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ubuntu@ip-my-ip-addr:~$ sudo mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ubuntu@ip-my-ip-addr:~$ sudo systemctl stop mysql
ubuntu@ip-my-ip-addr:~$ sudo usermod -d /var/lib/mysql/ mysql
usermod: no changes
ubuntu@ip-my-ip-addr:~$ sudo systemctl start mysql
ubuntu@ip-my-ip-addr:~$ sudo mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
ubuntu@ip-my-ip-addr:~$ sudo /etc/init.d/mysql stop
[ ok ] Stopping mysql (via systemctl): mysql.service.
ubuntu@ip-my-ip-addr:~$ sudo systemctl stop mysql
ubuntu@ip-my-ip-addr:~$ sudo mysqld --skip-grant-tables &
[1] 4905
ubuntu@ip-my-ip-addr:~$ mysql -u root mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
[1]+  Exit 1                  sudo mysqld --skip-grant-tables
ubuntu@ip-my-ip-addr:~$ sudo systemctl restart mysql
ubuntu@ip-my-ip-addr:~$ mysql -u root mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
ubuntu@ip-my-ip-addr:~$ 

内容的提问来源于stack exchange,提问作者Randusr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:27:26