Our primary objective is to operate a MySQL database using Docker for enabling us to utilize MySQL functionalities without the need for installing it directly on our local machine.
- Docker Desktop: Docker Desktop is essential for running Docker containers on your local PC. You can download and install it from the official Docker website for your operating system (Windows, macOS, or Linux).
- Docker Compose: Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define your container configurations in a single YAML file. You can typically install Docker Compose as a separate component, but it often comes bundled with Docker Desktop for Windows and macOS users.
- Also, know the commands on how to use Docker image and Docker compose.
This section outlines the steps to run MySQL in a Docker container and explains how to access the database using two different methods.
Start the Container: Use the following command to bring up the MySQL container using Docker Compose:
docker compose up
Once the command is entered, you will see the following output:
To ensure data persists across container restarts, configure volumes in your Docker Compose file:
services:
db:
...
volumes:
- ./db_data:/var/lib/mysql
- ./scripts:/docker-entrypoint-initdb.d
volumes:
db_data:
- The
db_data
directory on your host is mapped tovar/lib/mysql
inside the container, preserving the database data. - The
scripts
directory contains the initialization SQL scriptcodebasics-movies-db.sql
. This directory is mounted todocker-entrypoint-initdb.d
in the container. MySQL automatically executes scripts in this directory upon container initialization. Scripts are executed in ascending / alphabetical order.
You can access the MySQL database in two ways:
Method 1: Docker Exec Command Command:
docker exec -it my_mysql_container /bin/bash mysql -p
Terminal output:
Replace my_mysql_container
with your actual container name if different.
- Password: When prompted, enter the
MYSQL_ROOT_PASSWORD
(in this case,root
).
Method 2: Using mycli
- Install mycli (if not installed):
pip install mycli
- Connect to MySQL:
mycli -u user -h 127.0.0.1 -P 5004 -p password moviesdb
Terminal output:
Here, replace:user
with your MySQL username.127.0.0.1
with your Docker host address (uselocalhost
if running locally).5004
with the port number mapped to MySQL's default port 3306.password
with your MySQL password.moviesdb
with your database name.
For reference, here are the related MySQL configuration settings in the Docker Compose file:
db:
container_name: my_mysql_container
...
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: moviesdb
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "5004:3306"
Now, we are ready to run SQL query.
The purpose of this tutorial is for the users to get started with using MySQL on Docker. We will just see if we can execute some SQL commands if SQL script is correctly executed or not during creation of the container.
- Lists all the current databases that exists in the server.
SHOW DATABASES;
Terminal output:
- Use a specific database from the list of databases in the server
USE moviesdb;
Terminal output:
- Show the tables from the selected database.
SHOW TABLES;
Terminal output:
- Fetch all the data from the
movies
table.
SELECT * FROM movies;
Terminal output:
- Lets try a little complex SQL query:
SELECT * FROM movies WHERE industry="hollywood";
We can see all these commands are working properly which indicates our sample sql script executed successfully.
Using a Docker image of MySQL over installing it on your local PC offers several advantages:
- Easy Setup: Setting up MySQL using a Docker image is as simple as pulling the image and running a container. When you're done, you can remove the container, leaving no residual configurations or data on your local machine.
- Portability: Docker containers are highly portable. You can easily share the MySQL container image with others, deploy it on different machines, or move it to a cloud environment without worrying about compatibility or configuration is.
- Isolation: Docker containers are isolated environments, which means that the MySQL instance running in a container won't interfere with other applications or services on your local PC. This isolation helps avoid conflicts and dependency issues.
- Version Control: Docker images can be version-controlled using tools like Docker Compose or Kubernetes. This ensures that you can easily manage different versions of MySQL and switch between them as needed.
- Testing and Dev: Docker makes it easy to create isolated testing and development environments. You can quickly create a container for MySQL, test your application against it, and then discard the container when you're finished.
- Backup/Restore: Docker provides tools for backing up and restoring containers, making it straightforward to manage MySQL data. You can easily create snapshots of container data for disaster recovery.
- Dependency Management: Docker allows you to package not only MySQL but also other dependencies (e.g., web servers, application servers) into containers, making it easier to manage complex development or production environments.
This setup provides a robust and flexible development environment, allowing you to work with MySQL in a containerized manner. Should you encounter any issues, refer back to the specific sections for troubleshooting tips. Happy database managing!
- Movie database taken from Codebasics: https://codebasics.io/resources/sql-tutorials-for-beginners