What is the MySQL user creation script?


First, create a user and password using CREATE command. The syntax is as follows.

CREATE USER 'yourUserName'@'localhost' IDENTIFIED BY 'yourPassword';

The syntax to give all privileges of the specific database to the user is as follows.

GRANT ALL PRIVILEGES ON yourDatabaseName . * TO 'yourUserName'@'localhost';

Now you can implement the above syntaxes to create a user and grant all privileges.

Step 1 − Create a user

The query is as follows.

mysql> create user 'Adam Smith'@'localhost' IDENTIFIED BY 'Adam123456';
Query OK, 0 rows affected (0.29 sec)

Step 2 − Grant all privileges to the user.

The query is as follows.

mysql> GRANT ALL PRIVILEGES ON test . * TO 'Adam Smith'@'localhost';
Query OK, 0 rows affected (0.19 sec)

To cross check the user is created or not, you can use the MySQL.user table. The query is as follows.

mysql> select user,host from MySQL.user;

The following is the output.

+------------------+-----------+
| user             | host      |
+------------------+-----------+
| Manish           | %         |
| User2            | %         |
| mysql.infoschema | %         |
| mysql.session    | %         |
| mysql.sys        | %         |
| root             | %         |
| Adam Smith       | localhost |
| User1            | localhost |
| am               | localhost |
+------------------+-----------+
9 rows in set (0.00 sec)

Look at the above sample output, the user Adam Smith is present in the MySQL.user table.

Updated on: 29-Jun-2020

760 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements