June 27th, 2025 at 8:33 pm
How to Create a Database in MySQL (Step-by-Step for Command Line Users)
7 minutes reading

Working with MySQL through the command line might seem complex at first, especially if you’re more comfortable with visual tools like cPanel. However, gaining this skill can give you more control over your site, help you troubleshoot more efficiently, and make advanced tasks much easier down the road.
In this guide, we’ll show you exactly how to create database in MySQL command line—step by step. Whether you’re setting up a new site, preparing a development environment, or managing a growing application, knowing how to create and manage databases directly from the terminal is a valuable asset.
This tutorial covers everything you need to get started, from basic syntax to naming best practices and even how the command line compares to more familiar GUI-based options. If you’re ready to move beyond the basics and take a more hands-on approach to database management, this is the place to begin.
Why Use Command Line to Create Database in MySQL
There are many reasons why someone might need to create a MySQL database from the command line, and most of them come down to flexibility and control.
For example, if you’re working on a VPS or cloud server without a graphical interface, the terminal is often your only option. Developers and agencies often use the command line to automate tasks, streamline deployments, or efficiently manage multiple databases. Even small business owners who run their sites on more advanced hosting setups sometimes need to step beyond cPanel when troubleshooting or setting up a staging environment.

Creating a database from the terminal is also a great way to gain a deeper understanding of how your site’s backend works. It’s a skill that can help you move faster, solve problems independently, and manage your environment with fewer limitations.
Whether you’re managing a custom application, preparing a WordPress site for migration, or simply want to reduce your reliance on visual interfaces, the command line gives you direct access to one of the most important parts of your hosting setup – your data.
How to Create Database In MySQL Command Line
Creating a database in MySQL using the command line is one of the most straightforward but essential tasks you’ll perform as a site owner or developer.
To create a database in MySQL from the command line, you first need to know how to open the terminal or command prompt, log in to MySQL, and execute a simple SQL command.
We’ll guide you through this step by step across all major operating systems.
Step 1: Open the Command Line Interface
On Windows
- Click Start, type cmd, and press Enter to open Command Prompt.
- Alternatively, search for PowerShell and open it.
- If MySQL is installed and added to your system’s PATH, you can type mysql -u root -p directly.
- If not, navigate to your MySQL bin folder:
cd "C:Program FilesMySQLMySQL Server 8.0bin"
8.0 is the version of your MySQL, so you need to change it to your version.
On macOS
- Press Command + Space, type terminal, and hit Enter.
- Type the following to log in to MySQL:
mysql -u root -p
On Linux
- Press Ctrl + Alt + T to open the terminal.
- Log into MySQL using:
mysql -u root -p
Step 2: Log in to MySQL
mysql -u root -p
Once your terminal is open, type:
This command:
- Connects you as the MySQL root user (you can replace root with any other MySQL user).
- Prompts you to enter the MySQL user’s password.
After entering the password, you’ll see the MySQL prompt:
mysql>
You’re now inside the MySQL command environment.
Step 3: Use the CREATE DATABASE Command
At the mysql> prompt, run the following:
CREATE DATABASE db_name;
What this does:
- CREATE DATABASE: tells MySQL to create a new database.
- db_name is the database name. You can switch it to any name, but make sure to remember it.
- ; ends the SQL command.
If successful, you’ll see:
Query OK, 1 row affected
This confirms the database has been created.
Step 4: Verify the Database Exists
To check your new database was created correctly, use:
SHOW DATABASES;
the Command line will show you a result similar to this one
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| store_db |
+--------------------+
As you can see, the new database db_name is in the list.
You’ve now successfully connected to MySQL, created a new database, and confirmed it exists—all from the command line. Next, we’ll look at how to choose proper names and avoid common naming issues.
Best Practices When You Create Database in MySQL
Database names might seem like a small detail, but poor naming can lead to frustrating errors, inconsistent behavior across operating systems, and headaches when managing multiple sites. In our experience, following a few simple best practices can help you avoid common mistakes and maintain a clean and reliable environment.

Stick to Letters, Numbers, and Underscores
A database named blog_data_2025 is clean, easily findable, and will not cause any trouble with other commands. However, if you go for spaces, special characters like @,#,!,”, and worse of all “–“, things can go sideways fast.
For example, Hyphens (–) are interpreted as minus signs in MySQL. If you use one, MySQL may throw an error or misread the database name as part of a calculation. Special characters like @ or # can also cause unexpected issues with the syntax. So, database names like blog-data@client#125-2025 is out of the question.
No Capital Letters
MySQL treats database names differently on different systems. Linux, for example, is case-sensitive, and BlogDB is completely different than blogdb.
And while Windows and macOS are not case-sensitive, if you migrate your blog from a macOS to a Linux server, you will find yourself in deep trouble.
So, just stick to lowercase naming. blog_data_2025 is good enough.
Be Short, Clear, and Descriptive
This is self-explanatory, but while you’re at it, ensure that you’re not using abbreviations, vague names, or names that only make sense in the present day.
blog_data_2025 gives far more information than db_123 or side_project_24.
Ensure your name accurately reflects the database’s purpose to prevent losing or accidentally deleting information.
Don’t Use Reserved Words
MySQL has a long list of reserved words like user, select, order, or database. Using them as database names will most likely end up as a syntax error.
You may not get an error immediately if you name your database “user,” but you’re walking into a minefield. For example, MySQL has a system table called mysql.user, which stores all user account information (usernames, passwords, privileges, etc.).
If you create a database named user, some scripts or tools might confuse it with this internal table. You could unintentionally run queries in the wrong context. This is a major problem if you’re using automation or backup scripts.
In the worst-case scenario, you can overwrite or corrupt your user account data, locking yourself out or compromising security.
Use Environment or Project Identifiers
If you’re managing multiple versions of a site or app (development, staging, production), include that in the name. Environment tags help prevent mistakes, such as editing a live production database when you are meant to work in staging.
Keep Names Way Under 64 Characters
While MySQL allows up to 64 characters, long names are harder to type, prone to typos, and may not display well in some interfaces. Plus, have some mercy on yourself. You will have to type it every time you need to manage your database. So, make it easier for yourself.
In short, a well-named database is easier to maintain, safer to manage, and less likely to cause issues during development, migration, or deployment.
Command Line vs cPanel. Which one is better?
Depending on your experience level and needs, you can actually go for the far easier option – using cPanel. The graphic interface allows even complete beginners to create and manage their databases with relative ease. The biggest difference is that with cPanel, you need no code.
The command-line interface (CLI), on the other hand, is the go-to method for developers, power users, or anyone working on VPS, cloud, or dedicated servers where a control panel isn’t available. It offers more precision, faster execution, and full control, especially when automating tasks or managing multiple databases.
Who uses cPanel
- Beginners who prefer a guided interface
- Users who only need to create a few databases occasionally
- Website owners managing a single site from a control panel
If that sounds like your workflow, check out our guide on how to create MySQL database via cPanel.
Who uses CLI
- Users working in remote or headless server environments
- Developers setting up scripts or automated workflows
- Anyone who wants deeper access to MySQL features
Which One Should You Learn?
The answer is both, of course. No matter if you can comfortably rely on cPanel, it’s always good to know how your database is actually managed and operated. Knowing the CLI commands and how to create and maintain your database from the terminal will give you much greater flexibility and efficiency in the long run.
And as you’ve seen in this guide, creating a database from the terminal isn’t as complex as it seems. Neither are other more complex tasks, as you can see in our beginner-to-advanced MySQL database tutorial.
Still, if you think you may face some problems… well, you can. But let’s go through the main issues you may experience and how to troubleshoot them yourself.
Troubleshooting MySQL Database Creation Issues
Even with a simple command like CREATE DATABASE, things can go wrong. Typos, permission issues, or system quirks can lead to confusing error messages—especially if you’re new to the command line.
Here are some of the most common errors users encounter when creating MySQL databases from the CLI and how to fix them.

Access Denied When Logging In
The most common error you will stumble upon is:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
It usually means there’s an issue with the login credentials. You may be entering the wrong password, using an incorrect username, or attempting to access MySQL with a user who lacks sufficient privileges.
That’s why having a simple yet unique username is preferable. This way, you will only have to type one long word – the password.
How to fix:
Double-check your username and password
- Make sure you’re using the right login format
- If you’re not the root user, ask the admin or host to grant you the right privileges.
mysql -u root -p
Trying to create a Database That Already Exists
This one often happens when you’re not following the best naming practices. Whenever you see:
ERROR 1007 (HY000): Can't create database 'store_db'; database exists
MySQL is telling you that the database already exists, and it won’t create it again.
How to fix:
- First, confirm with:
SHOW DATABASES;
- If you need a fresh database, delete the old one carefully:
DROP DATABASE store_db;
Make sure to have a backup before you execute this command. Dropping a database is permanent. Everything inside will be lost forever.
SQL Syntax Errors
One of the most common (and most frustrating) issues looks like this:
ERROR 1064 (42000): You have an error in your SQL syntax...
This usually happens because of a typo or formatting issue. Maybe you forgot the semicolon, added a space where it doesn’t belong, or used a special character.
How to fix:
- Double-check your command
- Fix all typos
- Remove all spaces
- End with a semicolon
Using Reserved Words
As we have already explained, using a reserved word like ‘user,’ ‘order,’ or ‘select‘ as a database name will cause issues sooner or later. Most probably sooner.
How to fix:
- Just follow the naming best practices
Missing Privileges
Sometimes, the problem isn’t your syntax—it’s your permissions. If you see:
ERROR 1044 (42000): Access denied for user 'admin'@'localhost' to database 'new_db'
That means the MySQL user you’re logged in with doesn’t have permission to create a database.
How to fix:
- Log in as root
- Contact your hosting provider to request the CREATE privilege.
- If you’re managing your own server, you can assign privileges yourself:
GRANT CREATE ON *.* TO 'admin'@'localhost';
FLUSH PRIVILEGES;
In our vast experience, we’ve seen it all. However, we can not underline how often the problem is a typo. So, before you panic-open 300 websites to see how to fix a problem, just check if you typed everything correctly.
Moreover, MySQL’s CLI includes a helpful built-in reference. All you need to do is run:
HELP CREATE DATABASE;
This will give you a quick reminder of proper syntax—right from the terminal.
Take Full Control of Your Databases with HostArmada
Learning how to create a database in MySQL using the command line is a valuable skill. However, having a hosting provider that just gives you root access won’t be enough to have a winning website. You need a partner that supports you with the right tools, resources, and performance.
At HostArmada, we offer powerful yet beginner-friendly hosting solutions that support both CLI and cPanel workflows. Whether you’re managing databases manually or through a visual interface, our hosting environment is optimized for MySQL performance, security, and scalability.
Need more help with database management? We’ve created a comprehensive MySQL Database Tutorial to guide you through more advanced tasks, such as backups, indexing, and optimization.
If you prefer cPanel, we have a detailed tutorial on how to manage MySQL as well.
Along with all the help you can get with MySQL, you will receive lightning-fast website load time, top-of-the-line security, and a 99.9% uptime guarantee.
If you’re ready to simplify your database management without sacrificing control while ensuring your website performs at its maximum, check out our plans and choose the one that best fits your needs.