Home  >  Article  >  Database  >  How to reset MySQL root password in Linux

How to reset MySQL root password in Linux

不言
不言Original
2019-03-01 11:10:396194browse

MySQL is an open source database software widely used for data storage. Sometimes we forget the MySQL root password, but there is no need to be nervous. This article will introduce how to reset the MySQL root password in simple steps.

How to reset MySQL root password in Linux

(Related recommendations: MySQL Tutorial)

Step 1: Start MySQL in safe mode

First, you need to stop running the mysql server. We use one of the following commands to stop the MySQL server on Linux systems.

# service mysql stop           //对于基于SysVinit的系统
# systemctl stop mysql.service     //对于基于Systemd的系统

Now start the mysql server in safe mode using the --skip grant tables option. Use the following command to start MySQL in safe mode. In safe mode, MySQL does not prompt for a login password.

# mysqld_safe --skip-grant-tables &

Step 2: Reset mysql root password

Now log in to the mysql server as root user and change the password using the following command. This will reset the mysql root password on your system.

For MySQL5.6 or lower version

# mysql -u root
 mysql>USE mysql;
 mysql>UPDATE user SET password=PASSWORD("NEW-PASSWORD") WHERE User='root';
 mysql>FLUSH PRIVILEGES;
 mysql>quit

For MySQL5.7 or higher version

# mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("NEW-PASSWORD");
mysql>FLUSH PRIVILEGES;
mysql>quit

Step 3: Restart the mysql server

After changing the password, stop the mysql (running in safe mode) service and restart it using the command below.

//基于SysVinit的系统
# service mysql stop 
# service mysql start

//基于Systemd的系统 
# systemctl stop mysql.service
# systemctl start mysql.service

Step 4: Verify the new password

After resetting the mysql root account password and restarting, just log in to verify the new password.

# mysql -u root -p

Enter password: **********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.5.57 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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>

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the php Chinese website! ! !

The above is the detailed content of How to reset MySQL root password in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn