Local Windows modified mysql’s root password is invalid, theory 127.0.0.1 and localhost, understand all methods

Let me talk about the problem I encountered first:

   There is a project that uses 127.0.0.1 to connect to the local database, and now I need to set the local database password. So use the command through the cmd window:

#mysql -uroot -p // I already know that there is no password , You can log in here
mysql>update mysql.user set password=password(‘mypassword’) where user=’root’;
mysql>flush privileges;
mysql>quit;

  The password is changed, but the project still cannot connect after using the above password. Use the command through the cmd window, and use the modified password.

I figured it out later.

Solution:   

MySQL has three login commands:

(
1)#mysql -uroot -p
(
2)#mysql -uroot -p -h localhost
(
3)#mysql -uroot -p -h 127.0 span>.0.1

  See MySQL’s mysql.user table:

Share pictures

   are all root users, with different login methods , There are different passwords.

   now that you understand the principle, let’s modify it:

 first through known methods Log in:

#mysql
-uroot -p
Modify password:
grant all on
*.* to [email protected]'%< /span>' identified by 'mypassword' with grant option;
grant all on
*.* to [email protected]'127.0. 0.1' identified by 'mypassword' with grant option;
grant all on
*.* to [email protected]'localhost< /span>' identified by 'mypassword' with grant option;
flush privileges;

Supplement one:

   These three paths are different, please investigate:

p>

share picture

   proves that (1)#mysql -uroot -p is the same as (2)#mysql -uroot -p -h localhost, but is different from (3).

Supplement 2:

What is the difference between localhost and 127.0.0.1?
I believe some people will say that it is a local IP. Someone once said that using 127.0.0.1 is better than localhost. Can reduce one parsing.
It seems that this issue is still unclear. In fact, there is a difference between the two.
localhost is also called local, the correct interpretation is: local server
The correct interpretation of 127.0.0.1 in windows and other systems is: the local address (local server) is resolved through the host file of the local machine, windows automatically Resolving localhost to 127.0.0.1 localhot (local) is not transmitted via the network card! This is very important because it is not restricted by network firewalls and network cards. 127.0.0.1 is transmitted through the network card, which relies on the network card, and is subject to the restrictions of the network firewall and the network card. Generally, it is best to use localhost for local services when setting up the program. localhost will not be resolved into ip, nor will it take up network cards and network resources. Sometimes localhost can be used, but 127.0.0.1 cannot be used. This is the case. Suppose that when localhost is accessed, the system brings the current user of the machine to access it, and when using ip, it means that the machine is accessing the machine through the network, which may involve the authority of the network user.

#mysql -uroot -p // I already know that there is no password, you can log in here
mysql>update mysql.user set password=password(‘mypassword’) where user=’root’;
mysql>flush privileges;
mysql>quit;

MySQL has three login commands:

(
1)#mysql -uroot -p
(
2)#mysql -uroot -p -h localhost
(
3)#mysql -uroot -p -h 127.0 span>.0.1

Pass the known Way to log in:

#mysql
-uroot -p
Modify password:
grant all on
*.* to [email protected]'%< /span>' identified by 'mypassword' with grant option;
grant all on
*.* to [email protected]'127.0. 0.1' identified by 'mypassword' with grant option;
grant all on
*.* to [email protected]'localhost< /span>' identified by 'mypassword' with grant option;
flush privileges;

Leave a Comment

Your email address will not be published.