0
点赞
收藏
分享

微信扫一扫

MySql 忘记 Root 密码

停止 mysql 服务

使用如下命令查询进程号,把 mysql 对应的进程都关闭即可

$>  ps -ef | grep mysql
root     1373488       1  0 8月02 ?       00:00:00 /bin/sh /opt/app/soft/mysql-8.4.2/bin/mysqld_safe --user=mysql
mysql    1373672 1373488  0 8月02 ?       02:02:31 /opt/app/soft/mysql-8.4.2/bin/mysqld --basedir=/opt/app/soft/mysql-8.4.2 --datadir=/opt/app/soft/mysql-8.4.2/data --plugin-dir=/opt/app/soft/mysql-8.4.2/lib/plugin --user=mysql --log-error=iZ2vchr1whgzf4lgqr4d1gZ.err --pid-file=iZ2vchr1whgzf4lgqr4d1gZ.pid --port=3608
root     2007078 1935362  0 13:36 pts/0    00:00:00 grep --color=auto mysql

使用 --skip-grant-tables 启动参数重启数据库

使用 --skip-grant-tables 参数启动

$> mysqld_safe --user=mysql --skip-grant-tables &

登录

$> mysql -u root -p

修改 root 密码

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> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> alter user 'root'@'localhost' identified by 'Root_12345566';
Query OK, 0 rows affected (0.01 sec)

去掉 --skip-grant-tables 参数重启数据库

$> mysqld_safe --user=mysql &

修改密码报错

mysql> alter user 'root'@'localhost' identified by 'Root_12345566';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'

# 查询一下用户以及其对应的 host
mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
+-----------+------------------+
4 rows in set (0.00 sec)
mysql> update user set host = 'localhost' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
举报

相关推荐

0 条评论