How to create MySQL user with all privileges?

MySQL database user creation is needed very frequently. If we work on PHP, WordPress based applications then this user creation directly on MySQL is needed in some cases. Lets consider a use case where we need a MySQL database user with full privileges.

To create a MySQL database user we need to login to database server using a user that has the access to create new user. Let consider we logged to MYSQL server using root user. Let say we want to create a user called: demo and password: secret

MySQL user creation

Now run the following commands on MYSQL console:

mysql> CREATE USER 'demo'@'localhost' IDENTIFIED BY 'secret';

mysql> GRANT ALL PRIVILEGES ON * . * TO 'demo'@'localhost';

mysql> FLUSH PRIVILEGES;

Explanation of the above commands:

CREATE USER: This is the command to create user.

IDENTIFIED BY ‘secret’: This is used to set a password for that user.

GRANT: This command is used to provide access rights to databases, tables, etc.

TO ‘demo’@’localhost’: ‘demo’ is the user being created. localhost is the hostname of MySQL server. In our case it was localhost but it could something else for remote MySQL server.