MySQL Tips: Howto create a MySQL user

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:

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:

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:

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 powered by Disqus