CS216-create-users-in-mysql

advertisement
Creating a MySQL user
There are two ways you can do this.


Using CREATE USER and/or GRANT commands or
Inserting a new record into the mysql.user table
First let's see how to use the CREATE USER command. The syntax is the following:
CREATE USER user [IDENTIFIED BY [PASSWORD] 'password']
Here is an example:
1.
2.
3.
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1';
Now if you check the mysql.user table you can find a new record in it. Notice that all priviliges are set to No so this user can
do nothing in the DB. To add some preiviliges we can use the GRANT command as follows:
1.
2.
3.
GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'user1'@'localhost';
Here we have added only the most important privileges to the user. With the setting above this user is not able to create
tables.
To add all priviliges to the user you don't have to list all of them but you can use the ALL shortcut as follows:
1.
2.
3.
GRANT ALL ON *.* TO 'user1'@'localhost';
You can create a new MySQL user in one step as well using again the GRANT command with a small extension as here:
1.
2.
3.
GRANT ALL ON *.* TO 'user2'@'localhost' IDENTIFIED BY 'pass1';
The above examples used dedicated commands, but sometimes you maybe want to add a new MySQL user via directly
editing the mysql.user table. In this case you just insert a new record into the table with a normal INSERT command:
1.
2.
3.
4.
INSERT INTO user (Host,User,Password)
VALUES('localhost','user3',PASSWORD('pass3'));
Or you can add some privileges as well in a form like this:
1.
2.
3.
4.
INSERT INTO user (Host,User,Password,Select_priv,Insert_priv)
VALUES('localhost','user4',PASSWORD('pass3'),'Y','Y');
Download