SoFunction
Updated on 2025-04-11

Using Docker container to deploy MySQL on Linux

Docker container deploys MySQL on Linux

Basic steps to deploy MySQL server using Docker

Including the following points:

The MySQL Docker image maintained by the MySQL team is specially built for the Linux platform. Other platforms are not supported, and users who use these MySQL Docker images on these platforms will be at their own risk.

Download MySQL Server Docker Image

Important Tips

For MySQL Enterprise Users: To use the MySQL Enterprise Docker image, a subscription is required. Subscriptions adopt the Bring Your Own License model.

Downloading the server image in a separate step is not absolutely necessary; however, performing this step before creating a Docker container ensures that the local image is up to date. To download the MySQL Community Edition image from the Oracle Container Registry (OCR), run the following command:

docker pull /mysql/community-server:tag

A tag is the tag of the mirror version you want to pull (for example, 5.7, 8.0, or latest). If `:tag` is omitted, the latest tag will be used and an image of the latest universal version (GA) of the MySQL community server will be downloaded.

To download the MySQL Enterprise Edition image from OCR, you need to first accept the license agreement on the OCR and log in to the container repository using your Docker client. Please follow these steps:

  • Visit the OCR website / and select MySQL.
  • Under the MySQL repository list, select enterprise-server.
  • If you are not logged into OCR, click the Login button on the right side of the page and enter your Oracle account credentials when prompted.
  • Follow the instructions on the right side of the page to accept the license agreement.
  • Log in to the OCR using your container client, for example using the docker login command:
# docker login  
Username: Oracle-Account-ID
 Password: password
 Login successful.

Download the MySQL Enterprise Docker image from OCR using the following command:

docker pull  /mysql/enterprise-server:tag

To download the MySQL Enterprise Edition image from the My Oracle Support website, visit the website, log in to your Oracle account, and perform the following steps on the login page:

  • Select the Patches and Updates tab.
  • Go to the Patch Search area and switch to the Product or Family subtab under the Search tab.
  • Enter "MySQL Server" in the Product field and enter the required version number in the "Release" field.
  • Use the drop-down menu of the additional filter to select "Description—contains" and enter "Docker" in the text box.
  • Click the Search button, select the version you want from the results list, and then click the Download button.
  • In the File Download dialog box that appears, click and download the .zip file of the Docker image.

Unzip the downloaded .zip package to get the tarball() in it, and then run the following command to load the image:

docker load -i 

You can list the downloaded Docker images using the following command:

$> docker images
 REPOSITORY                                             TAG       IMAGE ID       CREATED        SIZE
 /mysql/community-server   latest    1d9c2219ff69   2 months ago   496MB

Start a MySQL server instance

To start a new Docker container for MySQL Server, use the following command:

docker run --name=container_name  --restart on-failure -d image_name:tag 

image_name is the name of the image used to start the container;

  • --nameThe option is used to provide a custom name for your server container and is optional; if a container name is not provided, a random name is generated.
  • --restartThe option is used to configure the container's restart policy; it should be set to the value on-failure to enable support for server restarts in client sessions (for example, when the client executes a RESTART statement or when configuring an InnoDB cluster instance). After enabling restart support, issuing a restart in a client session causes the server and container to stop and then restart. Support for server restarts are available for MySQL 8.0.21 and later.

For example:

To start a new Docker container for MySQL Community Server, use the following command:

docker run --name=mysql1 --restart on-failure -d /mysql/community-server:lates

To start a new Docker container for MySQL Enterprise Server using a Docker image downloaded from OCR, use the following command:

docker run --name=mysql1 --restart on-failure -d /mysql/enterprise-server:late

To start a new MySQL Enterprise Server Docker container using the Docker image downloaded from My Oracle Support, use this command:

docker run --name=mysql1 --restart on-failure -d mysql/enterprise-server:latest

If the Docker image with the specified name and label has not been downloaded through the previous docker pull or docker run command, the image will now be downloaded.

The initialization of the container begins, and when you run the docker ps command, the container appears in the list of running containers.

For example:

$> docker ps
 CONTAINER ID   IMAGE                                                         COMMAND                  CREATED          STATUS                    PORTS                       NAMES
 4cd4129b3211   /mysql/community-server:latest   "/ mysq…"   8 sec

Container initialization may take some time. When the server is ready to use, the status of the container in the output of the docker ps command will be changed from (health: starting) to (healthy).

The -d option in the docker run command used above makes the container run in the background.

Use this command to monitor the output of the container:

docker logs mysql1

Once initialization is complete, the output of the command will contain a random password generated for the root user;

For example, check the password using the following command:

$> docker logs mysql1 2>&1 | grep GENERATED
 GENERATED ROOT PASSWORD: Axegh3kAJyDLaRuBemecis&EShOs

Connect to the MySQL server from inside the container

Once the server is ready, you can run the mysql client within the MySQL server container you just started and connect to the MySQL server.

Use the docker exec -it command to start a mysql client in the Docker container you started. The example is as follows:

docker exec -it mysql1 mysql -uroot -p

When requested, enter the generated root password (see the last step of "Starting MySQL Server Instance" above to find the password method).

Since the MYSQL_ONETIME_PASSWORD option defaults to true, after you connect the mysql client to the server, you must reset the server's root password by executing the following statement:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

Replace password with the password of your choice. Once the password reset is complete, the server is ready for use.

Container shell access

To access the shell of the MySQL server container, use the docker exec -it command to start a bash shell inside the container:

$> docker exec -it mysql1 bash
 bash-4.2#

You can then run Linux commands inside the container.

For example, to view the contents in the server data directory within a container, use the following command:

bash-4.2# ls /var/lib/mysql
             ib_logfile0  ibdata1  mysql           private_key.pem    sys
     ib_buffer_pool  ib_logfile1  ibtmp1     performance_schema  publ

Stop and delete MySQL containers

To stop the MySQL server container we created, use the following command:

docker stop mysql1

docker stop sends a SIGTERM signal to the mysqld process, allowing the server to be shut down gracefully.

Also note that when the main process of the container (mysqld in the case of MySQL server container) stops, the Docker container will automatically stop.

To start the MySQL server container again:

docker start mysql1

To stop and restart the MySQL server container with one command:

 docker restart mysql1

To delete the MySQL container, first stop it and then use the docker rm command:

docker stop mysql1
docker rm mysql1 

If you want to delete Docker volumes of the server data directory at the same time, add the v option in the docker rm command.

Upgrade MySQL server container

Important matters:

  • Back up your database before any upgrade to MySQL.
  • The instructions in this section require persisting the server's data and configuration to the host.

Follow these steps to upgrade MySQL versions 5.7 to 8.0 in Docker:

  • Stop the MySQL 5.7 server (the container name is mysql57 in this example):
 docker stop mysql57
  • Download the Docker image of MySQL 8.0 server. Make sure to use the correct tags for MySQL 8.0.
  • Start a new MySQL 8.0 Docker container (named mysql80 in this example) using the old server data and configuration that is persisted on the host by binding it to mount it. For MySQL Community Server, run the following command:
docker run --name=mysql80 \
   --mount type=bind,src=/path-on-host-machine/,dst=/etc/ \
   --mount type=bind,src=/path-on-host-machine/datadir,dst=/var/lib/mysql \
   -d /mysql/community-server:8.0

If necessary, please adjust /mysql/community-server to the correct image name.

For example, use /mysql/enterprise-server to replace MySQL Enterprise images that can be downloaded from OCR, or use mysql/enterprise-server to download MySQL Enterprise images from my Oracle support.

  • Wait for the server to complete startup. You can use the docker ps command to check the status of the server.

For upgrading in the 8.0 series (i.e. upgrade from version 8. to version 8.), follow the same steps: Stop the original container and start a new container on the old server data and configuration using the updated image.

If you use 8.0 or the latest tag when starting the original container, and now there is a new version of MySQL 8.0 that you want to upgrade, you must first pull the new version of the image using the following command:

docker pull /mysql/community-server:8.0

You can upgrade by starting a new container with the same tag on the old data and configuration.

docker run --name=mysql80new \
   --mount type=bind,src=/path-on-host-machine/,dst=/etc/ \
   --mount type=bind,src=/path-on-host-machine/datadir,dst=/var/lib/mysql \
   -d /mysql/community-server:8.0

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.