Linux reset MySQL root password detailed

Source: Internet
Author: User
Tags flush mysql in safe mode

Restart the MySQL root password

Methods one, using Mysqladmin

The code is as follows Copy Code

#./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, e.g. Localhost.localdomain.

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

Method II, using MySQL SET password command

The code is as follows Copy Code

#./mysql-u Root

mysql> SET PASSWORD for ' root ' @ ' localhost ' = PASSWORD (' NewPassword ');

mysql> SET PASSWORD for ' root ' @ ' host_name ' = PASSWORD (' NewPassword ');

Method Three, update the user table with the UPDATE statement reset root password

The code is as follows Copy Code

#./mysql-u Root

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

mysql> FLUSH privileges;

Method Four, start MySQL safe mode reset root password

1, stop the MySQL process

Execution:/etc/init.d/mysql stop, the specific location may vary depending on the system, it may be the path of/etc/init.d/mysql,/etc/init.d/mysqld, or the following direct termination (preferably do not use the following mandatory statement):

The code is as follows Copy Code

# Killall-term Mysqld

2. Start MySQL in safe mode

The code is as follows Copy Code

# Mysqld_safe–skip-grant-tables &

Or

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

Tip: Mysqld_safe is generally in the/usr/local/mysql/bin/directory.

3, login MySQL

After the two steps are completed, you can enter MySQL without the password.

The code is as follows Copy Code

# mysql-u Root

Or

#/usr/local/mysql/bin/mysql-u Root MySQL

4, change the root password

The following sentences are executed sequentially:

The code is as follows Copy Code

Use MySQL;

Select Host, user, password from user;

Update user set Password=password ("NewPassword") where user= "root"

Flush privileges;

5. Exit the console and restart the MySQL service

The code is as follows Copy Code

Service mysqld Restart

Or

/etc/init.d/mysql restart

Forget the MySQL root password


First, manual modification

1. Stop MySQL Service

Execution:/etc/init.d/mysql stop, not necessarily on your machine/etc/init.d/mysql may also be/etc/init.d/mysqld

2, skip verification to start MySQL

The code is as follows Copy Code

/usr/local/mysql/bin/mysqld_safe--skip-grant-tables >/dev/null 2>&1 &

Note: If the location of the Mysqld_safe is not the same as the above need to be modified to your, if not clear can be found with the Find command.

3. Reset Password

Wait a minute and then execute:/usr/local/mysql/bin/mysql-u root MySQL

When the MySQL prompt appears, enter: Update user Set password = password (' Password to set ') where user = ' root ';

After carriage return carries out: flush privileges; Refresh the MySQL system permissions related tables.  Re-execution: exit; Exit.

4, restart MySQL

Kill MySQL Process: Killall mysqld

Restart Mysql:/etc/init.d/mysql start

Second, use the Reset script

The code is as follows Copy Code

wget Http://soft.vpser.net/lnmp/ext/reset_mysql_root_password.sh;sh reset_mysql_root_password.sh

Suitable for LNMP or MySQL admin scripts in/etc/init.d/mysql and MySQL installed in/usr/local/mysql/. Otherwise, you need to modify the script before you execute it

  code is as follows copy code

Sh reset_ Mysql_root_password.sh.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.