mysql重置root密码

英文原文参考地址

1、检查版本
mysql –version
MySQL output

mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper
Or output like this for MariaDB:

MariaDB output
mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1

2、停止服务

MySQL with:
sudosystemctl stop mysql

MariaDB wtih:
sudosystemctl stop mariadb

3、开启安全模式
sudo mysqld_safe –skip-grant-tables –skip-networking &

4、使用root免密码登录
mysql -uroot

MySQL prompt
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql>
MariaDB prompt
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [(none)]>

5、FLUSH PRIVILEGES;
非常关键,必须先执行这个,然后执行后面的语句才有效

6、修改密码
For MySQL 5.7.6 and newer as well as MariaDB 10.1.20 and newer, use the following command.

ALTER USER’root’@’localhost’IDENTIFIED BY ‘new_password’;

For MySQL 5.7.5 and older as well as MariaDB 10.1.20 and older, use:

SET PASSWORD FOR ‘root’@’localhost’=PASSWORD(‘new_password’);

或者
UPDATE mysql.user SET authentication_string =PASSWORD(‘new_password’)WHERE User =’root’AND Host =’localhost’;

7、重启
For MySQL, use:
sudokillcat/var/run/mysqld/mysqld.pid

For MariaDB, use:
sudokill/var/run/mariadb/mariadb.pid

Then, restart the service using systemctl.

For MySQL, use:
sudo systemctl start mysql

For MariaDB, use:
sudo systemctl start mariadb

百度大模型

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

腾讯云图