In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Foreword:
The last article introduced the installation of Docker tools and the use of common commands. In this article, we will show you how to run an MySQL instance in Docker, and some friends may ask: why run MySQL in Docker? Because it is easy to deploy MySQL instances in Docker, you do not need to consider the operating system and other dependency differences, and you can deploy multiple instances. For example, our original server has MySQL5.7 installed, and if we want to run the MySQL8.0 instance again, we only need to start the MySQL8.0 image with Docker. Let's take a look at how to start a MySQL instance with Docker.
▍ 1. Pull the official image of MySQL
In the previous article, we introduced three basic concepts in Docker: images, containers, and repositories. To deploy MySQL in Docker, the first step is to pull the MySQL image from the official repository, where we pull the MySQL image from the Docker Hub. Go to Docker Hub and search MySQL, you can see the following screen, in which there are different versions of images and how to use them.
Cdn.nlark.com/yuque/0/2019/png/119537/1572918549506-2b81bb36-1574-4aea-bc6b-9bb21fd1178d.png ">
For example, if you want to pull the images of MySQL5.7 version and 8.0 version, you can do the following:
# pull the images of versions 5.7 and 8.0 in a moment and then pull the successful docker pull mysql:5.7.23docker pull mysql:8.0.18# view image docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql 8.0.18 c8ee894bd2bd 2 weeks ago 456MBmysql 5.7. 23 1b30b36ae96a 12 months ago 372MB
In fact, the official image of MySQL also has some defects, such as the time zone is not Beijing time, the character set of the system and so on. Based on the MySQL5.7.23 version image, the following shows how to modify the build image.
# the main purpose of creating Dockerfile is to change the system character set and the contents of the timely zone are as follows: cat Dockerfile FROM mysql:5.7.23MAINTAINER wangRUN echo "alias ls='ls-- color=auto'" > > / root/.bashrc\ & & ln-sf / usr/share/zoneinfo/Asia/Shanghai / etc/localtimeENV LANG=C.UTF-8# to build an image docker build-t my-mysql:5.7.23. Sending build context to Docker daemon 2.048kBStep 1 sf 4: FROM mysql:5.7.23-- > 1b30b36ae96aStep 2 sf 4: MAINTAINER wang-- > Running in a7cc94f95cc7Removing intermediate container a7cc94f95cc7-- > d9590ed98de5Step 3 root/.bashrc 4: RUN echo "alias ls='ls-- color=auto'" > > / root/.bashrc & & ln-sf / usr/share/zoneinfo/Asia/Shanghai / etc/localtime-- > Running in e698bfdb3817Removing intermediate container e698bfdb3817-- > 31c9ed9103c5Step 4max 4: ENV LANG=C.UTF-8-- > Running In eefa296fef94Removing intermediate container eefa296fef94-> 10aa697936e9Successfully built 10aa697936e9Successfully tagged my-mysql:5.7.23# check the image again to find our newly built image docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmy-mysql 5.7.23 10aa697936e9 About a minute ago 372MBmysql 8.0.18 c8ee894bd2bd 2 weeks ago 456MBmysql 5.7.23 1b30b36ae96a 12 months ago 372MB ▍ 2. Start the MySQL instance
Here we introduce how to use the image built above to run MySQL instances. MySQL containerization also requires mapping ports, data persistence, loading configuration files and other operations. Let's show you the specific startup operations.
2.1 create data persistence paths and configuration files
# create configuration file directory and data directory mkdir-p / data/mysql57/ {cnf,data} # add configuration file my.cnfcd / data/mysql57/cnf/vim my.cnf# configuration file contents are as follows Customizable [mysqld] pid-file = / var/run/mysqld/mysqld.pidsocket = / var/run/mysqld/mysqld.sockdatadir = / var/lib/mysqlserver-id = 33061max_connections = 1000sql_mode = NO_AUTO_CREATE_USER NO_ENGINE_SUBSTITUTIONlower_case_table_names=1innodb_file_per_table = 1log_timestamps=SYSTEMcharacter-set-server = utf8max_allowed_packet = 32Msort_buffer_size = 4Mread_buffer_size = 4Mjoin_buffer_size = 4Mbinlog_cache_size = 4Mtmp_table_size = 96Mmax_heap_table_size = 96Minnodb_buffer_pool_size = 512M#logsslow_query_log = 1slow_query_log_file = / var/lib/mysql/slow.loglong_query_time = 3log-bin = / var/lib/mysql/ Binlogbinlog_format = rowexpire_logs_days = 15log_bin_trust_function_creators = 1
2.2 docker run running MySQL instance
# A command starts a MySQL instance docker run-itd-p 33061itd 3306-- name mysql57-- hostname=mysql57-v / data/mysql57/cnf:/etc/mysql-v / data/mysql57/data:/var/lib/mysql-- privileged=true-e MYSQL_ROOT_PASSWORD=Asdf@123456 my-mysql:5.7.23# explains the meaning of each parameter-d: run the container in the background and return the container ID-i: run the container in interactive mode Usually use-t: to reassign a pseudo-input terminal to the container with-t, and usually use-p: to specify the port mapping with-I in the format: host (host) port: container port-- name= "mysql57": specify a name for the container-- hostname=mysql57: specify the container's hostname-v: bind a volume-- privileged=true: start the container in a privileged manner
2.3 check the status of the container
# check container status docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES04cd3d99d5cb my-mysql:5.7.23 "docker-entrypoint.s …" 13 seconds ago Up 11 seconds 33060/tcp, 0.0.0.0 seconds 33060/tcp 33061-> 3306/tcp mysql57# enters the container docker exec-it mysql57 / bin/bash or docker exec-it [CONTAINER ID] / bin/bash ▍ 3. Other related operations
So far, we have successfully run the MySQL instance in Docker, but it is not over yet. There are still many operations to be done, such as how to back up and restore, how to change the configuration, and so on. Let's briefly introduce these operations.
Backup recovery # backup docker exec mysql57 sh-c 'exec mysqldump-all-databases-uroot-p "$MYSQL_ROOT_PASSWORD" > / tmp/all-databases.sql# restore docker exec-I mysql57 sh-c' exec mysql-uroot-p "$MYSQL_ROOT_PASSWORD" < / tmp/all-databases.sql change configuration # just modify the host / data/mysql57/cnf/my.cnf file and restart the container to docker restart mysql57
If you want to connect the MySQL in the container locally through the tool, you can use the host ip plus the mapped port to connect. For example, the MySQL instance we created above can be connected through the host ip+30661 port. If you are unable to connect, please check the network and server firewall.
Summary:
This article mainly introduces the methods and steps of deploying MySQL in Docker, which you will find very simple and convenient, and can be deployed on a large scale and multi-instance. If you want to try the latest version 8.0.18, you can deploy it yourself. In fact, it can be easier. For example, you can start an instance by running docker run mysql:5.7.23 directly. However, for more standardization, it is recommended to mount the configuration file and data directory to the host.
Reference:
Https://hub.docker.com/_/mysql
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.