环境介绍
最近搭建了一个mysql数据库服务器,数据都导进去之后,却怎么也试不出来root密码了。网上找了各种办法都不对,自己折腾出来后,做下记录。
[root@lamp1 ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@lamp1 ~]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
本方法只适用于不含应用的数据库测试,若有前台应用在跑,此方法会影响到前台应用,万勿乱测试。
先介绍下环境:
CentOS系统:版本8.5.2111
mysql数据库:版本8.0.31社区版
[root@lamp1 ~]# uname -a
Linux lamp1 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
[root@lamp1 ~]# cat etc/centos-release
CentOS Linux release 8.5.2111
[root@lamp1 ~]# mysql -V
mysql Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)
遇到的问题
遇到的问题很简单,就是网上的方法用不起来。主要是三点:
1、网上大多数介绍都是Windows系统的环境,对应不上。
2、就算是Linux环境,mysql版本也太低了,更改密码的操作命令都不一样了。
3、采用不验证密码的方法登录进去后,对数据表的操作是可以的,就是使用alter user命令的时候,提示不允许修改密码。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| ErpDb |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select User,Host from user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
上面对数据库或者数据表的查询操作都是正常的,不过更改密码的时候直接提示在skip-grant-tables状态下不允许执行。
mysql> alter user root@localhost identified by 'Aa@123321';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
解决步骤
解决思路就是,先在配置文件中加上skip-grant-tables参数后,免密码进入mysql命令行,这个时候输入更改密码的命令会提示不能够更改密码。再打开一个新的终端,登录系统后,将配置文件中的参数去掉,再次重新启动mysql服务。服务启动正常后,再回到之前的终端,这个终端还在mysql命令行里面,这个时候直接敲命令更改密码,成功完成。
具体操作步骤:
1、查找mysql配置文件位置,编辑my.cnf文件,加入skip-grant-tables参数。
[root@lamp1 ~]# find / -name my.cnf
/etc/my.cnf
[root@lamp1 ~]# vim /etc/my.cnf
[root@lamp1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables
[root@lamp1 ~]#
2、重新启动mysql服务,并确保服务启动成功。
[root@lamp1 ~]# systemctl restart mysqld
[root@lamp1 ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor pres>
Active: active (running) since
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3562 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0>
Main PID: 3589 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 4634)
Memory: 340.3M
CGroup: /system.slice/mysqld.service
└─3589 /usr/sbin/mysqld
3、免密码登录到mysql命令行状态。
[root@lamp1 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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>
4、再次打开一个putty终端,编辑my.cnf文件,将刚刚加上的免密登录参数skip-grant-tables去掉,保存退出。
5、再次重新启动mysql服务,并确保服务启动正常。
6、回到之前的操作终端里面,再次执行更改密码的命令,可以看到,操作会有提示报错,不过密码可是真的改过来了。
mysql> alter user root@localhost identified by 'Aa@123321';
ERROR 2013 (HY000): Lost connection to MySQL server during query
No connection. Trying to reconnect...
Connection id: 8
Current database: *** NONE ***
Query OK, 0 rows affected (0.01 sec)
最后退出来,重新用新的密码进入数据库就好了。
感谢各位的阅读,本公众号会持续分享实际的服务案例,若感兴趣请关注该公众号。
需要上门解决问题时,请及时联系我们!




