Home > Operation and Maintenance > Docker > How to do database mirroring with docker

How to do database mirroring with docker

PHPz
Release: 2023-04-26 14:11:32
Original
1478 people have browsed it

Docker is currently the most popular containerization technology, which can package applications and their dependencies into an independent container and run in different environments. Databases are a core component of applications, and running databases in Docker is a very common scenario because it provides a convenient, safe, and repeatable way to test, deploy, and scale database applications.

So, how to create a database image in Docker? The steps to create a MySQL database mirror are described in detail below.

1. Download the official MySQL image

You can download the official MySQL image from Docker Hub. We can use the following command to download the MySQL image from Docker Hub:

docker pull mysql/mysql-server
Copy after login

2. Create a customized MySQL image

Although we can use the official MySQL image directly, it may not meet our needs, such as configuration files, initialization scripts, data backup, etc. Therefore, we must create a customized image based on the official image to meet our needs.

1. Create a Dockerfile

Dockerfile is a text file that contains a series of instructions to build a Docker image. We use the MySQL official image as the basis, and then create a customized image by adding configuration files, initialization scripts, data backup, etc.

In this example, we create a Dockerfile file with the following content:

FROM mysql/mysql-server

# 安装telnet和net-tools
RUN yum update && yum install -y telnet net-tools

# 添加自定义配置文件
ADD my.cnf /etc/mysql/my.cnf

# 添加初始化脚本
ADD init.sql /docker-entrypoint-initdb.d/

# 添加数据备份
ADD backup.sql /tmp/backup.sql
Copy after login

The explanation of the above Dockerfile file is as follows:

  • FROM mysql/mysql-server : Use the MySQL official image as the base image.
  • RUN yum update && yum install -y telnet net-tools: Install telnet and net-tools tools.
  • ADD my.cnf /etc/mysql/my.cnf: Add the custom configuration file my.cnf to the /etc/mysql/ directory.
  • ADD init.sql /docker-entrypoint-initdb.d/: Add the initialization script init.sql to the /docker-entrypoint-initdb.d/ directory.
  • ADD backup.sql /tmp/backup.sql: Add data backup backup.sql to the /tmp/ directory.

2. Build the image

In this example, we have prepared Dockerfile files, custom configuration files, initialization scripts, data backup and other resources. Next, you need to execute the following command in the directory where the Dockerfile file is located to build the image:

docker build -t my-mysql:latest .
Copy after login

Among them, -t is used to name the image, and :latest means to use the latest version.

3. Run the MySQL container

Now that we have successfully created a custom MySQL image, we will run the image in the container and assign a name to the container:

docker run -d -p 3306:3306 --name my-mysql -v /data/mysql:/var/lib/mysql my-mysql
Copy after login

Among them, -d means running the container in the background, -p maps the container's 3306 port to the host's 3306 port, -v maps the host's /data/mysql directory to the container's /var/lib/mysql directory, my -mysql indicates the name of the container.

Now we can use the MySQL client tool to connect to the MySQL container and test that it is running properly.

mysql -h 127.0.0.1 -P 3306 -u root -p
Copy after login

4. Conclusion

Through this process, we have learned how to create a custom MySQL image in Docker and run the image. This makes it easier to test, deploy, and scale database applications. In actual projects, we can add more custom configurations and initialization scripts as needed, as well as backup and recovery data and other functions.

The above is the detailed content of How to do database mirroring with docker. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template