Describe the process of creating a new user in MySQL.

Creating a new user in MySQL involves several steps, including connecting to the MySQL server, creating the user account, granting privileges, and optionally specifying host restrictions. Here's a detailed technical explanation of the process:

Exit MySQL Shell: Once you have created the user and granted privileges, you can exit the MySQL shell by typing:


Apply Changes: After granting privileges, apply the changes by executing the FLUSH PRIVILEGES statement to reload the grant tables and refresh the privileges.


Grant Privileges: After creating the user, you need to grant appropriate privileges to the user account. Use the GRANT statement to specify the privileges and the database or databases to which they apply. Common privileges include SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, etc.

GRANT ALL PRIVILEGES ON database_name.* TO 'new_user'@'localhost';

Replace database_name with the name of the database to which you want to grant access. You can also use *.* to grant access to all databases.

Create User Account: Use the CREATE USER statement to create a new user account. You specify the username and the host from which the user is allowed to connect. If no host is specified, the default is 'localhost', meaning the user can only connect from the local machine.

CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';

Access MySQL Database: Once connected, you enter into the MySQL shell where you can execute SQL commands. By default, you are in the mysql system database, where user account information is stored.

USE mysql;

Connect to MySQL Server: First, establish a connection to the MySQL server using a MySQL client tool or command-line interface. This typically involves providing the necessary credentials such as username and password of an existing MySQL user with administrative privileges (e.g., the root user).

mysql -u root -p