What should I do if I forget the password of the root account of the MySQL database?

It is easy to solve the problem of forgetting the ordinary account password of the MySQL database. We can directly log in to the root account to reset the ordinary account password. What if the root account password is forgotten or lost?

This chapter will teach you how to reset the root account password to ensure it is effective.

Version: MySQL5.7

Operating system: CentOS/Ubuntu

method 1:

1. Stop MySQL service

# kill `cat/var/run/mysqld/mysqld.pid`

或者# pkill mysqld

2. Create a text file of password assignment statements

# vi mysql-init

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass!6';

The instance will execute this statement to reset the password before loading the authorization table.

3. Use the -init-file option to start the MySQL service

# mysqld --init-file=mysql-init --user=mysql &

4. Delete the text file and use the new password to connect to MySQL

# rm -f mysql-init

# mysql -uroot -p 'MyNewPass!6'

5. Stop the MySQL service and start it normally

# kill `cat/var/run/mysqld/mysqld.pid`

# systemctl start mysqld

Method 2:

1. Stop MySQL service

# kill `cat/var/run/mysqld/mysqld.pid`

or

# pkill mysqld

2. Use the --skip-grant-tables and --skip-networking options to start the MySQL service

# mysqld--skip-grant-tables --skip-networking --user=mysql

--skip-grant-tables:跳过授权表认证

--skip-networking:加了跳过授权表选项后所有的人都可以无密码登录,这是很不安全的,此选项不监听网络,防止恶意登录。

3. Connect to MySQL without a password and reset the password

# mysql

mysql> FLUSH PRIVILEGES;

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass!6';

4. Stop the MySQL service and start it normally (without the above two skip options)

# kill `cat/var/run/mysqld/mysqld.pid`

# systemctl start mysqld

Guess you like

Origin blog.51cto.com/15127501/2657391