How to Run MySQL Using Docker. Run MySQL with less effort | by Mahbub Zaman | Towards Data Science

If you’re a database administrator, data scientist, or developer, you may have to use MySQL to store and manipulate data. Since Docker has become a popular DevOps tool in recent years, you will eventually need to know how to run MySQL inside a Docker container.
Running your application on a different machine can be nerve-wracking because of environment management. Docker solves this problem by using its containerization technology, which uses Docker images. If you are new to Docker or want to brush your skills, read the following post where I’ve covered some basics.
towardsdatascience.com
Once you’ve finished this tutorial, you’ll be on your way to testing any MySQL query using Docker. This method will save you time and effort since you don't have to install or configure MySQL.
First, you will need to install Docker. For this example, we will use a Docker compose file, a SQL file containing bootstrap data, also known as mysql-dump and macOS. We need to put both docker-compose.yml and school.sql files inside the same folder. The folder name used in this setup is MySQL-Snippets.
Docker Compose: We can use Docker compose to run multiple containers and define their properties inside a YAML file. These containers are known as services. It’s helpful when your application has multiple stacks, such as a web server and a database server.
MySQL-dump: A mysql-dump contains MySQL queries in plain text. The MySQL command-line client can run these commands and send them to the MySQL server.
Let’s break down the individual ingredients of the docker-compose.yml file.
version: '3.1'services:  db:    image: mysql    restart: always    environment:      MYSQL_ROOT_PASSWORD: root      MYSQL_DATABASE: test_db    ports:      - "3307:3306"    volumes:      - $HOME/Desktop/MySQL-Snippets/school.sql:/school.sql
First, we are using a version tag to define the Compose file format, which is 3.1. There are other file formats — 1, 2, 2.x, and 3.x. Get more information on Compose file formats from Docker’s documentation here.
We follow our version tag by the services hash. Inside this, we have to define the services we want to use for our application. For our application, we only have one service called db.
To make our setup process quick and easy, we are using the pre-built official image of MySQL for the image tag.
When we use always for the restart tag, the container always restarts. It can save time. For example, you don’t have to start the container every time you reboot your machine manually. It restarts the container when either the Docker daemon restarts or the container itself is manually restarted.
Under the environment tag, we have defined the environment variables, which we will use for database authentication.
The ports tag is used to define both host and container ports. It maps the port 3307 on the host to port 3306 on the container.
Finally, the volume tag is used to mount a folder from the host machine to the container. It comprises two fields separated by a colon. The first part is the path in the host machine. The second part is the path in the container. Remove this portion if you don’t want to mount the mysql-dump into the container.
Great, you’ve accomplished the setup for this post. Now run the following command from the same directory where the docker-compose.yml file is located. The command docker compose up starts and runs your entire app.
docker compose up
Now run docker ps command on the host machine to see a list of your running containers. As we can see, we have a running container called mysql-snippets_db_1.
We can also see an image is created by running docker images command.
Access the running container mysql-snippets_db_1 by running the following command.
docker exec -it mysql-snippets_db_1 bash
The docker exec command allows us to enter the running container. The flags -i -t (often written as -it) are used to access the container in an interactive mode. Now we are providing the name of the container we want to access, which in this case mysql-snippets_db. The bash command is used to get a bash shell inside the container, so we can access MySQL command line and execute MySQL queries.
You’ve just finished setting up the container. Now, we’ll walk through how to connect to MySQL server.
Connect to MySQL server using your username and password as root. Once connected to the server, you can run MySQL commands.
mysql -uroot -proot
Here’s an example you can try:
We can see our database called test_db, defined inside our Compose file. To select the database, run USE test_db; command. Use SHOW TABLES; command to show all the tables of our database test_db. Type ctrl + L to clear the console.
Since we don’t have any tables, the command SHOW TABLES; returned an empty set. Now you’ve confirmed you’ve connected to MYSQL server, and you can go onto the next step.
Now we can load the MySQL dump into our test_db database. Which in this case school.sql. It is accessible inside the container because we have mounted from the host machine.
mysql -uroot -p test_db < school.sql
Connect to the MySQL server using the username root. Enter the password root to authenticate. Now we are setting the default database to test_db. The < along with the filename is used to send the commands to the server.
If we run the MySQL command SHOW TABLES, we can see two tables called marks and students inside our database test_db, which are defined inside the school.sql.
A relational database such as MySQL is used in the industry often, so it’s a topic you’ll want to understand thoroughly. Now that you’ve completed this basic tutorial, you can dive deeper into exploring MySQL with Docker. You can find more MySQL commands here. Docker has revolutionized the way we manage environments, including development, testing, and production. That’s why you must acquire this skill for both personal and industrial purposes. I hope this will help you get started with MySQL and Docker.