Home  >  Article  >  Database  >  mysql set root

mysql set root

王林
王林Original
2023-05-23 11:51:374045browse

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.

  1. Install 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.

  1. Connecting to MySQL

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.

  1. Change the root user password

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:

  • Connect to the MySQL server, open a terminal or command line window, and enter the following command:
mysql -u root -p
  • Enter root The user's old password to enter MySQL command line mode.
  • Enter the following SQL statement on the command line to update the root user password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Among them, new_password is the new password. You can change it as needed.

  • Enter the following command to refresh permissions for the changes to take effect:
FLUSH PRIVILEGES;
  1. Create a new MySQL user

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:

  • Connect to the MySQL server, enter the MySQL command line mode, enter the following SQL statement to create a new user "test", and set the password to "test_pwd".
CREATE USER 'test'@'localhost' IDENTIFIED BY 'test_pwd';
  • Grant this user SELECT and INSERT permissions to a specific database. For example, grant the user SELECT and INSERT permissions in the test_db database.
GRANT SELECT, INSERT ON test_db.* TO 'test'@'localhost';
  • Refresh permissions to make changes take effect
FLUSH PRIVILEGES;
  1. Delete the old MySQL user

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".

  • Connect to the MySQL server, enter the MySQL command line mode, and enter the following SQL statement to delete the existing user "old_user".
DROP USER 'old_user'@'localhost';
  • Refresh permissions for changes to take effect.
FLUSH PRIVILEGES;
  1. Modify MySQL user permissions

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.

  • Authorize user "test" to have SELECT and INSERT permissions on the test_db database.
GRANT SELECT, INSERT ON test_db.* TO 'test'@'localhost';
  • Revoke the INSERT permission of user "test" on the test_db database.
REVOKE INSERT ON test_db.* FROM 'test'@'localhost';
  • Refresh permissions for changes to take effect.
FLUSH PRIVILEGES;
  1. Summary

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!

Statement:
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
Previous article:mysql start closeNext article:mysql start close