Create a database and associate it to a user - MySQL

nwaweru

Ndirangu Waweru

Posted on October 17, 2022

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!

💖 💪 🙅 🚩
nwaweru
Ndirangu Waweru

Posted on October 17, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related