How to reset MySQL ROOT password under Linux

  
 

When MYSQL is installed, the password configuration interface will pop up by default, which shows the password of the root user, but once you forget it, you can only find a way to reset it. There are several ways to reset the ROOT password.

Method 1. Use mysqladmin

# ./mysqladmin -u root password 'newpassword'

# ./mysqladmin -u root -h host_name password 'newpassword'

Usually mysqladmin's path is /usr/bin, host_name is your real host name, eg localhost.localdomain.

The quotation marks after the password are not required, but if the password contains spaces or some special symbols, Quotes are required.

Method 2, use mysql SET PASSWORD command

# ./mysql -u root

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword ');

mysql> SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpassword');

Method 3. Update the user table with the UPDATE statement to reset the ROOT password

# ./mysql -u root

mysql> UPDATE mysql.user SET Password = PASSWORD('newpassword') WHERE User = 'root';

mysql> FLUSH PRIVILEGES;

Method 4, start MYSQL safe mode reset ROOT password

1, stop MySQL process

Execute: /etc/init.d/mysql stop, specific location may Depending on the system, it may be /etc/init.d/mysql, /etc/init.d/mysqld, etc., or terminate directly below (preferably do not use the following mandatory statement):

# killall -TERM mysqld

2, start MySQL in safe mode

# mysqld_safe –skip-grant-tables &

or,

# Mysqld_safe --skip-grant-tables >/dev/null 2>&1 &

Tip: mysqld_safe is generally /Usr /local /mysql /bin /directory.

3, login MYSQL

After completing the above two steps, you can enter MySQL without password

# mysql -u root

or,

# /usr/local/mysql/bin/mysql -u root mysql

4, change the ROOT password

The following sentences are executed in order:

use mysql;

select host, user, password from user;

update user set password=password(“newpassword”) where user=”root”

flush privileges;< Br>


5. Exit the console and restart the MYSQL service

service mysqld restart

or,

/etc/init.d/Mysql restart

Copyright © Windows knowledge All Rights Reserved