Add a password to the root user who logs in for the first time

To set a password for the root user in MySQL, you can follow these steps:

1. Log in to the MySQL server:

sudo mysql -u root

2. Run the following command to set a password for the root user:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';

This will set the password for the root user to "your_password". Please replace "your_password" with the actual password you want to set.

3. (Optional) If you want to allow the root user to log into the MySQL server from a remote host, you can run the following command:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;

This will allow the root user to connect to the MySQL server from any remote host with administrative privileges.

Be aware that this may reduce the security of the MySQL server, as allowing connections to the MySQL server from remote hosts may make the server vulnerable. If possible, allow only specific IP addresses to connect to the MySQL server.

4. Run the following command to refresh the MySQL privilege table for the changes to take effect:

FLUSH PRIVILEGES;

5. Exit the MySQL client:

exit

You have now set a password for the MySQL root user. Note that setting a password for the root user is an important security measure that can help protect the MySQL server from unauthorized access.

Guess you like

Origin blog.csdn.net/Toml_/article/details/131953126