docker实现MySQL数据同步的方法

本站所有内容来自互联网收集,仅供学习和交流,请勿用于商业用途。如有侵权、不妥之处,请第一时间联系我们删除!Q群:迪思分享

免费资源网 – https://freexyz.cn/

大家好,今天分享一下docker的一个实战

我们使用docker 实现MySQL 的数据同步

搜索mysql 镜像

[root@localhost ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 12222 [OK] mariadb MariaDB Server is a high performing open sou… 4691 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 907 [OK] percona Percona Server is a fork of the MySQL relati… 571 [OK] phpmyadmin phpMyAdmin – A web interface for MySQL and M… 465 [OK] mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 93 centos/mysql-57-centos7 MySQL 5.7 SQL database server 92 bitnami/mysql Bitnami MySQL Docker Image 64 [OK] databack/mysql-backup Back up mysql databases to… anywhere! 56 circleci/mysql MySQL is a widely used, open-source relation… 25 ubuntu/mysql MySQL open source fast, stable, multi-thread… 24 mysql/mysql-router MySQL Router provides transparent routing be… 23 centos/mysql-56-centos7 MySQL 5.6 SQL database server 22 arey/mysql-client Run a MySQL client from a docker container 20 [OK] google/mysql MySQL server for Google Compute Engine 19 [OK] mysqlboy/mydumper mydumper for mysql logcial backups 3 mysqlboy/docker-mydumper docker-mydumper containerizes MySQL logical … 3 bitnami/mysqld-exporter 2 ibmcom/mysql-s390x Docker image for mysql-s390x 1 mirantis/mysql 0 mysql/mysql-operator MySQL Operator for Kubernetes 0 mysqlboy/elasticsearch 0 mysqleatmydata/mysql-eatmydata 0 cimg/mysql 0 ibmcom/tidb-ppc64le TiDB is a distributed NewSQL database compat… 0 [root@localhost ~]#

拉取mysql 镜像 (我们在这里指定5.7这个版本)

[root@localhost ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql 72a69066d2fe: Pull complete 93619dbc5b36: Pull complete 99da31dd6142: Pull complete 626033c43d70: Pull complete 37d5d7efb64e: Pull complete ac563158d721: Pull complete d2ba16033dad: Pull complete 0ceb82207cd7: Pull complete 37f2405cae96: Pull complete e2482e017e53: Pull complete 70deed891d42: Pull complete Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7 [root@localhost ~]#

查看本地镜像

[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat04 1.0 12b32b1a124d 18 hours ago 685MB nginx latest 605c77e624dd 2 months ago 141MB tomcat 9.0 b8e65a4d736d 2 months ago 680MB tomcat latest fb5657adc892 2 months ago 680MB mysql 5.7 c20987f18b13 2 months ago 448MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB centos latest 5d0da3dc9764 5 months ago 231MB [root@localhost ~]#

运行一下mysql 容器,做内外网的端口映射,做容器与Linux 主机上的挂载 ,然后给它设密码123456 ,后面加上操作的镜像名

[root@localhost ~]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 –name mysql01 mysql:5.7 f3248538d690f67fa057b0b9e00dd060b1ce40378171de0a9f3d9ebd478111d2 [root@localhost ~]#

显示mysql 容器正在运行中

[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f3248538d690 mysql:5.7 “docker-entrypoint.s…” 4 minutes ago Up 4 minutes 33060/tcp, 0.0.0.0:3310->3306/tcp, :::3310->3306/tcp mysql01 6d6fa6a4c6d7 5d0da3dc9764 “/bin/bash” 25 hours ago Up 25 hours nice_panini [root@localhost ~]#

使用数据库管理工具sql yog 连接我们的数据库

docker实现MySQL数据同步的方法插图

查看服务器ip地址

[root@localhost ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.12 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::617f:ef93:80d4:e14e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:87:af:a6 txqueuelen 1000 (Ethernet) RX packets 2460532 bytes 2826366757 (2.6 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1198876 bytes 243212283 (231.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]#

这里,

命名为容器,

地址:192.168.1.12 (服务器IP地址)

密码:123456 (我们刚才在docker mysql 容器当中设置的密码)

端口:3310 (这是我们刚才做的端口)

docker实现MySQL数据同步的方法插图1

连接成功:

docker实现MySQL数据同步的方法插图2

就是说,我们使用现在已经可以通过使用数据库管理工具sql yog 连接与使用我们的数据库

这个效果和实际本质与我们平时使用的数据库是一样的

在Linux主机上查看/home 下的文件 (注意,多了一个mysql)

[root@localhost ~]# cd /home/ [root@localhost home]# ls 456.txt admin ceshi daqing mysql zhangsan [root@localhost home]#

映射的配置文件与数据文件

[root@localhost home]# cd mysql/ [root@localhost mysql]# ls conf data [root@localhost mysql]#

这就是我们的数据文件

[root@localhost mysql]# cd data/ [root@localhost data]# ls auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem [root@localhost data]#

正式连接数据库 (192.168.1.12)

docker实现MySQL数据同步的方法插图3

在我们MySQL中加一个库

docker实现MySQL数据同步的方法插图4

在Linux上 ,在映射目录的数据文件多了一个帅哥

[root@localhost data]# ls auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem shuaige ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem sys [root@localhost data]#

删除mysql这个容器

[root@localhost data]# docker rm -f mysql01 mysql01 [root@localhost data]#

映射的数据文件还在,

[root@localhost data]# ls auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem shuaige ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem sys [root@localhost data]#

即,在mysql容器删除之后,数据没有丢失,实现了持久化

这就是docker 容器数据卷的好处

免费资源网 – https://freexyz.cn/


© 版权声明
THE END
★喜欢这篇文章吗?喜欢的话,麻烦动动手指支持一下!★
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容