1. First, confirm that the server is in a secure state, that is, no one can connect to the MySQL database arbitrarily.
Because during the resetting MySQL root password, the MySQL database is completely out of password protection, and other users can also log in and modify MySQL information at will. The server's quasi-security state can be achieved by closing MySQL's external ports and stopping Apache and all user processes. The safest state is to operate on the server's console and unplug the network cable.
2. Modify MySQL login settings:
vim /etc/
Add a sentence to the paragraph of [mysqld]: skip-grant-tables
For example:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/ skip-grant-tables
Save and exit vi.
3. Restart mysqld
service mysqld restart Stopping MySQL: [ OK ] Starting MySQL: [ OK ]
4. Log in and modify MySQL's root password
mysql
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 to server version: 3.23.56 Type ‘help;' or ‘\h' for help. Type ‘\c' to clear the buffer. mysql> USE mysql ; Database changed mysql> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; Query OK, 0 rows affected (0.00 sec) Rows matched: 2 Changed: 0 Warnings: 0 mysql> flush privileges ; Query OK, 0 rows affected (0.01 sec) mysql> quit
5. Modify MySQL login settings back
vim /etc/
Delete the skip-grant-tables that you just added in the [mysqld] segment
Save and exit vim
6. Restart mysqld
service mysqld restart Stopping MySQL: [ OK ] Starting MySQL: [ OK ]
Allow remote connections
GRANT ALL PRIVILEGES ON . TO root@'%' IDENTIFIED BY ‘your password';
% means that there are many machines.
Open port 3306, set exceptions for the firewall, and release 3306.
Open the iptables configuration file:
vi /etc/sysconfig/iptables
Add a line in the middle
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
After all the modifications are completed, restart iptables:
service iptables restart
You can verify that the rules have already taken effect: iptables -L
The above is the method of forgetting mysql password in Centos and allowing remote connections that the editor introduces to you. The editor will reply to you in time. Thank you very much for your support for my website!