MySQL CREATE USER Statement Examples

Let’s look at some examples of the CREATE USER command in MySQL, and understand it’s working.

MySQL Create Single User Example

In this example, we will create a new user “gfguser1” that connects to the MySQL database server from the localhost with the password “abcd”.

Query: 

CREATE USER gfguser1@localhost IDENTIFIED BY 'abcd';

Note: The create user statement only creates a new user, it does not grant any permissions to the user.  

MySQL Create Multiple User Example

In this example, we will create more than one new user that connects to the MySQL database server from the localhost.

Queries: 

CREATE USER
'gfguser2'@'localhost' IDENTIFIED BY 'efgh',
'gfguser3'@'localhost' IDENTIFIED BY 'uvxy';

The above code creates two new users with username “gfguser2” and “gfguser3” with passwords “efgh” and “uvxy” respectively. 

Allowing a user account to connect from any host example

To allow a user account to connect from any host, the percentage (%) wildcard is used in the following way.

Query:

CREATE USER gfguser1@'%'
IDENTIFIED BY 'abcd'; 

To allow the user account to connect to the database server from any subdomain of the “mysqltutorial.org” host, then the percentage wildcard % should be used as follows: 

Query: 

CREATE USER gfguser@'%.mysqltutorial.org'
IDENTIFIED by 'abcd'

MySQL CREATE USER Statement

MySQL CREATE USER Statement allows users to create a new USER in the MySQL database.

Similar Reads

CREATE USER in MySQL

The CREATE USER statement in SQL is used to create a new user and a password to access that user....

Syntax

The syntax for the CREATE USER statement in MySQL is:...

MySQL CREATE USER Statement Examples

Let’s look at some examples of the CREATE USER command in MySQL, and understand it’s working....

View Permissions of an User Account

The SHOW GRANTS statement is used to view the permissions of a user account....