Home  >  Article  >  Database  >  How to change mysql database password

How to change mysql database password

PHPz
PHPzOriginal
2023-04-19 14:12:184356browse

When managing a database, security is crucial. An effective method is to change the database password regularly to maintain security. MySQL is a popular open source database management system that allows you to change passwords in many ways.

Here are the steps on how to change the MySQL database password:

Step 1: Log in to MySQL

First, connect to the MySQL server and log in to it with administrator rights. This can be done using the command line or any MySQL client such as phpMyAdmin. Use the following command to connect to MySQL on a Linux or Mac OS X system:

mysql -u root -p

This will prompt for the MySQL administrator password.

Step 2: Select the database

You need to know the database name to change the database password. You can use the following command to list all existing MySQL databases:

show databases;

Select the database whose password you want to change. Suppose we want to change the password of database "mydb". Use the following command to select the database:

use mydb;

Step 3: Change Password

Now you can use the following command to change the user password for the currently selected database:

alter user 'username'@'localhost' identified by 'newpassword';

Note: For versions prior to MySQL 5.7, use the SET PASSWORD statement:

set password for 'username'@'localhost' = password('newpassword');

In the above command, "username" is the username whose password you want to change, and "localhost" is the IP address of the local host. You can also use "%" to represent all IP addresses. The new password is "newpassword".

For example, if you want to change the password of user "john" in database "mydb" to "newpassword", use the following command:

alter user 'john'@'localhost' identified by 'newpassword'; -- for MySQL 5.7 and after versions
set password for 'john'@'localhost' = password('newpassword'); -- for MySQL 5.7 and before versions

Step 4: Reload permissions

Once the password is changed, the permission table of the database needs to be reloaded for the change to take effect. The permission table can be reloaded using the following command:

flush privileges;

At this point, the MySQL database password has been successfully changed.

Summary

MySQL is a powerful open source database management system, but it requires appropriate protection to prevent unauthorized access. Changing your database password is a simple yet effective way to increase security. This article describes how to use the command line to change the MySQL database password. Using this method, you can keep your database secure and secure from unauthorized users.

The above is the detailed content of How to change mysql database password. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn