This is a solution for forgetting the password of the mysql root Super administrator. Let's take a look at the two methods for changing the user name and password of mysql.
This is a solution for forgetting the password of the mysql root Super administrator. Let's take a look at the two methods for changing the user name and password of mysql.
You must be running on the same machine in MySQL. You must be connected as the MySQL root user, and the root user must have the insert permission and reload management permission on the MySQL database. In addition, if you change the root user password, you must specify it using the following MySQL command.
You can add new users by issuing the GRANT statement:
Shell> mysql -- user = root mysql Mysql> grant all privileges on *. * TO monty @ localhost Identified by 'something' with grant option; Mysql> grant all privileges on *. * TO monty @ "%" Identified by 'something' with grant option; Mysql> grant reload, process on *. * TO admin @ localhost; Mysql> grant usage on *. * TO dummy @ localhost; |
These GRANT statements install three new users:
Monty: A full super user who can connect to the server from anywhere, but must use a password 'something' to do this. Note: You must issue a GRANT statement to monty @ localhost and monty @ "%. If we add a localhost entry, the entry created by mysql_install_db for the anonymous user entry of localhost takes priority when we connect from the local Host, because it has a more specific Host field value, therefore, the user table is arranged in the order of users.
Admin: a user who can connect from localhost without a password and is granted reload and process management permissions. This allows you to run the mysqladmin reload, mysqladmin refresh, mysqladmin flush-* commands, and mysqladmin processlist commands. No database-related permissions are granted. They can GRANT permissions in the future by issuing another GRANT statement.
Dummy: you can connect to a user without a password, but only from the local host. The global permission is set to 'n' -- the USAGE permission type allows you to set a user without permission. It assumes that you will be granted database-related permissions in the future.
You can also directly add the same user access information by issuing an INSERT statement, and then tell the server to load the authorization table again:
Shell> mysql -- user = root mysql Mysql> insert into user VALUES ('localhost', 'monty ', PASSWORD ('something '), 'Y ', 'y', 'y ') Mysql> insert into user VALUES ('%', 'monty ', PASSWORD ('something '), 'Y ', 'y', 'y ') Mysql> insert into user SET Host = 'localhost', User = 'admin ', Reload_priv = 'y', Process_priv = 'y '; Mysql> insert into user (Host, User, Password) VALUES ('localhost', 'dummy ',''); Mysql> flush privileges; |
Depending on your MySQL version, for the above, you may have to use a different number of 'y' values (versions earlier than 3.22.11 have fewer permission columns ). For admin users, only the INSERT extension syntax that is more readable in version 3.22.11.
Note: To set a Super user, you only need to create a user table entry with the permission field set to 'y '. No db or host table entries are required.
The permission columns in the user table are not explicitly set by the last INSERT statement (for dummy users), so those columns are assigned the default value 'n '. This is the same thing grant usage does.
In the following example, add a User custom, which can be connected from the host localhost, server. domain, and whitehouse.gov. He only wants to access the bankaccount database from localhost, the expenses database from whitehouse.gov, and the customer database from all three hosts. He wants to use the password stupid from all three hosts.
To use the GRANT statement to set permissions for individual users, run these commands:
Shell> mysql -- user = root Mysql> grant select, INSERT, UPDATE, DELETE, CREATE, DROP ON bankaccount .* TO custom @ localhost Identified by 'stupid '; Mysql> grant select, INSERT, UPDATE, DELETE, CREATE, DROP ON expenses .* TO custom@whitehouse.gov Identified by 'stupid '; Mysql> grant select, INSERT, UPDATE, DELETE, CREATE, DROP ON customer .* TO custom @ '%' Identified by 'stupid '; |
Run these commands by directly modifying the authorization table to set user permissions (Note: flush privileges at the end ):
Shell> mysql -- user = root Mysql> insert into user (Host, User, Password) VALUES ('localhost', 'custom', PASSWORD ('stupid ')); Mysql> insert into user (Host, User, Password) VALUES ('server. domain ', 'custom', PASSWORD ('stupid ')); Mysql> insert into user (Host, User, Password) VALUES ('whitehouse. gov ', 'custom', PASSWORD ('stupid ')); Mysql> insert into db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv) VALUES ('Localhost', 'bankaccount', 'custom', 'y '); Mysql> insert into db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv) VALUES ('Whitehouse. gov ', 'expenses', 'custom', 'y '); Mysql> insert into db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv) VALUES ('%', 'customer', 'custom', 'y', 'y '); Mysql> flush privileges; |
The first three INSERT statements Add user table entries, allowing custom to connect from different hosts with a given password, but no permission is granted (all permissions are set to the default value 'n '). Add db table entries in the last three INSERT statements, and grant custom database permissions to the bankaccount, expenses, and customer databases. However, the database can only be accessed from the correct host. Generally, when the authorization table is directly modified, the server must be notified to mount them again (use flush privileges) to make the permission modification take effect. If you want to give a specific user access to any machine on a given domain, you can issue the following GRANT statement:
Mysql> GRANT... ON *.* TO myusername @ "% .mydomainname.com" Identified by 'mypassword '; |
To do the same thing by directly modifying the authorization table:
Mysql> insert into user VALUES ('% .mydomainname.com', 'myusername ', PASSWORD ('mypassword '),...); Mysql> flush privileges; |