MySQL: create database and grant usage to user on it

To create a database and grant usage to user on it, you must to be root on mysql server:

$ mysql -u root -p

First, create a new database:

mysql> create database my_useful_db;

Then, grant usage to your user on this new database:

mysql> grant usage on *.* to my_favorite_user@localhost identified by 'my_very_secret_password';

After, grant all privileges to your user on this database:

mysql> grant all privileges on my_useful_db.* to my_favorite_user@localhost ;

Finally, flush privileges to apply/reload changes:

mysql> FLUSH PRIVILEGES;

Now, you can try to connect to this database with your username and password:

$ mysql -u my_favorite_user my_useful_db -p
Password:
Your MySQL connection id is 12
Server version: 5.0.38-Ubuntu_0ubuntu1-log Ubuntu 7.04 distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

 

That’s it!

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *