To create a user account from a MySQL server instance, we need to make use of a CREATE USER call to the mysql.user table.

First, fire up MySQL in your terminal and login as an administrator account with:

mysql -u root -p

You will be prompted for your root password before being granted access to the MySQL monitor.

Now to grab a list of available user accounts on the system. Running:

select user,host from mysql.user;

If the user account that you wish to create doesn’t appear in that list, you are good to go. To create a new user account run the following query:

create user 'new-username'@'localhost' identified by 'password-for-new-account';

If you omit the @’localhost’ section, the user account will be generated with a host value of ‘%’. If you omit the identified by section, the user account will be created without a password (which is of course quite insecure).

Of course, this user isn’t linked to any databases yet, but this is simple to rectify with a:

grant all on mydatabase.* to useraccount@localhost

that will grant all rights for the user account to everything held in the mydatabase database. (Note that you generally don’t want to assign all rights, so will scale back as necessary in a real world example.)

Simple and nifty.

Related Link: