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.
MySQL provides a secure initialization toolmysql_secure_installation
, which can help us set the root password and other security configurations.
mysql -u root -p
sudo mysql_secure_installation
If you do not use the security initialization tool, you can also change the root password directly in MySQL.
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
FLUSH PRIVILEGES;
You can also add the root password directly in the MySQL configuration file, This way MySQL will automatically load the password when it starts.
/etc/mysql/my.cnf
or /etc/my.cnf
): sudo nano /etc/mysql/my.cnf
[mysqld]
section: [mysqld] skip-grant-tables
sudo systemctl restart mysql
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
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!