MDLog:/sysadmin

The Journal Of A Linux Sysadmin

MySQL Tips: Howto Create a MySQL User

| Comments

Here is a quick tip how to create from mysql cli a new database and a new mysql user that has full privileges on this newly created database:

1
2
3
4
mysql -uroot -p
<enter_mysql_root_pass>
CREATE DATABASE <DB_NAME>;
GRANT ALL PRIVILEGES ON <DB_NAME>.* TO 'my_user'@'localhost' IDENTIFIED BY 'my_password' WITH GRANT OPTION;

For a new mysql user with full privileges on all mysql databases you would use:

1
GRANT ALL PRIVILEGES ON *.* TO 'my_user'@'localhost' IDENTIFIED BY 'my_password' WITH GRANT OPTION;

These users are accessible only from localhost, and for ex. if you want the user to be accessible from any host (not a good idea) you just have to use % instead of localhost, like:

1
GRANT ALL PRIVILEGES ON <DB_NAME>.* TO 'my_user'@'%' IDENTIFIED BY 'my_password' WITH GRANT OPTION;

These are just simple, but frequently used forms of the mysql commands used to create new mysql users, but they can be used for more complex things: check the mysql manual page for full details.

Comments