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

Mobile front-end adaptation solution (summary)

I searched online and found that many interviews ...

How to create a virtual environment using virtualenv under Windows (two ways)

Operating system: windowns10_x64 Python version: ...

Sharing of web color contrast and harmony techniques

Color contrast and harmony In contrasting conditi...

A permanent solution to MYSQL's inability to recognize Chinese

In most cases, MySQL does not support Chinese whe...

Analysis and solution of data loss during Vue component value transfer

Preface In the previous article Two data types in...

MySQL Server 8.0.3 Installation and Configuration Methods Graphic Tutorial

This document records the installation and config...

A detailed explanation of the subtle differences between Readonly and Disabled

Readonly and Disabled both prevent users from chan...

Use crontab to run the script of executing jar program regularly in centOS6

1. Write a simple Java program public class tests...

How to use port 80 in Tomcat under Linux system

Application Scenario In many cases, we install so...

Detailed steps for developing WeChat mini-programs using Typescript

We don't need to elaborate too much on the ad...

js to achieve cool fireworks effect

This article shares the specific code for using j...

Specific use of Linux man command

01. Command Overview Linux provides a rich help m...

How to use CSS counters to beautify ordered lists of numbers

In web design, it is very important to use an org...

MySQL 8.0.16 Win10 zip version installation and configuration graphic tutorial

This article shares with you the installation and...

HTML table markup tutorial (28): cell border color attribute BORDERCOLOR

To beautify the table, you can set different bord...