MySQL create user if it does not exist?

MySQLMySQLi Database

You can create user if it does not exist with the help of “create user” command. The command will work on MySQL version 5.7.6 and above. The syntax is as follows −

mysql> CREATE USER IF NOT EXISTS 'yourUserName'@'localhost' IDENTIFIED BY 'yourPassword';

Apply the above syntax to create a user if it does not exist. The query is as follows −

mysql> CREATE USER IF NOT EXISTS 'Smith'@'localhost' IDENTIFIED BY 'Smith123456';
Query OK, 0 rows affected (0.29 sec)

To check the new user is created or not, use the below query −

mysql> SELECT User FROM mysql.user;

The following is the output −

+------------------+
| User             |
+------------------+
| John             |
| Mac              |
| Manish           |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
| Smith            |
| am               |
+------------------+
9 rows in set (0.00 sec)

Look at the above output, the use “Smith” created successfully.

raja
Published on 21-Dec-2018 17:26:50
Advertisements