# Creating a Database

MySQL is a leading open-source relational database management system (RDBMS) known for its reliability, scalability, and ease of use. Setting up a database properly in MySQL is crucial for ensuring long-term maintainability, performance, and security of applications. This guide walks through different ways to create a MySQL database: using the MySQL CLI, using Docker containers, and using the <span class="s2">mysqladmin</span> tool. It also emphasises best practices that should be followed at each step.

## **Creating a Database Using MySQL CLI**

The most common and straightforward way to create a database is by using the MySQL command-line interface (<span class="s2">mysql</span> client). First, a connection must be established to the MySQL server using an account with appropriate privileges, typically the <span class="s2">root</span> account or a designated administrative user.

#### **Connect to MySQL:**

Connect to MySQL:

```
mysql -u root -p
```

To connect to a <span class="s1">remote MySQL database</span> using the MySQL CLI, you need to specify the host’s IP address or domain name using the `<span class="s2">-h</span>` flag along with the username and password:

```mysql
mysql -h <remote_host> -P <port> -u <username> -p
```

You will be prompted to enter the password for the <span class="s1">root</span> user. Upon successful login, the MySQL shell opens where SQL queries can be executed.

#### **Create a New Database**

To create a database with default settings:

```
CREATE DATABASE mydatabase;
```

However, it is a best practice to explicitly define the character set and collation. This prevents potential problems with encoding and sorting, especially when dealing with multilingual data or special characters. The recommended character set for modern applications is <span class="s1">utf8mb4</span>, which fully supports Unicode.

Create a database with specific character set and collation:

```
CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```

You can verify that the database was created by listing all databases:

```
SHOW DATABASES;
```

Explicitly setting the character set ensures data consistency and minimizes future migration issues. Additionally, defining these settings at creation time avoids relying on server defaults, which can vary across different environments.

## **Creating a Database in Docker**

Docker is a tool that helps run applications in isolated environments called containers. A MySQL container provides a self-contained database instance that can be quickly deployed and managed. If you are running MySQL inside a Docker container, follow these steps:

#### **Access Elestio Terminal**  


Head over to your deployed MySQL service dashboard and head over to **Tools &gt; Terminal**. Use the credentials provided there to log in to your terminal.

[![image.png](https://docs.elest.io/uploads/images/gallery/2025-04/scaled-1680-/LoCimage.png)](https://docs.elest.io/uploads/images/gallery/2025-04/LoCimage.png)

Once you are in your terminal, run the following command to head over to the correct directory to perform the next steps

```
cd /opt/app/
```

#### **Access the MySQL Container Shell**

<div id="bkmrk-instead-of-pulling-a"><div>Instead of pulling an image or running the container manually, use Docker Compose to interact with your running container. As you are using Elestio, it will already be a Docker compose:</div></div>```
docker-compose exec mysql bash
```

Inside the container, access the MySQL shell:

```
mysql -u root -p
```

#### **Create Database**

Now, to create a database, use the following command. This command tells MySQL to create a new logical database called `mydatabase`. By default, it inherits settings like encoding and collation from the template database (<span class="s1">template1</span>), unless specified otherwise.

```
CREATE DATABASE mydatabase;
```

## **Creating a Database Using** `<strong>mysqladmin</strong>`

The `<span class="s2">mysqladmin</span>` utility provides a non-interactive way to create databases. It is particularly useful for automation scripts and quick administrative tasks.

To create a database:

```
mysqladmin -h <host> -P <port> -u root -p create mydatabase
```

One limitation of `<span class="s1">mysqladmin</span>` is that it does not allow specifying character set and collation at creation time. Therefore, if these settings need to be controlled explicitly (which is generally recommended), it is better to use the <span class="s1">mysql</span> CLI instead.

Before using <span class="s1">mysqladmin</span>, ensure the MySQL server is running. On traditional installations, check the status:

```
sudo systemctl status mysql
```

If the service is not active, it can be started with:

```
sudo systemctl start mysql
```

## **Best Practices for Creating Databases**


**Use Meaningful Names**

Choosing clear and descriptive names for databases helps in organisation and long-term maintenance. Avoid generic names like <span class="s3">testdb</span> or <span class="s3">database1</span>, as they do not convey the database’s role or content. Instead, choose names that reflect the kind of data stored or the application it supports, such as <span class="s3">customer\_data</span>, <span class="s3">sales\_records</span>, or <span class="s3">analytics\_db</span>. Meaningful names improve clarity for developers, DBAs, and future maintainers who need to quickly understand the purpose of each database without relying heavily on documentation.

**Follow Naming Conventions**

A standardized naming convention across all environments and teams simplifies database management and reduces confusion. MySQL database names are case-sensitive on Unix-based systems, so consistent use of lowercase letters is recommended. Use underscores to separate words (e.g., <span class="s3">order\_details</span>) rather than camelCase or spaces. This avoids the need for extra quoting in SQL queries and prevents platform-specific bugs. Additionally, avoid using reserved MySQL keywords or special characters in database names, as these can lead to parsing errors and unexpected behaviour.

**Restrict User Permissions**

Granting only the minimum required permissions significantly strengthens database security and reduces the likelihood of accidental damage or data leaks. Following the Principle of Least Privilege, reporting users should only be given <span class="s3">SELECT</span> access, while application users may require `<span class="s3">SELECT</span>`, `<span class="s3">INSERT</span>`, `<span class="s3">UPDATE</span>`, and `<span class="s3">DELETE</span>` rights. Only a few trusted administrative users should have powerful privileges like `<span class="s3">ALTER</span>`, `<span class="s3">DROP</span>`, or `<span class="s3">GRANT</span>`. Avoid assigning superuser access unless absolutely necessary. Creating user roles or groups with defined scopes can help standardise permission levels across teams and services.

**Enable Backups**

Regular backups are critical to ensure business continuity and safeguard against data loss from unexpected events such as accidental deletions, server crashes, or software bugs. MySQL provides tools like <span class="s3">mysqldump</span> for logical backups of individual databases, and <span class="s3">mysqlpump</span> or <span class="s3">xtrabackup</span> for more advanced use cases. It’s good practice to schedule automated backups using cron jobs or database orchestration tools. Backup files should be stored securely and regularly tested for restoration to verify that the process works as expected during emergencies.

**Monitor Performance**

Ongoing performance monitoring is essential to maintain the responsiveness and stability of MySQL databases. Monitoring tools like `<span class="s3">performance_schema</span>`, `<span class="s3">information_schema</span>`, or external platforms like Percona Monitoring and Management (PMM) help identify slow queries, locked transactions, and system resource bottlenecks. Use `<span class="s3">EXPLAIN</span>` and `<span class="s3">ANALYZE</span>` to understand query plans and optimize indexes. Keeping an eye on connection stats, query latency, and buffer pool usage allows for timely tuning and ensures efficient database operations at scale.

## **Common Issues and Their Solutions**

Here’s a table summarizing common problems faced during database creation and how to resolve them:

<table border="1" id="bkmrk-issue-cause-solution" style="border-collapse: collapse; border-color: rgb(0, 0, 0);"><thead><tr><th style="border-color: rgb(0, 0, 0);">**Issue**

</th><th style="border-color: rgb(0, 0, 0);">**Cause**

</th><th style="border-color: rgb(0, 0, 0);">**Solution**

</th></tr></thead><tbody><tr><td style="border-color: rgb(0, 0, 0);">ERROR 1044 (42000): Access denied for user

</td><td style="border-color: rgb(0, 0, 0);">The connected user does not have the <span class="s1">CREATE</span> privilege.

</td><td style="border-color: rgb(0, 0, 0);">Connect as a user with administrative privileges or grant necessary permissions.

</td></tr><tr><td style="border-color: rgb(0, 0, 0);">ERROR 1007 (HY000): Can't create database; database exists

</td><td style="border-color: rgb(0, 0, 0);">Attempting to create a database that already exists.

</td><td style="border-color: rgb(0, 0, 0);">Choose a different name or drop the existing database if appropriate using <span class="s1">DROP DATABASE</span>.

</td></tr><tr><td style="border-color: rgb(0, 0, 0);">Can't connect to MySQL server on 'localhost'

</td><td style="border-color: rgb(0, 0, 0);">MySQL server is not running, or incorrect connection parameters are used.

</td><td style="border-color: rgb(0, 0, 0);">Start the MySQL service and verify network and authentication parameters.

</td></tr><tr><td style="border-color: rgb(0, 0, 0);">Collation or character set issues later in application

</td><td style="border-color: rgb(0, 0, 0);">Database created without explicitly specifying character set or collation.

</td><td style="border-color: rgb(0, 0, 0);">Always specify <span class="s1">utf8mb4</span> and a collation like <span class="s1">utf8mb4\_unicode\_ci</span> during database creation.

</td></tr><tr><td style="border-color: rgb(0, 0, 0);">Docker MySQL container refuses connections

</td><td style="border-color: rgb(0, 0, 0);">MySQL container not ready or port mappings not correctly set.

</td><td style="border-color: rgb(0, 0, 0);">Check container logs with <span class="s1">docker-compose logs mysql</span> and verify port exposure settings in <span class="s1">docker-compose.yml</span>.

</td></tr></tbody></table>