Brought to you by molecularsciences.org.
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
This publication may not be redistributed without this notice.

Creating New User

In mysql, there are many ways to create a user. Let's begin my logging in as root

mysql -u root -p

The following command adds a user to MySQL database.

CREATE USER 'me'@'localhost' IDENTIFIED BY 'mypass';

To check if a user has been added, check mysql.user table.

use mysql;
select * from user;

You may have noticed that all privileges are set to no so the new user can pretty much do nothing. This leads us to your next question. How do I grant access to my new user? To grant privileges, we use the GRANT command as follows:

GRANT SELECT, INSERT, UPDATE, DELETE ON mydatabase.* TO 'me'@'localhost';

Grants privileges to select, insert, update, and delete all tables of the database mydatabase.

GRANT ALL ON mydatabase.* TO 'me'@'localhost';

Grants all privileges on the database mydatabase.

GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'me'@'localhost';

Grants privileges to select, insert, update, and delete on all databases.

There are several ways to add a user.

INSERT INTO user(Host,User,Password)

VALUES ('localhost','me',PASSWORD('mypass'));

You can also grant privileges while creating a user.

INSERT INTO user(Host,User,Password,Select_priv,Insert_priv,Update_priv)

VALUES ('localhost','me',PASSWORD('mypass'),'Y','Y','Y');