Cara menggunakan docker mysql tutorial

Menggunakan Docker untuk menjalankan aplikasi dapat meringankan melakukan setup awal setiap melakukan deployment aplikasi. 

Dengan docker kita bisa juga deploy aplikasi lain dengan stack berbeda, ini tentunya akan lebih praktis.

MySQL is a well-known open-source relational database management system and one of the most popular web server solutions. It stores and structures data in a meaningful manner, ensuring easy accessibility.

Docker is a set of platform-as-a-service products that support CI/CD development. It allows users to develop and deploy applications inside virtual environments, called containers. With a single image, Docker can boot up an application with all its libraries and dependencies.

In this tutorial, learn how to deploy a MySQL Docker container and start working with the containerized database.

Prerequisites

  • Access to a command line/terminal window
  • A user account with sudo privileges or access to the root account
  • An existing Docker installation

Running a MySQL Docker Container

If you need to set up a database quickly and without using up too many resources, deploying MySQL in a container is a fast and efficient solution. This is only appropriate for small and medium-sized applications. Enterprise-level applications would not find a MySQL Docker container sufficient for their workload.

Using the Docker software for setting up your database is becoming increasingly popular for small-scale apps. Instead of having a separate server for database hosting, you can deploy a MySQL database container.

Multiple containers can run on your computer. The containers share the same kernel and libraries of the host while packaging the deployed application or software into single units. This makes the database extraordinarily lightweight and fast to spin up.

Installing a MySQL Docker Container

Setting up a database in Docker is simply building a container based on a MySQL image. Follow the steps outlined below to get your MySQL container up and running.

Step 1: Pull the MySQL Docker Image

1. Start by pulling the appropriate Docker image for MySQL. You can download a specific version or opt for the latest release as seen in the following command:

sudo docker pull mysql/mysql-server:latest

If you want a particular version of MySQL, replace sudo docker run --name=[container_name] -d [image_tag_name]4 with the version number.

2. Verify the image is now stored locally by listing the downloaded Docker images:

sudo docker images

The output should include sudo docker run --name=[container_name] -d [image_tag_name]5 among the listed images.

Step 2: Deploy the MySQL Container

1. Once you have the image, move on to deploying a new MySQL container with:

sudo docker run --name=[container_name] -d [image_tag_name]
  • Replace sudo docker run --name=[container_name] -d [image_tag_name]6 with the name of your choice. If you do not provide a name, Docker generates a random one.
  • The sudo docker run --name=[container_name] -d [image_tag_name]7 option instructs Docker to run the container as a service in the background.
  • Replace sudo docker run --name=[container_name] -d [image_tag_name]8 with the name of the image downloaded in Step 1.

In this example, we create a container named sudo docker run --name=[container_name] -d [image_tag_name]9 with the sudo docker run --name=[container_name] -d [image_tag_name]4 version tag:

sudo docker run --name=[container_name] -d mysql/mysql-server:latest

2. Then, check to see if the MySQL container is running:

docker ps

You should see the newly created container listed in the output. It includes container details, one being the status of this virtual environment. The status changes from sudo docker run --name=[container_name] -d mysql/mysql-server:latest1 to sudo docker run --name=[container_name] -d mysql/mysql-server:latest2, once the setup is complete.

Step 3: Connect to the MySQL Docker Container

1. Before you can connect the MySQL server container with the host, you need to make sure the MySQL client package is installed:

apt-get install mysql-client

2. Then, open the logs file for the MySQL container to find the generated root password:

sudo docker logs [container_name]

For the sudo docker run --name=[container_name] -d [image_tag_name]9 container, we run:

sudo docker logs mysql_docker

3. Scroll through the output and find the line sudo docker run --name=[container_name] -d mysql/mysql-server:latest4: , copy and paste the password in a notepad or text editor so you can use it later.

4. Next, go to the bash shell of the MySQL container by typing:

sudo docker exec -it [container_name] bash

For the container created as an example, we run:

sudo docker -it mysql_docker bash

3. Provide the root password you copied from the logs file, when prompted. With that, you have connected the MySQL client to the server.

4. Finally, change the server root password to protect your information:

sudo docker images0

Replace sudo docker run --name=[container_name] -d mysql/mysql-server:latest5 with a strong password of your choice.

Configure MySQL Container

When you install a MySQL container, you will find its configuration options in the sudo docker run --name=[container_name] -d mysql/mysql-server:latest6 directory.

If you need to modify the configuration, create an alternative config file on the host machine and mount them inside the container.

1. First, create a new directory on the host machine:

sudo docker images1

2. Create a custom MySQL config file inside that directory:

sudo docker images2

3. Once in the file, you can add lines with the desired configuration.

For example, if you want to increase the maximum number of connections to 250 (instead of the default 151), add the following lines to the configuration file:

sudo docker images3

4. Save and exit the file.

5. For the changes to take place, you need to remove and rerun the MySQL container. This time, the container uses a combination of configuration settings from the newly created file and the default config files.

To do this, run the container and map the volume path with the command:

sudo docker images4

6. To check whether the container loaded the configuration from the host, run the following command:

sudo docker images5

You should see that the maximum number of connections is now sudo docker run --name=[container_name] -d mysql/mysql-server:latest7.

Manage Data Storage

By default, Docker stores data in its internal volume.

To check the location of the volumes, use the command:

sudo docker images6

You will see the sudo docker run --name=[container_name] -d mysql/mysql-server:latest8 mounted in the internal volume.

You can also change the location of the data directory and create one on the host. Having a volume outside the container allows other applications and tools to access the volumes when needed.

1. First, find an appropriate volume on the host and create a data directory on it:

sudo docker images7

2. Now start the container again, mounting the previously made directory:

sudo docker images8

If you inspect the container, you should see that the MySQL container now stores its data on the host system. Run the command:

sudo docker images6

Start, Stop, and Restart MySQL Container

The container automatically stops when the process running in it stops.

To start the MySQL container run:

sudo docker run --name=[container_name] -d [image_tag_name]0

Stop the MySQL container, use the command:

sudo docker run --name=[container_name] -d [image_tag_name]1

To restart the MySQL container run:

sudo docker run --name=[container_name] -d [image_tag_name]2

Delete MySQL Container

Before deleting a MySQL container, make sure you stop it first.

Then, remove the docker container with:

sudo docker run --name=[container_name] -d [image_tag_name]3

Conclusion

After reading this article, you should have successfully deployed a MySQL container.

Combining Docker and MySQL can be an excellent solution for a small-scale application. Now you can start exploring all the possibilities of a MySQL container.

Postingan terbaru

LIHAT SEMUA