MySQL is an open-source database management system and one of the prominently used web server solutions. It is used to store and efficiently structure the data so that the user can easily access the data.
Docker is a set of platform-as-a-service(PaaS) products that support CI/CD development for various software. It also provides the feature to build and deploy the applications inside the virtual environment of Docker, called container. With just a single image, Docker can boot up an application with all its libraries, containers, and dependencies.
- Access to a command line/terminal window for running various commands.
- A user who has an account with sudo privileges or access to the root account of the local machine.
- An existing Docker installation of the system.
Running a Container in MySQL Docker
You can deploy your MySQL container in a Docker container easily and efficiently. The deployment process does not require too many resources for Docker containers. It can be a really useful resource for small and medium-scale applications. Enterprise-level applications would not find a MySQL Docker container sufficient for their workload and deployment process.
Nowadays, the use of MySQL Docker is increasing rapidly on small-scale applications. It provides a separate server for database hosting to deploy a MySQL database container.
Multiple containers can run at the same time on your Docker computer. The containers responsible for deployment have the same library and kernel of the host while packaging the deployed application or software into single units. This helps to make the database fast and efficient for development and deployment.
Installing the MySQL Docker Container
Setting up a database in Docker is basically building a container based on a MySQL image.
Follow the steps below to get your MySQL container up and running.
Step - 1: Pull MySQL Docker Image
- You should start the installation step by pulling the appropriate Docker image for MySQL. You can download a specific version and install the latest release from the following command:
sudo docker pull mysql/mysql-server:latest
If you want a particular version of MySQL Docker, you can replace the latest with any other version number.
- You should now verify that the image is stored locally by documenting the downloaded Docker images for MySQL using the below command:
|sudo docker images
The output should also include mysql/mysql-server among the listed images in the MySQL Docker.
Step - 2: Deploy MySQL Container
- Once you have the image, you can move on to deploying a new MySQL container with the following code:
sudo docker run --name=[container_name] -d [image_tag_name]
Replace [container.name] with the name of your choice for MySQL Docker. If you do not provide a name, Docker generates a random name by itself. The -d option allows Docker to run the container as a service in the background for your software.
Replace [image.tag.name] with the name of the image downloaded in Step 1 above.
In this example, we create a container in MySQL Docker named mysql.docker with the latest version tag shown below:
sudo docker run --name=[container_name] -d mysql/mysql-server:latest
- Then, you should check if the MySQL container is running correctly or not using the following command:
You should see the newly created container listed in the output of your program. It includes container details, one being the status of this virtual environment. The status changes from health: starting to healthy once the setup is complete and the system is giving correct outputs.
Step 3: Connect to MySQL Docker Container
- Before you connect the MySQL server container to the host, you need to make sure the MySQL client package is installed. You can check this with the help of the command below:
apt-get install mysql-client
- Now, as you have checked the installation of the client package, you should open the logs file for the MySQL container to find the generated root password of the system using the below command:
sudo docker logs [container.name]
For the mysql_docker container, we need to run a different command which is provided below:
sudo docker logs mysql.docker
- Scroll through the output and find the line [Entry.point] GENERATED ROOT PASSWORD:. Copy and paste the password in a notepad or any other text editor so you can use it later when needed.
- Next, you need to go to the bash shell of the MySQL container by typing the following command:
sudo docker exec -it [container_name] bash
For the container you created as an example in the MySQL container, we should run the following command:
sudo docker -it mysql.docker bash
- Now you should provide the root password you have copied from the log file when it gets displayed. You have now successfully connected the MySQL server to the server client of your system.
- It is the final step of the information where at the end, you will change your password to protect your information:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '[newpassword]';
Now you should replace [newpassword] with a strong password of your choice to protect your data and information.
Start, Stop, and Restart MySQL Container
The container of the MySQL Docker automatically stops when the process running in it stops.
To start the MySQL container in docker, we should run the below command:
sudo docker start [container.name]
For stopping the MySQL container, we should use the following command:
sudo docker stop [container.name]
For restarting the restart in MySQL container, we need to run the following command:
sudo docker restart [container.name]
Delete MySQL Container
Before deleting a MySQL container, you must make sure you stop it first. Now, you can remove the docker container from MySQL with the command given below:
sudo docker rm [container.name]
Docker Compose MySQL
The docker run command in MySQL can quickly become lengthy and difficult to handle, especially if you have a configuration which has complex requirements. Writing a docker-compose.yml file is more maintainable in Docker MySQL.
The example for the Docker compose is provided below:
Now you will be able to start MySQL using MYSQL_ROOT_PASSWORD=secure docker-compose up -d.a.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
We hope this article helped you understand MySQL Docker. In this article, we discussed the concept of various types of processes of MySQL and their different types, along with examples that will be helpful to professional developers from Java and MySQL backgrounds, application architectures, database engineers, and other learners looking for information on MySQL Docker with the containers.
To know more about Docker MySQL, you can enroll in the Post-Graduate Program In Full-Stack Web Development offered by Simplilearn in collaboration with Caltech CTME. This Web Development course is a descriptive online bootcamp that includes 25 projects, a capstone project, and interactive online classes. In addition to Docker MySOL, the course also details everything you need to become a full-stack technologist and accelerate your career as a software developer.
Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. You can take up any of these free courses to upgrade your skills and advance your career.