Several ways to change MySQL password

Several ways to change MySQL password

Preface:

In the daily use of the database, it is inevitable that you will encounter situations where you need to modify your account password, such as when your password is too simple and needs to be modified, when your password expires and needs to be modified, when you forget your password and need to modify it, etc. This article will introduce the scenarios where passwords need to be changed and several ways to change passwords.

1. Forgot root password

It is quite common to forget the root password, especially when the test environment you set up has not been used for a long time. It is easy to forget the password you set at that time. The commonly used method at this time is to skip permission verification, then change the root password, and then enable permission verification. Taking MySQL 5.7 as an example, let’s briefly talk about the main process:

First, modify the configuration file and add a sentence in the [mysqld] section: skip-grant-tables. The purpose of adding this parameter is to skip permission verification. Then restart the database. After the database is started again, we can log in to the database directly without a password to change the password.

# Change the root password in skip-grant-tables mode
 [root@host ~]# mysql
 Welcome to the MySQL monitor. Commands end with ; or \g.
 Your MySQL connection id is 16
 Server version: 5.7.23-log MySQL Community Server (GPL)

  Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

  Oracle is a registered trademark of Oracle Corporation and/or its
 affiliates. Other names may be trademarks of their respective
 owners.

  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  mysql> update mysql.user set authentication_string = password ('xxxxxx') where user = 'root' and host = 'localhost';
 Query OK, 0 rows affected, 1 warning (0.00 sec)
 Rows matched: 1 Changed: 0 Warnings: 1

  mysql> flush privileges;
 Query OK, 0 rows affected (0.01 sec)

After changing the root password, remove the skip-grant-tables parameter again and restart the database.

2. Several ways to change passwords

In addition to forgetting your password, there may be other situations where you need to change your password. At this time, you can use the normal method to change your password. Still taking MySQL 5.7 as an example, we will introduce several commonly used methods for changing passwords.

Use alter user to modify

For example, if we want to change the password of the testuser account, we can log in using the root account and then execute the alter user command to change the password of the testuser account.

mysql> alter user 'testuser'@'%' identified by 'Password1';
 Query OK, 0 rows affected (0.01 sec)

  mysql> flush privileges;
 Query OK, 0 rows affected (0.00 sec)

Using the SET PASSWORD command

Use SET PASSWORD to modify the password. The command format is SET PASSWORD FOR 'username'@'host' = PASSWORD('newpass'); Similarly, the root account can be used to modify the passwords of other accounts.

mysql> SET PASSWORD FOR 'testuser'@'%' = PASSWORD('Password2');
 Query OK, 0 rows affected, 1 warning (0.00 sec)

  mysql> flush privileges;
 Query OK, 0 rows affected (0.00 sec)

Use mysqladmin to change the password

Use the mysqladmin command to modify the account password format: mysqladmin -u username -p old password password new password

[root@host ~]# mysqladmin -utestuser -pPassword2 password Password3
 mysqladmin: [Warning] Using a password on the command line interface can be insecure.
 Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
 [root@host ~]# mysql -utestuser -pPassword3
 mysql: [Warning] Using a password on the command line interface can be insecure.
 Welcome to the MySQL monitor. Commands end with ; or \g.
 Your MySQL connection id is 2388
 Server version: 5.7.23-log MySQL Community Server (GPL)

  Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

  Oracle is a registered trademark of Oracle Corporation and/or its
 affiliates. Other names may be trademarks of their respective
 owners.

  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  mysql>

Directly update the user table

In fact, all MySQL account information is stored in the mysql.user table, and we can also modify the password directly by updating the user table.

# Version 5.7 and later
 mysql> update mysql.user set authentication_string = password ('Password4') where user = 'testuser' and host = '%';
 Query OK, 1 row affected, 1 warning (0.06 sec)
 Rows matched: 1 Changed: 1 Warnings: 1

  mysql> flush privileges;
 Query OK, 0 rows affected (0.01 sec)

  # Version 5.6 and earlier
 update mysql.user set password=password('new password') where user='user name' and host='host';

3. Set login-path for local quick login

In order to prevent password exposure or forgetting, we can also set login-path to achieve quick login without entering password locally.

Login-path is a new feature supported by MySQL 5.6. By using the mysql_config_editor tool, the authentication information for logging into the MySQL service is encrypted and saved in the .mylogin.cnf file (located in the user's home directory by default). The MySQL client tool can connect to MySQL by reading the encrypted file to achieve quick login.

Suppose we want to configure the root account to log in locally quickly, we can do this:

# After pressing Enter, you need to enter the root password once
 [root@host ~]# mysql_config_editor set --login-path=root -uroot -hlocalhost -p -P3306
  Enter password:

   # After configuration is complete, you can use login-path to log in
 [root@host ~]# mysql --login-path=root
 Welcome to the MySQL monitor. Commands end with ; or \g.
 Your MySQL connection id is 2919
 Server version: 5.7.23-log MySQL Community Server (GPL)

  Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

  Oracle is a registered trademark of Oracle Corporation and/or its
 affiliates. Other names may be trademarks of their respective
 owners.

  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  mysql>

Summarize:

This article mainly introduces several methods of modifying database account passwords, basically covering all scenarios. Here I would also like to remind everyone that it is best to limit database account logins to IP segments, make passwords as complex as possible, and modify them regularly, especially in important environments. You cannot be careless at all. It's the end of the year, and safety is king.

The above are the details of several ways to change MySQL passwords. For more information about changing MySQL passwords, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to change the root user's password in MySQL
  • MySQL 8.0.19 winx64 installation tutorial and change the initial password under Windows 10
  • mysql8.0 forgotten password modification and net command service name invalid problem
  • Ubuntu View and modify mysql login name and password, install phpmyadmin
  • 4 ways to modify MySQL root password (summary)
  • How to allow external network access to mysql and modify mysql account password
  • How to modify the root user password in mysql 8.0.16 winx64 and Linux
  • How to correctly modify the ROOT password in MySql8.0 and above versions
  • Problems with changing password and connecting to Navicat when installing and using MySQL 8.0.16 under Windows 7
  • Solve the problem of changing the password when logging in for the first time after installing MySQL 8.0
  • Summary of how to modify the root password in MySQL 5.7 and MySQL 8.0

<<:  Two solutions for automatically adding 0 to js regular format date and time

>>:  IE6 implements min-width

Recommend

How to migrate sqlite to mysql script

Without further ado, I will post the code for you...

VUE+Canvas realizes the whole process of a simple Gobang game

Preface In terms of layout, Gobang is much simple...

Analysis of the methods of visual structure layout design for children's websites

1. Warm and gentle Related address: http://www.web...

Detailed explanation of keepAlive use cases in Vue

In development, it is often necessary to cache th...

Example code for css3 to achieve scroll bar beautification effect

The specific code is as follows: /*Scroll bar wid...

Use thead, tfoot, and tbody to create a table

Some people use these three tags in a perverted wa...

Vue3 gets the current routing address

Correct answer Using useRouter : // router path: ...

How to install Maven automatically in Linux continuous integration

Unzip the Maven package tar xf apache-maven-3.5.4...

Specific method to delete mysql service

MySQL prompts the following error I went to "...

Implementation of docker-compose deployment of zk+kafka+storm cluster

Cluster Deployment Overview 172.22.12.20 172.22.1...

Ubuntu 16.04 64-bit compatible with 32-bit programs in three steps

Step 1: Confirm the architecture of your system d...

VMwarea virtual machine installation win7 operating system tutorial diagram

The installation process of VMwarea will not be d...

How to strike a balance between ease of use and security in the login interface

Whether you are a web designer or a UI designer, ...

JS+Canvas draws a lucky draw wheel

This article shares the specific code of JS+Canva...