DEV Community

Ndirangu Waweru
Ndirangu Waweru

Posted on

Create a database and associate it to a user - MySQL

Login to your MySQL root account (or any other account with admin privileges):

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Create the database you want your new user to be associated with:

CREATE DATABASE example_database;
Enter fullscreen mode Exit fullscreen mode

Create the new user:

CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'password';
Enter fullscreen mode Exit fullscreen mode

Grant the new user access to the newly created database:

GRANT ALL ON example_database.* TO 'example_user'@'localhost';
Enter fullscreen mode Exit fullscreen mode

Login to MySQL with your new user:

mysql -u example_user -p
Enter fullscreen mode Exit fullscreen mode

Check the databases under the new user:

show databases;
Enter fullscreen mode Exit fullscreen mode

Build something awesome!

Top comments (0)