How to Create Database, User, and Access to MySQL

Dogukan Ulu
2 min readAug 28, 2023

--

In this article, I will explain how to access to our MySQL server using mysql via terminal. We should have mysql command installed and defined as binary so that we can run these commands everywhere.

Once we install MySQL on our local environment, the first thing is to connect to our MySQL instance with the root user. We should run the below command.

$ mysql -u root -p

After accessing the instance, we should first create the database:

CREATE DATABASE <database_name>;

Once the database is created, we can create our user with the desired password:

CREATE USER '<user_name>'@'localhost' IDENTIFIED BY '<password>';

In the end, we should give all the privileges to our user on the database we created:

GRANT ALL PRIVILEGES ON <database_name>.* TO '<user_name>'@'localhost';

After granting privileges, you need to flush the privileges so that the changes take effect immediately:

FLUSH PRIVILEGES;

Since we created a new database and user with all privileges, we can now exit the current session and connect with those.

mysql -u <user_name> -p

We are going to be prompted to password. Once we enter our password <password>, we will be able to access the new database with the new user.

I hope it helps, thanks for reading :)

You may reach out via Linkedin and GitHub, all comments are appreciated 🕺

--

--