This article introduces two situations: how to modify the root password when you know the root password, and how to modify the root password if you forget the root password, for more information, see how to change the password of the root user in the MySQL database? The following describes how to change the password of the root user.
1: Use the set password statement to modify
mysql> select user();+----------------+| user() |+----------------+| root@localhost |+----------------+1 row in set (0.08 sec) mysql> set password=password('123456');Query OK, 0 rows affected (0.00 sec) mysql> flush privileges;Query OK, 0 rows affected (0.00 sec) mysql> exit
2: update the user table of the mysql database
mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A Database changedmysql> update user set password=PASSWORD('QwE123') where user='root';Query OK, 4 rows affected (0.03 sec)Rows matched: 4 Changed: 4 Warnings: 0 mysql> flush privileges;Query OK, 0 rows affected (0.00 sec) mysql> quit
3: Use the mysqladmin command to modify
The command is mysqladmin-u root-p 'oldpassword' password newpass as follows:
[root@DB-Server ~]# mysqladmin -u root -p'123456' password 'Qwe123'Warning: Using a password on the command line interface can be insecure.
Verify that the root password is successfully modified
[root@DB-Server ~]# mysqladmin -u root -p'123456' password 'Qwe123'Warning: Using a password on the command line interface can be insecure.
The above is the root password. if you forget the root password, how can you change the root password?
1: Stop the MySQL service first.
[root@DB-Server ~]# service mysql stopShutting down MySQL..[ OK ][root@DB-Server ~]#
Or
[root@DB-Server ~]# /etc/rc.d/init.d/mysql stopShutting down MySQL..[ OK ]
2: run the mysqld_safe command to start mysql and update the password of the root account.
-- Skip-grant-tables: Do not start grant-tables (authorization table), skip permission control.
-- Skip-networking: skip the TCP/IP protocol and only access it from the local machine (this option is not required. Yes.
[root@DB-Server ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &[1] 5145You have new mail in /var/spool/mail/root[root@DB-Server ~]# 150709 14:10:53 mysqld_safe Logging to '/var/lib/mysql/DB-Server.localdomain.err'.150709 14:10:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql [root@DB-Server ~]# mysql -u root mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.6.20-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.00 sec) mysql> use mysqlDatabase changedmysql> UPDATE user SET password=PASSWORD("Qwe123") WHERE user='root'; Query OK, 4 rows affected (0.01 sec)Rows matched: 4 Changed: 4 Warnings: 0 mysql> flush privileges;Query OK, 0 rows affected (0.00 sec) mysql> exitBye
3: Restart the MySQL service.
The above is all the content of this article. I hope you will like it.