How to Create a MySQL Database Using the Command Line Interface (CLI)

Create a MySQL Database in the Terminal

If you need to create a database for your website there are multiple ways to do it. In this guide, you can learn how to create a MySQL database using the command line interface (CLI).

Don’t have time to read the article? Watch our walk-through video.

IMPORTANT: This guide is intended for use with our Cloud Server Hosting and other hosting plans that do not include cPanel. If your server includes cPanel, you should follow our guide on How to Create a MySQL Database Using the cPanel API.

Create a MySQL Database Using CLI

If you are running a Linux server with MySQL but no cPanel, you can simply use the terminal to create a MySQL database, database user, and password, as well as, assign all privileges to the user for the database.

  1. SSH into your server as root.
  2. Log into MySQL as root:
    mysql -u root
  3. Create a new database user:
    GRANT ALL PRIVILEGES ON *.* TO 'db_user'@'localhost' IDENTIFIED BY '[email protected]$w0rd123!';
  4. Log out of MySQL by typing: \q.
  5. Log in as the new database user you just created:
    mysql -u db_user -p

    Then, type the new database user’s password and press Enter.

  6. Create a new database:
    CREATE DATABASE db_name;
Create a Database in the Command Line Interface (CLI)
Creating a MySQL user and database in the command line interface (CLI)

Congratulations! Now, you know how to create a MySQL database from the command line. Want to learn more about database management? Check out official MySQL documentation or our guide on How to Import MySQL Databases in Command Line.

If you don’t need cPanel, don't pay for it. Only pay for what you need with our Cloud VPS solutions

check markCentOS, Debian, or Ubuntu    check markNo bloatware    check markSSH Key management made easy

JB
John-Paul Briones Content Writer II

John-Paul is an Electronics Engineer that spent most of his career in IT. He has been a Technical Writer for InMotion since 2013.

More Articles by John-Paul

Was this article helpful? Let us know!