How to add MySQL root password?
Mar 14, 2024 pm 05:12 PMMySQL 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.
- Open the terminal and enter the following command to log in to the MySQL database:
mysql -u root -p
- Then enter the default password (if this is the first time you log in, you may not have a password).
- Next, execute the following command to start the security initialization tool:
sudo mysql_secure_installation
- 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.
- Log in to MySQL through the terminal:
mysql -u root -p
- Use the following command to modify the password of the root user:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
- Modify After completing the password, refresh the permissions to make the changes take effect:
FLUSH PRIVILEGES;
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.
- Open the MySQL configuration file (usually
/etc/mysql/my.cnf
or/etc/my.cnf
):
sudo nano /etc/mysql/my.cnf
- Add the following under the
[mysqld]
section:
[mysqld] skip-grant-tables
- Save and close the file, then restart the MySQL service:
sudo systemctl restart mysql
- 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
- Change the root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
- 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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging
