13. Installing MySQL in Docker

Table of contents

1 Introduction

2. Deploy MySQL

2.1, Docker warehouse view image

2.2. Pull MySQL image

2.3. Create a persistent directory

2.4. Start the MySQL container

2.5. View the MySQL directory on the host

2.6. Local MySQL test

2.7. Create a new MySQL user and configure remote access

2.8. Local Navicat connection test

3. Why the database is not suitable for Docker containerization

3.1. Data security

3.2, hardware resource contention

3.3. Network bandwidth occupation

3.4. Additional isolation of data

4. Summary


1 Introduction

During development, usually self-built MySQL database is convenient for personal development and testing. Here, Docker is used to install MySQL 5.7.

2. Deploy MySQL

2.1, Docker warehouse view image

Go to the mirror warehouse: https://hub.docker.com/ and search for mysql.

The first item in the search result is the result we want.

Click to view the details, and you can see all the information about MySQL mirroring. What we need to install is MySQL5.7, so we enter into Tags.

Find the 5.7 version we want.

The commands in the black box behind are the commands that we can pull this version.

2.2. Pull MySQL image

Copy and pull the image command from Docker Hub and execute it.

docker pull mysql:5.7

2.3. Create a persistent directory

When deploying the MySQL database, you must consider the issue of data persistence. If the persistent data volume is not attached, once the MySQL container is destroyed, all data will be lost.

# 用于存放mysql配置文件
mkdir -p mysql/conf
# 用于存放mysql数据
mkdir -p mysql/data

2.4. Start the MySQL container

docker run -d -p 33066:3306 \
    -v /home/pengyaohuang/mysql/conf:/etc/mysql/conf.d  \
    -v /home/pengyaohuang/mysql/data:/var/lib/mysql \
    -e MYSQL_ROOT_PASSWORD=root \
    --name mysql mysql:5.7

-v Hang MySQL configuration files and data files on the host.

2.5. View the MySQL directory on the host

tree -d -L 3 /home/pengyaohuang/mysql/

2.6. Local MySQL test

First enter the docker container

docker exec -it mysql /bin/bash

Then enter MySQL through the command:

mysql -uroot -p

2.7. Create a new MySQL user and configure remote access

Create an admin_rw account:

# 创建用户
mysql> create user 'admin_rw'@'%' identified by '123456';
# 赋予所有权限
mysql> grant all privileges on *.* to 'admin_rw'@'%';
# 刷新权限
mysql> flush privileges;

2.8. Local Navicat connection test

Since I mapped to the host port as 33066, my MySQL connection information is: 192.168.74.128:33066

connection succeeded:

3. Why the database is not suitable for Docker containerization

Although we can install and use our MySQL normally according to the previous steps. But in actual projects, we rarely install MySQL using Docker, either directly on the host machine or using cloud service RDS. This is because the Docker container provides stateless services, so it is not suitable for deploying stateful services (such as databases) into Docker containers. There are 4 reasons:

3.1. Data security

Although Docker can persist the data in the container to the host machine through the data volume, it still cannot guarantee that the data will not be lost. If the container crashes and the database is not shut down properly, data may be lost.

3.2, hardware resource contention

Usually, multiple containers are started on a Docker host. If the database container and other application containers are deployed on the same host, resource contention will inevitably occur due to their different requirements for hardware resources. used problem.

3.3. Network bandwidth occupation

Docker's network is a virtual network, which is forwarded through the docker0 bridge on the host. Databases usually have relatively high requirements for network bandwidth. Therefore, if the database container and other application containers are deployed on the same host, the network bandwidth will inevitably become the bottleneck of the database performance.

3.4. Additional isolation of data

Deploying the database in a container will undoubtedly increase the isolation of the container, which is not conducive to the horizontal expansion of the database.

4. Summary

Docker is used to make it easier to build new environments and redeploy applications. In practice, once a database is deployed, it is rarely upgraded or redeployed. Therefore, from this perspective, the database is not suitable for Docker containerization.

Guess you like

Origin blog.csdn.net/p793049488/article/details/132471922