MySQL is an open source relational database management system that supports multiple operating systems, has good performance and reliability, and is widely used in large-scale enterprise-level applications and websites. When using MySQL, the root user is the highest authority user and has full control over MySQL. This article will introduce how to set up the root user in MySQL.
If MySQL is not already installed, please download and install MySQL from the MySQL official website (https://www.mysql.com/) first. During the installation process, please set the password for the root account.
Before using MySQL, we need to connect to the MySQL server first. You can connect to MySQL through the command line, or you can use Graphical User Interface (GUI) tools, such as MySQL Workbench, etc.
Connect to MySQL through the command line:
Open a terminal or command line window and enter the following command to connect to MySQL:
mysql -u root -p
Prompts for the root user password. After entering the correct password, You can successfully connect to the MySQL server.
Connect to MySQL through MySQL Workbench:
Open MySQL Workbench, click the "New Connection" button, enter the host name, port number, user name and password of the MySQL server, and click "Test Connection" button to test the connection and then save the connection.
In MySQL, the root user has very high permissions, so we should change its password to improve security. We can change the root user password through the following steps:
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
Among them, new_password is the new password. You can change it as needed.
FLUSH PRIVILEGES;
In MySQL , we can create a new user through the CREATE USER command, and then grant specific permissions to the user. For example, we can create a user named "test" with SELECT and INSERT permissions. The following is the creation and authorization process:
CREATE USER 'test'@'localhost' IDENTIFIED BY 'test_pwd';
GRANT SELECT, INSERT ON test_db.* TO 'test'@'localhost';
FLUSH PRIVILEGES;
In MySQL, We can use the DROP USER command to delete existing users and their permissions. For example, we can remove all permissions from a user named "old_user".
DROP USER 'old_user'@'localhost';
FLUSH PRIVILEGES;
In MySQL, we can use the GRANT and REVOKE commands to grant or revoke user-specific permissions. For example, we can authorize user "test" to have SELECT and INSERT permissions on the test_db database, and then revoke the INSERT permissions.
GRANT SELECT, INSERT ON test_db.* TO 'test'@'localhost';
REVOKE INSERT ON test_db.* FROM 'test'@'localhost';
FLUSH PRIVILEGES;
Through the above steps, we can successfully set up the root user in MySQL, create new users, modify user passwords, and authorize them and revoke different permissions. At the same time, we should also pay attention to MySQL security issues and take necessary security measures, such as using passwords to protect accounts, using IP restrictions, using data encryption, etc.
The above is the detailed content of mysql set root. For more information, please follow other related articles on the PHP Chinese website!