Home  >  Article  >  Database  >  Detailed explanation of how to set a password for root in mysql

Detailed explanation of how to set a password for root in mysql

PHPz
PHPzOriginal
2023-04-20 10:12:372193browse

MySQL is an open source relational database management system that is widely used in various fields. In MySQL, root is a default superuser account, which has the highest authority and highest authority control. Generally speaking, during installation, there is no root account password by default, and you need to manually set the password to ensure the security of the server. So, how to set a password for the MySQL root user? This article will explain in detail how to set a password for root in MySQL.

  1. Login to MySQL

After installing MySQL, we need to log in to MySQL and log in as the root user. In Windows, we can use the MySQL Command Line Client to log in; in Linux or macOS, we can use the terminal to log in. Enter the following command:

mysql -u root -p

Enter the command line mode of the MySQL administrator, where the -u parameter is used to specify User, in this case the root user.

If you have not set a password, just press Enter.

  1. Create database user and authorize

When we get a new MySQL database, root is usually a superuser without a password. In order to improve data security, we can control access by creating new users and giving them appropriate permissions. Below are the steps to create a new user and authorization.

Take creating a user with the user name exampleUser and the password examplePassword as an example.

First, create a new user using the following command:

CREATE USER 'exampleUser'@'localhost';

whereexampleUser is the username of the new user, localhost means that the user can only access locally. If you need to log in remotely, you need to replace localhost with the user's IP address or the wildcard character %.

Then, we need to authorize the new user to have access rights to the database.

GRANT ALL PRIVILEGES ON *.* TO 'exampleUser'@'localhost' IDENTIFIED BY 'examplePassword';

In this line of command, we add all libraries and tables permissions are granted to the new user. IDENTIFIED BY represents the user password.

  1. Modify the password of the root user

In order to enhance the security of the MySQL database, we need to set the password of the root user.

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');

This will change the root user's password. PASSWORD Ensures that the supplied parameters are encrypted.

Note:

  • Please be sure to remember the password of the root user, otherwise you will not be able to log in to MySQL;
  • You need to ensure that the user name or IP address used is the same as The authorizations must be consistent, otherwise the database may not be accessible.

This article explains in detail how to set the password for the root user in MySQL. I hope it will be helpful to beginners. A good habit to strengthen database security is to set passwords promptly and assign appropriate permissions to users to securely access and protect data from theft.

The above is the detailed content of Detailed explanation of how to set a password for root in mysql. 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