Docker mysql 主从

时间:2022-09-01 12:21:57

一、独立容器部署mysql主从 

# 主从 my.cnf加上

[mysqld]
server-id = XXX
log-bin = mysql-bin
log-bin-index = log-bin.index 

  (1)master

docker run -d \
--name mysql_master \
-p 3310:3306 \
-v /data/conf/master-slave/master.cnf:/etc/mysql/my.cnf \
-v /data/conf/master-slave/localtime:/etc/localtime \
-v /data/storage/cluster_data/master_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=Password4Root \
mysql

  (2)slave 

docker run -d \
--name mysql_slave \
-p 3311:3306 \
-v /data/conf/master-slave/slave.cnf:/etc/mysql/my.cnf \
-v /data/conf/master-slave/localtime:/etc/localtime \
-v /data/storage/cluster_data/slave_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=Password4Root \
--link mysql_master:master \
mysql

  (3)查看 

docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dc5f46aa135b mysql "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3311->3306/tcp mysql_slave
be3e1af663cd mysql "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3310->3306/tcp mysql_master

  (4)主库Replication设置

# 登入主库
docker exec -it mysql_master mysql -uroot -pPassword4Root mysql> grant all privileges on *.* to 'repl'@'%' identified by 'repl_password';
Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 434
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

  (5)从库Replication设置

# 登入从库
docker exec -it mysql_slave mysql -uroot -pPassword4Root # 设置主从
mysql> change master to
-> master_host='master',
-> master_user='repl',
-> master_password='repl_password',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=434; # 启动slave
mysql> start slave; # 查看状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: master
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 434
Relay_Log_File: 5925cc301b6b-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:

  Docker mysql 主从

  

  (6)测试

  主库新建数据库

  Docker mysql 主从

  从库查看

  Docker mysql 主从

  尝试重启mysql_master容器,slave状态变为connecting

  Docker mysql 主从

  重启完成后,状态恢复正常

  Docker mysql 主从

二、Docker Swarm mysql主从

  (1)创建secret文件

# echo "Password4Root" | docker secret create root_cluster_password -
w4wv4askqam5znua5032gm8vt

  (2)使用swarm配置管理

  master.cnf和slave.cnf要存在

# 创建主从配置文件
[root@manager master-slave]# docker config create master.cnf master.cnf
t1rjlq7djtulxzl7gppf3rrum
[root@manager master-slave]# docker config create slave.cnf slave.cnf
u229zquvbevbq50k92ylqcgqn

  (3)创建overlay网络  

# docker network create -d overlay mysql-cluster --subnet 10.10.2.0/24
zf4n8waxc283uprq8eppx2gzp

  (4)创建服务 

# master
[root@manager ~]# docker service create \
> --name mysql_master \
> --replicas 1 \
> --network mysql-cluster \
> --secret root_cluster_password \
> --config src=master.cnf,target=/etc/mysql/my.cnf \
> -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/root_cluster_password \
> --mount type=volume,src=master_data,dst=/var/lib/mysql \
> --publish 3320:3306 \
> mysql
nuthmf6fdpmjmew1pzhdninmg
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged # slave
[root@manager ~]# docker service create \
> --name mysql_slave \
> --replicas 1 \
> --network mysql-cluster \
> --secret root_cluster_password \
> --config src=slave.cnf,target=/etc/mysql/my.cnf \
> --mount type=volume,src=slave_data,dst=/var/lib/mysql \
> -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/root_cluster_password \
> --publish 3321:3306 \
> mysql
pyed0j0jlijy1upbyicuayvko
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged

  

[root@manager ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
nuthmf6fdpmj mysql_master replicated 1/1 mysql:latest *:3320->3306/tcp
pyed0j0jlijy mysql_slave replicated 1/1 mysql:latest *:3321->3306/tcp 

  (5)主从配置

# 登入master所在节点的主库

mysql> grant all privileges on *.* to 'repl'@'%' identified by 'repl_password';
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 434
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec) # 登入slave所在节点从库
mysql> change master to
-> master_host='mysql_master',
-> master_user='repl',
-> master_password='repl_password',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=434;
Query OK, 0 rows affected, 2 warnings (0.29 sec) mysql> start slave;
Query OK, 0 rows affected (0.01 sec

  查看状态

  Docker mysql 主从

  (6)测试验证

  主库创建数据库

  Docker mysql 主从

  查看从库

  Docker mysql 主从

  stop 主库(在swarm集群中,关停的容器默认会自动重启)

  Docker mysql 主从

  重启完成后恢复正常

  Docker mysql 主从

三、服务编排部署mysql主从

  (1)mysql_replications.yml  

version: "3.3"
services:
master:
image: mysql:latest
networks:
- rep_net
ports:
- "3330:3306"
volumes:
- type: volume
source: master-data
target: /var/lib/mysql
volume:
nocopy: true
configs:
- source: localtime
target: /etc/localtime
- source: master.cnf
target: /etc/mysql/my.cnf
deploy:
replicas: 1
restart_policy:
condition: on-failure
resources:
limits:
cpus: '0.8'
memory: 500M
reservations:
cpus: '0.25'
memory: 200M
secrets:
- root_password
environment:
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/root_password slave:
image: mysql:latest
networks:
- rep_net
ports:
- "3331:3306"
volumes:
- type: volume
source: slave-data
target: /var/lib/mysql
volume:
nocopy: true
configs:
- source: localtime
target: /etc/localtime
- source: slave.cnf
target: /etc/mysql/my.cnf
deploy:
replicas: 1
restart_policy:
condition: on-failure
resources:
limits:
cpus: '0.8'
memory: 500M
reservations:
cpus: '0.25'
memory: 200M
secrets:
- root_password
environment:
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/root_password secrets:
root_password:
external: true configs:
localtime:
external: true
master.cnf:
external: true
slave.cnf:
external: true volumes:
master-data:
slave-data:
networks:
rep_net:
driver: overlay

  (2)部署服务

# docker stack deploy -c mysql_replications.yml repl
Creating network repl_rep_net
Creating service repl_master
Creating service repl_slave

  (3)查看服务

[root@manager stack_compose]# docker service ls
ID NAME MODE REPLICAS IMAGE
khfpkq81op7b repl_master replicated 1/1 mysql:latest *:3330->3306/tcp
u7k4enpy1eru repl_slave replicated 1/1 mysql:latest *:3331->3306/tcp

  (4)配置主从

# master

# docker exec -it fb85153f27ac mysql -uroot -pPassword4Root

mysql> grant all privileges on *.* to 'repl'@'%' identified by 'repl_password';
Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 434
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec) # slave mysql> change master to
-> master_host='master',
-> master_user='repl',
-> master_password='repl_password',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=434;
Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave;
Query OK, 0 rows affected (0.00 sec mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: master
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 434
Relay_Log_File: 0ddecac560e1-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
.....
.....

  主从正常

Docker mysql 主从的更多相关文章

  1. Docker Mysql主从同步配置搭建

    Docker Mysql主从同步配置搭建 建立目录 在虚拟机中建立目录,例如路径/home/mysql/master/data,目录结构如下: Linux中 新建文件夹命令:mkdir 文件夹名 返回 ...

  2. Docker Mysql主从同步配置搭建Demo

    进行Docker操作前,先建立目录,我的路径是d:/docker/mysql,目录结构如下: --mysql --master --data --conf --my.cnf --slaver --da ...

  3. django 本地项目部署uwsgi 以及云服务器部署 uwsgi+Nginx+Docker+MySQL主从

    一 .django 本地项目部署uwsgi 1 本地部署项目 uwsgi安装测试 通过uwsgi 进行简单部署 安装uwsgi命令:pip install uwsgi -i http://pypi.d ...

  4. docker mysql 主从配置

    docker安装运行单实例的MySQL参考另一篇文档 http://www.cnblogs.com/manger/p/7611309.html 1.首先在/data/script下创建两个文件my-m ...

  5. Docker Mysql数据库主从同步配置方法

    一.背景 最近在做内部平台架构上的部署调整,顺便玩了一下数据库的主从同步,特此记录一下操作- 二.具体操作 1.先建立数据存放目录(-/test/mysql_test/) --mysql --mast ...

  6. Docker - Docker中搭建MySQL主从

    1.pull完centos7纯净版的镜像后,创建容器,然后将宿主机上下载的MySQL文件 (MySQL下载地址:http://mysql.mirror.kangaroot.net/Downloads/ ...

  7. 这次一定要教会你搭建Redis集群和MySQL主从同步(非Docker)

    前言 一直都想自己动手搭建一个Redis集群和MySQL的主从同步,当然不是依靠Docker的一键部署(虽然现在企业开发用的最多的是这种方式),所以本文就算是一个教程类文章吧,但在动手搭建之前,会先聊 ...

  8. 使用docker 实现MySQL主从同步/读写分离

    1. 利用 docker 实现 mysql 主从同步 / 读写分离 为了保证数据的完整和安全,mysql 设计了主从同步,一个挂掉还可以用另个.最近重构论坛,想来改成主从吧.担心失误,就先拿 dock ...

  9. docker运行mysql主从备份,读写分离

    1)从Docker官方下拉MySQL的image 打开https://hub.docker.com/ 搜索mysql 在docker中运行 默认tag为latest docker pull mysql ...

随机推荐

  1. git入门网站

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git入门教程:对商业的.开源的. ...

  2. Salesforce.com Object Query Language (SOQL) 示例

    Salesforce 中的用户操作 打开按公司名称排序的供应商记录列表视图.SOQL 查询 SELECT CompanyName__c,ContactName__c FROM Suppliers__x ...

  3. HYSBZ 2145	悄悄话

    2145: 悄悄话 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 271  Solved: 104[Submit][Status][Discuss] ...

  4. Openstack Grizzily 单节点测试机安装( All In One CentOS/RHEL)

    Openstack Grizzily版本已经相当完善,根据官方文档安装基本不存在什么问题,但是想快速测试了解Openstack功能的用户非常多,devstack的安 装需要check最新的代码,时常碰 ...

  5. Android版本铎A梦幻连连看游戏源代码完整版

    我写主代码,没有版权问题,它少量小游戏和应用源代码稍后会陆续上线!哆啦A梦连连看包含了2种游戏模式和60关卡,并能够通过改动配置文件非常方便的实现自行添加新关卡.採用andengine游戏引擎开发,内 ...

  6. POJ1135_Domino Effect(最短)

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8224   Accepted: 2068 Des ...

  7. 设置控件Enable=false,控件颜色不变

    [System.Runtime.InteropServices.DllImport("user32.dll ")] public static extern int SetWind ...

  8. 《安卓网络编程》之第七篇 java编写Service程序

    import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net. ...

  9. 无废话XML--XML约束(DTD)

    基本术语     一.序言Prolog:包括XML声明(XML Declaration)和文档类型声明(Document Type Declaration).     二.良构(well-formed ...

  10. [HNOI 2001]求正整数

    Description 对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m.例如:n=4,则m=6,因为6有4个不同整数因子1,2,3,6:而且是最小的有4个因子的整数. Input n ...