How to modify MySQL database when setting password and forgetting password

Source: Internet
Author: User

Environment Introduction:
The test environment I used was the red Hat linux6 version of the server that installed Mysql5.7.
You have previously talked about the MySQL server installation process and can use the Navicat tool to connect to MySQL server. Here's how to set up an account password for MySQL to log in.
When I first installed the service, I added a message in the/ETC/MY.CNF configuration file Skip-grant-tablesAllows it to log on to MySQL server without password authentication. The following is a description of the MySQL database to set the password and forgot password when the method of modification.

Note: MySQL5.7 after initial installation, a random initial password is generated and recorded in the/var/log/mysqld.log, which can be viewed through the cat command, and the password keyword is found to find the random initial password.

Here's how to set the password:
Method 1: Edit the user table directly with update

  mysql-u Root/* Enter Mysql*/mysql>use mysql/* into MySQL user */mysql>update users set authentication_string= Password (' Newpass '), password_expired= ' N where user= ' root '; /This SQL applies to versions after mysql5.7 (including 5.7), prior to 5.7 versions for update user set Password = Password (' newpass ') WHERE user = ' root ';*/mysql>   FLUSH privileges; /* Tell the server to overload the authorization table */ 

Quit MySQL, edit the/etc/my.cnf file, delete the contents of the Skip-grant-tables, and log in with your account password when you restart the service.
Method 2:mysqladmin Command (of course, if your mysqladmin is not connected to MySQL server, or you have no way to execute mysqladmin, this method is invalid)
mysqladmin -u root password "newpass" /*设置的密码为newpass*/<br/>如果要修改用户名密码
Method 3: Assign a password to the account with the Set password command
mysql&gt; SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘newpass‘);
Method 4: If you are not connected as an anonymous user, you can change your password by omitting the FOR clause:
mysql&gt; SET PASSWORD = PASSWORD(‘newpass‘);
Method 5: You can also use the grant usage statement at the global level (in .) To specify a password for an account without affecting the current permissions of the account:
mysql&gt; GRANT USAGE ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘newpass‘;

add: Establish a password when creating a new account

mysql> INSERT INTO user (Host,User,Password) VALUES(‘%‘,‘oldboy‘,PASSWORD(‘newpass‘));mysql> FLUSH PRIVILEGES;

When you use Set PASSWORD, insert, or update to specify the password for an account, it must be encrypted with the PASSWORD () function. (The only exception is if the password is empty, you do not need to use password ()). The need to use password () is because the user table saves the password in an encrypted manner, not in plaintext.

mysql> INSERT INTO user (Host,User,Password) VALUES(‘%‘,‘oldboy‘,‘newpass‘);mysql> FLUSH PRIVILEGES;

The result is that the password ' newpass ' is not encrypted after it is saved to the user table. When Oldboy uses the password to connect to the server, the value is encrypted and compared with the one saved in the user table. However, the saved value is the string ' Newpass ', so the comparison fails and the server rejects the connection:
Access denied

Change the password method as follows:
Method 1: If you forget the root password, you can change it by using the first method of setting the password above.
1. First modify the configuration file/etc/my.cnf, add the skip-grant-tables parameter under [MySQL], restart the service. You can log in to MySQL without entering your password
2. Go to MySQL library, enter use MySQL, and update the MySQL user table with updated
3.FLUSH privileges; / Refresh Permissions /, and then quit MySQL
4. Log off or delete the skip-grant-tables parameter in the MY.CNF configuration file and restart the MySQL service

Method 2: use Mysqladmin
mysqladmin -u root -p password "newpass2" /*这时候回车提示输入的密码是旧密码*/
mysqladmin -u root -pnewpass password "newpass2" /*如果记得旧密码,用这种方式*/

How to modify MySQL database when setting password and forgetting password

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.