Adding MySQL Users / Setting User Privileges

The syntax for adding a user is:

mysql> GRANT [privs] ON [db] TO [user]@[host] IDENTIFIED BY [pass];

e.g.
mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'bob'@'localhost' IDENTIFIED BY '3kuh3Ok';

Would grant all privileges on the testdb database to the local user bob, with password 3kuh3Ok.

A few things to note:
* '%' is used to indicate any remote host... e.g. bob@'%' is bob at any host (other than localhost)
* Users consist of both a username AND a host, so bob@localhost, bob@192.168.1.23, and bob@'bob.com' are all distinct users.

Grant statements can also change the permissions on an existing user. For example:

mysql> GRANT ALL PRIVILEGES ON bobs_db.* TO bob@'%' IDENTIFIED BY 'a5u19kb';
mysql> GRANT SELECT,INSERT ON blog_db.omgdata TO bob@'%';

Would create a user bob@'%' (any remote host) with password a5u19kb, and grant the user full privileges on database 'bobs_db', and then ALSO grant that user SELECT and INSERT privileges on the omgdata table of the blog_db database.

To create a user with no db access, you can use:

mysql> GRANT USAGE ON *.* TO user@host IDENTIFIED BY 'password';

Submitted by jkelly on Sat, 2006-08-12 03:22. categories [ | ] login or register to post comments