How to Create New Database in MSQL/MariaDB Server

Introduction

A MySQL database is a type of relational database management system (RDBMS) that is used to store, organize and manage data. It is one of the most popular open source databases available today, and it is widely used in many web applications. MySQL databases are highly scalable and secure, making them ideal for managing large amounts of data. They also provide a number of features that make them easy to use and maintain. With MySQL databases, users can easily create tables, query data, and even perform transactions with just a few simple commands.

Prerequisites

  • A server running CentOS or Oracle Linux
  • MySQL/MariaDB root access.

Create New Database in MSQL/MariaDB Server via command line

1. Login to MySQL database using root:

# mysql -u root -p

Example with output :

# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1234567
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

2. To create a database, use the “create database” or “CREATE DATABASE command as follows:

mysql> CREATE DATABASE DATABASE_NAME;

Example below will create a database with the name testdb :

mysql> CREATE DATABASE testdb;
Query OK, 1 row affected (0.15 sec)

Error you will received if a database of the same name already exists, the system will not create a new database.

ERROR 1007 (HY000): Can't create database 'testdb'; database exists

You can avoid this error by using the following command. It only creates the database testdb if a database of that name does not already exist.

mysql> CREATE DATABASE IF NOT EXISTS testdb;

3. Display the databases;

mysql> SHOW DATABASES;

Example output :

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

Conclusion

Creating your own databases on MySQL is an essential skill for anyone looking to create a comprehensive and secure data storage system. With the right best practices, you can ensure that your databases are well-structured, reliable, and easy to maintain.

Leave a Comment