Create a MySQL Database on Linux via Command Line
Setting up a MySQL database on Linux using the command line can seem intimidating, but it’s a fundamental skill for anyone working with databases. In this blog post, we will walk you through the steps to create a MySQL database on a Linux system using simple and easy-to-follow commands.
Steps to create a MySQL Database on Linux via Command Line
- Access MySQL Shell
- Create a New Database
- List All Databases
- Use the New Database
- Create Tables and Insert Data (Optional)
1. Access MySQL Shell
Before we can create a database, we need to access the MySQL shell. Open a terminal or command prompt and enter the following command, providing your MySQL root user password when prompted:
1 | mysql -u root -p |
2. Create a New Database
Once you’re inside the MySQL shell, you can create a new database using the CREATE DATABASE
command. Choose a name for your database and replace <database_name>
with your desired name:
1 | CREATE DATABASE <database_name>; |
For example, to create a database named “mydb”, the command would be:
1 | CREATE DATABASE mydb; |
3. List All Databases
To verify that your new database has been created successfully, you can list all the databases using the following command:
1 | SHOW DATABASES; |
This command will display a list of all databases, including the one you just created.
4. Use the New Database
To start using your newly created database, you need to select it using the USE
command:
1 | USE <database_name>; |
For instance, to use the “mydb” database, run:
1 | USE mydb; |
5. Create Tables and Insert Data (Optional)
Once you have your database ready, you can create tables and start inserting data. This step is optional but essential for building your database’s structure and adding data to it. Use the CREATE TABLE
and INSERT INTO
commands to accomplish this.
Conclusion
Congratulations! You’ve successfully created a MySQL database on Linux using the command line. Understanding how to set up a database is a crucial skill for any developer or system administrator working with data-driven applications. By following these simple steps, you can quickly create and manage databases to support your projects.
Now that you have a new database, you can start building and storing data. Whether you’re working on a personal project or a large-scale application, databases are the backbone of data management. Happy coding!