CentOS reset MySQL root password tutorial

  • 2020-12-05 17:23:52
  • OfStack

After I installed 10,000 mysql on CentOS6.4, I could not access it through root, because the root password was not set at the time of installation. It seems that there was an initial random password, but I don't remember it. It is too much trouble to reset the root password directly.
First, you must have root permissions on the operating system. If you don't even have root permissions for your system, consider the root system before you go through the following steps.

PS: Grant permission for root
Method 1: Modify the /etc/sudoers file to find the %wheel1 line and remove the previous comment (#)


## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL

Then modify the user to belong to the root group (wheel) with the following command:


#usermod -g root tommy

After modification, now you can log in with the tommy account, and then use the command sudo su - to get the root permission to operate.

Method 2: Modify the /etc/sudoers file, find root1 line, add 1 line under root, as follows:


## Allow root to run any commands anywhere
root ALL=(ALL)  ALL
tommy ALL=(ALL)  ALL

After modification, you can now log in with the tommy account, and then use the command sudo su - to obtain the root permission to operate.

Method 3: Modify the /etc/passwd file. Find the following line and change user ID to 0, as shown below:


tommy:x:500:500:tommy:/home/tommy:/bin/bash

The modifications are as follows


tommy:x:0:500:tommy:/home/tommy:/bin/bash

Save, after logging in with the tommy account, you will directly obtain the permissions of the root account.


root is similar to secure mode login system. Some people suggest pkill mysql, but I don't recommend it. Because when you execute this command, it causes a situation like this:


/etc/init.d/mysqld status
mysqld dead but subsys locked

So even if you are in the safe mode start mysql may not be useful, so the 1 kind is/etc/init d/mysqld stop, if you use the pkill first, then under the start1 stop cough up again.
After installing mysql using rpm package, reset the root password according to step 1 below:
Start the mysql:


#/etc/init.d/mysql start

After successful startup, check the mysql process information and get the installation directory of mysqld_safe (very critical) :


#ps -ef | grep -i mysql
root  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.pid
mysql  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.pid
root  3597 3105 0 01:45 pts/1 00:00:00 grep -i mysql

You can see the installation location of mysqld_safe (highlighted in blue above) : /usr/bin/
Then execute 1 to stop mysql:


/etc/init.d/mysql stop

Start mysql in a safe manner:


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

Wait for 5 seconds, then execute the following statement:


#usermod -g root tommy
0

Note: mysql and mysql_safe are both under: /usr/bin/. This is obtained by using the command "ps-ef | grep-i mysql".

"mysql > After the prompt, type:


mysql> update user set password = Password('root') where User = 'root';

Execute after enter (refresh MySQL system permissions related table) :


mysql> flush privileges;

Then execute exit exit:


#usermod -g root tommy
3

After exiting, login mysql using the following command to see if it works:


#usermod -g root tommy
4

Press the prompt to enter the password:


#usermod -g root tommy
5

But execute the view database command to report an error:


#usermod -g root tommy
6

ERROR 1820 (HY000): You must SET PASSWORD before executing this statement

Solutions:


mysql> SET PASSWORD=PASSWORD('root');

#usermod -g root tommy
9

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 you are using mysqladmin:


# mysqladmin -u root -p password "test123"

Enter password:  [Enter the original password] 


Related articles: