Home  >  Article  >  Database  >  How to set root password in mysql

How to set root password in mysql

PHPz
PHPzOriginal
2023-04-20 10:10:443219browse

As the most commonly used database management system, the installation and configuration of MySQL is a skill that we must master when doing back-end development, especially the setting of the MySQL root password.

The MySQL root account is the highest authority account of the MySQL database and has functions such as creating and deleting databases, user accounts, and modifying permissions. For MySQL database security, the password setting of the root account is crucial. Below we will explain in detail how to set the MySQL root password.

  1. First, log in to MySQL

Enter the following command in the terminal to log in to MySQL:

mysql -u root

If you set a password during MySQL installation, You will need to enter a password. If you are logging in for the first time or if you have not set a password, leave it blank and press Enter to enter.

  1. Modify MySQL root password

After entering the MySQL client, we need to change the password of the root account. Enter the following command in the terminal:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

Among them, replace "newpassword" with the password you want to set.

  1. Confirm that the password change is successful

After changing the password, we need to confirm whether the password change is successful. Enter the following command in the terminal:

SELECT User, Host, Password FROM mysql.user;

Check the output results to see whether the password of the root account is the new password we just set.

  1. Change the configuration file

In addition to changing the password in the MySQL client, we also need to modify the MySQL configuration file. Enter the following command in the terminal:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Find the following content in the file:

#bind-address = 127.0.0.1

Modify it to:

bind-address = 0.0.0.0

Save the file and exit. The purpose of this step is to make the MySQL server accessible on other computers than the local one.

  1. Restart MySQL

Enter the following command in the terminal to restart the MySQL server:

sudo service mysql restart

After restarting, we can use the root account on other computers To access the MySQL database, use the new password you just set.

Summary

In using MySQL for back-end development, password setting for the root account is an essential process and a very important security measure. Through the above introduction, I believe you have mastered how to set the MySQL root password. I hope this article can be helpful to everyone.

The above is the detailed content of How to set root password 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