Skip to main content

How to install MySQL Docker Container

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.

 

Prerequisites

  • Access to a command line/terminal window
  • A user account with sudo privileges
  • 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:

docker pull mysql/mysql-server:latest

If you want a particular version of MySQL, replace latest with the version number.

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

docker images

The output should include mysql/mysql-server among the listed images.

example Docker image on your system for deploying a MySQL Docker container

Step 2: Deploy the MySQL Container

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

docker run --name=[container_name] -d mysql/mysql-server:latest
  • Replace [container_name] with the name of your choice. If you do not provide a name, Docker generates a random one.
  • The -d option instructs Docker to run the container as a service in the background.
  • In the command above, we used the latest version tag. This may differ according to the image you downloaded.

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 health: starting to healthy, once the setup is complete.

Command to deploy a MySQL Docker container and verify MySQL container is running

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, start a MySQL client inside the container by typing:

docker exec -it [container_name] mysql -uroot -p

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

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

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '[newpassword]';

Replace [newpassword] with a strong password of your choice.

Configure MySQL Container

When you install a MySQL container, you will find its configuration options in the /etc/mysql/my.cnf 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:

mkdir -p /root/docker/[container_name]/conf.d

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

nano /root/docker/[container_name]/conf.d/my-custom.cnf

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:

[mysqld]
max_connections=250

File changing the configuration of a MySQL container by expanding the maximum number of connections.

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:

docker run \
--detach \
--name=[container_name]\
--env="MYSQL_ROOT_PASSWORD=[my_password]" \
--publish 6603:3306 \
--volume=/root/docker/[container_name]/conf.d:/etc/mysql/conf.d \
mysql

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

mysql -uroot -pmypassword -h127.0.0.1 -P6603 -e 'show global variables like "max_connections"';

You should see that the maximum number of connections is now 250.

Manage Data Storage

By default, Docker stores data in its internal volume.

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

docker inspect [container_name]

You will see the /var/lib/mysql mounted in the internal volume.

List details of MySQL Docker container and data storage location


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:

mkdir -p /storage/docker/mysql-data

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

docker run \
--detach \
--name=[container_name] \
--env="MYSQL_ROOT_PASSWORD=my_password" \
--publish 6603:3306 \
--volume=/root/docker/[container_name]/conf.d:/etc/mysql/conf.d \
--volume=/storage/docker/mysql-data:/var/lib/mysql \
mysql

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

docker inspect [container_name]

Start, Stop, and Restart MySQL Container

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

To start the MySQL container run:

docker start [container_name]

Stop the MySQL container, use the command:

docker stop [container_name]

To restart the MySQL container run:

docker restart [container_name]

Delete MySQL Container

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

Then, remove the docker container with:

docker rm [container_name]

example of stopping and deleting a MySQL container



So I hope you have successfully installed MySQL server and in-case did you face any errors at meanwhile then revert me back via comment I will be happy to provides an Resolution for it.

Comments

Popular posts from this blog

What is STP? - Explain Advantages and Disadvantages

The Spanning Tree Protocol is a network protocol that builds a loop-free logical topology for Ethernet networks. The basic function of STP is to prevent bridge loops and the broadcast radiation that results from them. STP is a protocol. It actively monitors all links of the network. To finds a redundant link, it uses an algorithm, known as the STA (spanning-tree algorithm). The STA algorithm first creates a topology database then it finds and disables the redundant links. Once redundant links are disabled, only the STP-chosen links remain active. If a new link is added or an existing link is removed, the STP re-runs the STA algorithm and re-adjusts all links to reflect the change. STP (Spanning Tree Protocol) automatically removes layer 2 switching loops by shutting down the redundant links. A redundant link is an additional link between two switches. A redundant link is usually created for backup purposes. Just like every coin has two sides, a redundant link, along with

What are the Advantages and Disadvantages of TCP/UDP ?? Difference between TCP and UDP

As in previous blog we have define and explain about what is TCP and UDP and from now we are moving ahead with Advantages, Disadvantages and Difference of TCP and UDP but for this you have to know about TCP and UDP hence to understand it go for a What is TCP and UDP . Advantage of TCP Here, are pros/benefits of TCP: It helps you to establish/set up a connection between different types of computers. It operates independently of the operating system. It supports many routing-protocols. It enables the internetworking between the organizations. TCP/IP model has a highly scalable client-server architecture. It can be operated independently. Supports several routing protocols. It can be used to establish a connection between two computers. Disadvantages of TCP Here, are disadvantage of using TCP: TCP never conclude a transmission without all data in motion being explicitly asked. You can't use for broadcast or multicast transmission. TCP has no block boundaries, so you