Home  >  Article  >  Database  >  mysql user set password

mysql user set password

PHPz
PHPzOriginal
2023-05-23 11:23:07805browse

MySQL is a commonly used database management system used to store and manage large amounts of data. For MySQL users, setting a password is a very important security measure. This article will introduce how to set a password for MySQL users to ensure database security.

  1. Login to MySQL

First, you need to log in to MySQL. You can use the following command to log in to MySQL:

mysql -u root -p

Among them, "-u root" means to log in as the root user, and "-p" means that you need to enter a password.

  1. Create User

Next, you need to create a MySQL user. You can use the following command to create a user:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Where "username" is the username you created, "localhost" means that the user can only log in to MySQL locally, and "password" is the user's password.

If you want users to be able to log in to MySQL remotely, you can replace "localhost" with the user's IP address.

  1. Authorized User

After creating a user, you need to authorize the user to access the database. You can use the following command to authorize users:

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

Where "database_name" is the name of the database you want the user to access, and "*" means the user can access all tables in the database.

  1. Refresh permissions

After completing the authorization operation, you need to refresh MySQL permissions. You can use the following command to refresh permissions:

FLUSH PRIVILEGES;
  1. Change Password

If you need to change the password of the MySQL user, you can use the following command:

ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';

Where "new_password" is the new password you want to set.

Please note that this method can only change the password of the MySQL user, but cannot change the password of the root user. If you need to change the password of the root user, you need to use other methods.

  1. Banning empty passwords

In order to protect the security of your database, it is recommended to prohibit MySQL users from using empty passwords. You can use the following command to prevent users from using empty passwords:

mysql> UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='username' AND Host='localhost';
  1. Delete User

If you no longer need a MySQL user, use the following command to delete the user:

DROP USER 'username'@'localhost';

Summary

Through the above steps, you can set a password for the MySQL user to ensure the security of the database. When creating a new user, it is very important to set a password before authorizing the user to access the database. If you need to change the password, you can also use the corresponding command to do so.

The above is the detailed content of mysql user set 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