Home >Database >Mysql Tutorial >Detailed explanation of how to set a password for root in mysql
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.
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.
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.
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:
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!