How to reset the root password of MySQL in CentOS, centosmysql
After installing mysql on CentOS6.4, I cannot enter through root because the root password is not set during installation. It seems that I have an initial random password, but I don't remember it. It is too troublesome, reset the root password directly.
First, you must have the root permission of the operating system. If you do not have the root permission of the system, consider the root system and then follow the steps below.
PS: grant root permission
Method 1: Modify the/etc/sudoers file, locate % wheel and remove the comment (#).
## Allows people in group wheel to run all commands%wheel ALL=(ALL) ALL
Then modify the user and make it belong to the root group (wheel). The command is as follows:
#usermod -g root tommy
After the modification is completed, you can log on with the tommy account and run the sudo su-command to obtain the root permission for the operation.
Method 2: Modify the/etc/sudoers file, locate the root line, and add a line under root, as shown below:
## Allow root to run any commands anywhereroot ALL=(ALL) ALLtommy ALL=(ALL) ALL
After the modification is completed, you can log on with the tommy account and run the sudo su-command to obtain the root permission for the operation.
Method 3: Modify the/etc/passwd file, find the following line, and change the user ID to 0, as shown below:
tommy:x:500:500:tommy:/home/tommy:/bin/bash
After modification
tommy:x:0:500:tommy:/home/tommy:/bin/bash
Save. After you log on with the tommy account, you can directly obtain the permissions of the root account.
Root is similar to logging on to the system in safe mode. Someone suggested pkill mysql, but I do not recommend it. Because when you execute this command, it will lead to the following situation:
/etc/init.d/mysqld statusmysqld dead but subsys locked
In this way, even if you start mysql in safe mode, it may not be useful. d/mysqld stop. If you use pkill, start and stop again.
After installing mysql using the rpm package, follow these steps to reset the root password:
Start mysql:
#/etc/init.d/mysql start
Check the mysql process information and obtain the installation directory of mysqld_safe (critical ):
#ps -ef | grep -i mysqlroot 3466 1 0 01:45 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/BSC.TEST.pidmysql 3569 3466 16 01:45 pts/1 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/BSC.TEST.err --pid-file=/var/lib/mysql/BSC.TEST.pidroot 3597 3105 0 01:45 pts/1 00:00:00 grep -i mysql
You can see the installation location of mysqld_safe (the blue part above):/usr/bin/
Run the following command to stop mysql:
/etc/init.d/mysql stop
Start mysql in a safe way:
#/usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
Wait for 5 seconds and then execute the following statement:
#/usr/bin/mysql -u root mysql
Note: The mysql and mysql_safe directories are the same:/usr/bin/, which is obtained through the "ps-ef | grep-I mysql" command.
Enter:
mysql> update user set password = Password('root') where User = 'root';
Press enter and execute (refresh the table related to MySQL system permissions ):
mysql> flush privileges;
Then execute exit to exit:
mysql> exit;
After exiting, run the following command to log on to mysql and check whether it is successful:
#mysql -u root -p
Enter the password as prompted:
root
However, an error is returned when you run the command to view the database:
mysql> show databases;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
Solution:
mysql> SET PASSWORD=PASSWORD('root');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.00 sec)
PS: If mysqladmin is used:
# mysqladmin -u root -p password "test123"
Enter password: [Enter the original password]