Create MySQL Database via Command Line


Command lines queries are very useful when the database has performance constraints, i.e. the queries executed in command line require less execution time. In this tutorial we will be creating Mysql database in command line.

Step 1- Create a New Database
To create Mysql database from command line, we need our MySQL shell opened and logged in with the privileged user.
Now in the shell, just write create database command followed by the database name. As we are using a database name as sample, the query would be:
CREATE DATABASE sample;
After typing the above line, hit Enter. We will be notified about the row changes made in MySQL admin.

New Database

Step 2- Granting Privileges and creating user
After creating MySQL database in command line, we need to add a user to it providing all privileges.
For that, write Grant all privileges on and specify the database name.
After that, type “.*” and give the desired username followed by @ Localhost and then set its password after the “Identified By” tag.
Thus the query would be:
GRANT ALL PRIVILEGES ON sample.* TO user_sample@localhost IDENTIFIED BY ‘test_user’;

user and privileges

Step 3- Admin Panel
Now let us open the php my admin panel.
On the admin page, click on the “Databases” tag located towards the top; here in the list of databases, our sample database would be listed.
Looking through Databases

Step 4- User
Now let’s check for the user that we created earlier. For that, move to the users tab.
And there in the list, the new user that we created through the command line would appear.
Users Tab

And this is how we can create MySQL database through command line.