0%

修改mysql root密码

搭建mysql后,忘了root用户密码怎么办。记录下之前的操作

如果你docker安装的,那就需要exec到容器中。

1
$ sudo docker exec -it mysql-container /bin/bash

步骤

1
2
3
4
5
6
7
1. sudo /etc/init.d/mysql stop
2. sudo pkill mysql
3. sudo mkdir -p /var/run/mysqld
4. sudo chown mysql /var/run/mysqld
5. sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
6. mysql -uroot
7. flush privileges;

用安全模式登录ROOT账号,用新的的new_password替换原先old_password

1
2
3
4
5
6
7
8
9
10
1. use mysql;
2. update user set plugin='mysql_native_password' where user='root';
3. MySQL 5.7+ : update user set authentication_string=PASSWORD('new_password') where user='root';
4. MySQL 5.6- : update user set password=PASSWORD('new_password') where user='root';
5. If the password conversion function does not work use: SET CREDENTIALS FOR 'root' TO 'new_password';
5. flush privileges;
6. commit;
7. exit;
8. sudo /etc/init.d/mysql stop
9. sudo /etc/init.d/mysql start

如果mysql服务重启失败,就重启系统。