Home > Database > Mysql Tutorial > How to add MySQL root password?

How to add MySQL root password?

WBOY
Release: 2024-03-14 17:12:04
Original
658 people have browsed it

MySQL root密码应该如何添加?

MySQL is a popular open source relational database management system commonly used to store and manage data. After installing MySQL, we need to set a root user password to protect the database security. This article will introduce how to add the MySQL root password, as well as specific code examples.

1. Use the MySQL secure initialization tool

MySQL provides a secure initialization toolmysql_secure_installation, which can help us set the root password and other security configurations.

  1. Open the terminal and enter the following command to log in to the MySQL database:
mysql -u root -p
Copy after login
Copy after login
  1. Then enter the default password (if this is the first time you log in, you may not have a password).
  2. Next, execute the following command to start the security initialization tool:
sudo mysql_secure_installation
Copy after login
  1. Follow the prompts, including setting the root password, deleting anonymous users, disabling remote root login, etc.

2. Change the password directly in MySQL

If you do not use the security initialization tool, you can also change the root password directly in MySQL.

  1. Log in to MySQL through the terminal:
mysql -u root -p
Copy after login
Copy after login
  1. Use the following command to modify the password of the root user:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
Copy after login
Copy after login
  1. Modify After completing the password, refresh the permissions to make the changes take effect:
FLUSH PRIVILEGES;
Copy after login

3. Add the root password in the MySQL configuration file

You can also add the root password directly in the MySQL configuration file, This way MySQL will automatically load the password when it starts.

  1. Open the MySQL configuration file (usually /etc/mysql/my.cnf or /etc/my.cnf):
sudo nano /etc/mysql/my.cnf
Copy after login
  1. Add the following under the [mysqld] section:
[mysqld]
skip-grant-tables
Copy after login
  1. Save and close the file, then restart the MySQL service:
sudo systemctl restart mysql
Copy after login
  1. Use the following command to log in to MySQL. At this time, you can directly access it as the root user without a password:
mysql -u root
Copy after login
  1. Change the root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
Copy after login
Copy after login
  1. Delete the content added in the configuration file, save and close the file, and restart the MySQL service.

Summary

With the above method, we can easily add the MySQL root password to improve the security of the database. Whether you use a secure initialization tool, change the password directly in MySQL, or add a password in the configuration file, you can effectively protect the database from unauthorized access. Of course, when setting a password, you must ensure that the password is secure and complex to avoid being cracked.

The above is the detailed content of How to add MySQL root password?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template