STEP 1: We need to login the MySQL prompt using below command because all operation will be executed in MySQL prompt.
mysql -u root -p
and need to login with MySQL root password.
STEP 2: CREATE NEW USER
We can create new MySQL user with the following command:
mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
where:
user – the name of the MySQL user which will be created
password – the password which we want to assign for that user
All MySQL commands are ends with a semicolon (;).
STEP 3: GRANT PERMISSIONS FOR A USER
The next thing that we have to assign the grant privileges for that user is able to access the MySQL and work with databases.
we grant all privileges to the MySQL user and for all database tables related to the database with name "database".
mysql> GRANT ALL PRIVILEGES ON database.table TO 'user'@'localhost';
where:
database – the name of the MySQL database to which we grant access
table – the name of the database table to which we grant access
We are allowed to use the asterisk wildcard symbol (*) when we want to grant access to all databases/tables:
mysql> GRANT ALL PRIVILEGES ON database.* TO ‘user’@’localhost’;
or
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘user’@’localhost’;
Here is a sample syntax where only two privileges are granted for the user:
mysql> GRANT SELECT, INSERT, DELETE ON database.* TO 'user'@'localhost';
Here is a list of the MySQL privileges which are most commonly used: