Docker deployment mysql5.7.28 and data persistence

Objective: docker container deployment mysql5.7.28, mysql data written to the log file directory host (persistent data)

Download mysql Mirror

Enter the mysql database mirroring select version 5.7.28 of the mirror, and download

docker pull mysql:5.7.28

Start mysql container

1. Start mysql container

docker run --name mysql_test -d -e MYSQL_ROOT_PASSWORD=123456  -p 3307:3306 mysql:5.7.28

 run: Command to start a container

 --name: And the naming container

 -d:Let the container running in the background (corresponding to the other parameters: -iinteractive operation, -tto open a pseudo-terminal)

 -e MYSQL_ROOT_PASSWORD: Initialize the root user's password is 123456

 -p: 3306 mapped to the host port of the container port 3307

 mysql:5.7.28: Mysql mirrored warehouse name and label can also be directly activated by mirroring id

2. Go to the mysql container to view mysql configuration file path

docker exec -it mysql_test sh      # 进入mysql容器
mysql -uroot -p123456              # 进入root账号登录mysql

3. Check the mysql configuration data files and log file path

show variables like 'general_log_file';           # 日志文件路径
show variables like 'log_error';                  # 错误日志文件路径
show variables like 'slow_query_log_file';        # 慢查询日志文件路径
show variables like 'datadir';                    # 查看数据库数据文件路径

View and found the log, slow query log and data files are located in the container / var / lib / mysql, the error log is not configured permissions

15847114301.jpg

That being the case, we will mount the container into an empty directory / var / lib / mysql and modify the configuration file error log files together into this path

Ready to mount directory and file

1. Under the new host to mount directories and log files

sudo mkdir -p /home/docker_data/mysql

Note: The  path must be an absolute path, the host if the directory does not exist, it will automatically generate

2. Modify the owner permission to mount the file / home / docker_data / mysql to 999

sudo chown 999 /home/docker_data/mysql

3. The container mysql_test profile /etc/mysql/mysql.conf.d/mysqld.cnf directory copied to the host / home / test

# 退出容器mysql_test
exit                                                                           
# 复制mysql配置文件mysqld.cnf到宿主机目录/home/test
docker cp mysql_test:/etc/mysql/mysql.conf.d/mysqld.cnf /home/test   

4. Modify Profile path error log / var / lib / mysql

vim /home/test/mysqld.cnf                      # 编辑配置文件mysqld.cnf
#log-error      = /var/log/mysql/error.log     # 修改前
log-error      = /var/lib/mysql/error.log      # 修改后
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

5. Remove the container mysql_test

docker stop mysql_test         # 停止
docker rm mysql_test           # 删除
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

docker start mysql container

1. Start mysql vessel and mount the file to the host

docker run --name mysql_5.7.28 -d -e MYSQL_ROOT_PASSWORD=123456 -v /home/docker_data/mysql:/var/lib/mysql -p 3307:3306 mysql:5.7.28
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

-v: The host directory is mounted to the container ( -v /home/docker_mysql/datadir:/var/lib/mysqlto the host / home / docker_mysql / datadir mounted to the container / var / lib / mysql directory, -v parameter can be repeated several times, each time to mount a directory container)

2. The host profile /home/test/mysqld.cnf /etc/mysql/mysql.conf.d copied to the container mysql_5.7.28

docker cp /home/test/mysqld.cnf  mysql_5.7.28:/etc/mysql/mysql.conf.d
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

3. Restart the container and view the docker container operation

docker ps -a

 

4. docker Log Viewer start case

docker logs -t -f --tail=10 mysql_5.7.28

The Options:
        --details display more information
    -f, --follow real-time tracking log
        --since string display since a log timestamp, or relative time, such as 42m (i.e. 42 minutes)
        the end --tail string from the log log shows how many lines, default All
    -t, --timestamps presentation time stamp
        --until string display from a previous log timestamp, or relative time, such as 42m (i.e. 42 minutes)

example:

View the log after the appointed time, only the last 100 lines:

$ docker logs -f -t --since="2018-02-08" --tail=100 CONTAINER_ID
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

View logs the last 30 minutes:

$ docker logs --since 30m CONTAINER_ID
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

View the log after a certain time:

$ docker logs -t --since="2018-02-08T13:23:37" CONTAINER_ID
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

View a log period of time:

$ docker logs -t --since="2018-02-08T13:23:37" --until "2018-02-09T12:23:37" CONTAINER_ID
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

Note : If the owner permission to mount the file is not set correctly fail to start and report to insufficient permissions (as shown below):

5. navicat connection is successful and a new database -test_2019

Command line or directly connected to the server

 

mysql -h 127.0.0.1 -uroot -p123456 -P3307
或者:mysql -h mysql容器IP -uroot -p123456 -P3306
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

Note: -h designated ip -u specifies the user name -p password -P specify the port number specified

View container IP method

方法一  docker inspect 容器的id或者容器名 | grep IPAddress
方法二  sudo docker inspect -f='{{.NetworkSettings.IPAddress}}' $(sudo docker ps -a -q)
方法三  ps -aux | grep 3307

6. Review the database folder datadir in more than a database file -test_2019, so far deployed mysql

 

 

 

 

Published 59 original articles · won praise 19 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43507959/article/details/103646691