Docker mysql 主从
一、独立容器部署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:
(6)测试
主库新建数据库
从库查看
尝试重启mysql_master容器,slave状态变为connecting
重启完成后,状态恢复正常
二、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
查看状态
(6)测试验证
主库创建数据库
查看从库
stop 主库(在swarm集群中,关停的容器默认会自动重启)
重启完成后恢复正常
三、服务编排部署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 主从的更多相关文章
- Docker Mysql主从同步配置搭建
Docker Mysql主从同步配置搭建 建立目录 在虚拟机中建立目录,例如路径/home/mysql/master/data,目录结构如下: Linux中 新建文件夹命令:mkdir 文件夹名 返回 ...
- Docker Mysql主从同步配置搭建Demo
进行Docker操作前,先建立目录,我的路径是d:/docker/mysql,目录结构如下: --mysql --master --data --conf --my.cnf --slaver --da ...
- django 本地项目部署uwsgi 以及云服务器部署 uwsgi+Nginx+Docker+MySQL主从
一 .django 本地项目部署uwsgi 1 本地部署项目 uwsgi安装测试 通过uwsgi 进行简单部署 安装uwsgi命令:pip install uwsgi -i http://pypi.d ...
- docker mysql 主从配置
docker安装运行单实例的MySQL参考另一篇文档 http://www.cnblogs.com/manger/p/7611309.html 1.首先在/data/script下创建两个文件my-m ...
- Docker Mysql数据库主从同步配置方法
一.背景 最近在做内部平台架构上的部署调整,顺便玩了一下数据库的主从同步,特此记录一下操作- 二.具体操作 1.先建立数据存放目录(-/test/mysql_test/) --mysql --mast ...
- Docker - Docker中搭建MySQL主从
1.pull完centos7纯净版的镜像后,创建容器,然后将宿主机上下载的MySQL文件 (MySQL下载地址:http://mysql.mirror.kangaroot.net/Downloads/ ...
- 这次一定要教会你搭建Redis集群和MySQL主从同步(非Docker)
前言 一直都想自己动手搭建一个Redis集群和MySQL的主从同步,当然不是依靠Docker的一键部署(虽然现在企业开发用的最多的是这种方式),所以本文就算是一个教程类文章吧,但在动手搭建之前,会先聊 ...
- 使用docker 实现MySQL主从同步/读写分离
1. 利用 docker 实现 mysql 主从同步 / 读写分离 为了保证数据的完整和安全,mysql 设计了主从同步,一个挂掉还可以用另个.最近重构论坛,想来改成主从吧.担心失误,就先拿 dock ...
- docker运行mysql主从备份,读写分离
1)从Docker官方下拉MySQL的image 打开https://hub.docker.com/ 搜索mysql 在docker中运行 默认tag为latest docker pull mysql ...
随机推荐
- ubuntu14.04 安装系统/搜狗/QT/qq/wps/CAJviewer
1.安装ubuntu系统 http://jingyan.baidu.com/album/4dc40848491fc5c8d946f1b1.html?picindex=1 官方网站: ht ...
- css背景图充满屏幕
代码: body { /* 加载背景图 */ background: url(resource/inv_bg.png); /* 背景图不平铺 */background-repeat: no-repea ...
- Mysql: Specified key was too long; max key length is 1000 bytes
在使用quartz持久化的时候,笔者使用的mysql,为了以后方便迁移数据,笔者的Mysql默认引擎MyISAM 于是顺理成章的执行了quartz-2.2.3\docs\dbTables\tables ...
- (转载)Ubuntu下安装Qt
转载自:http://blog.csdn.net/timidsmile/article/details/7716201 Ubuntu下安装Qt 1: sudo apt-get install qt4- ...
- 函数 day9
一,什么是函数? 函数的定义与调用 s = 'fkdsagadfdsagfdsagg' count = 0 for i in s: count += 1 print(count) l1 = [1,2, ...
- ASCII、Unicode和UTF-8编码的区别;中英文混合截取
ASCII编码是128个字符 中国把汉字编入GB2312,Shift_JIS/Euc-kr各国标准..... Unicode是为了解决各国乱码的,但浪费存储空间 UTF-8编码把一个Unicode字符 ...
- kafka配置简要描述
配置文件在config/server.properties 下面的一些配置可能是你需要进行修改的. 这里为官方提供配置文件解释:https://kafka.apache.org/08/configur ...
- Eclipse新建JSP文件的默认编码
默认情况下,Eclipse新建的JSP文件的编码是“ISO-8859-1”,不支持中文.需要手动修改为“UTF-8” 以下设置可使Eclipse生成的JSP文件的默认编码为“UTF-8” Window ...
- [Python]Python章1 Python中_的故事
_xx 单下划线开头 Python中没有真正的私有属性或方法,可以在你想声明为私有的方法和属性前加上单下划线,以提示该属性和方法不应在外部调用.如果真的调用了也不会出错,但不符合规范. 本文为译文,版 ...
- Mockplus3.5.0.1新增标注功能
Mockplus3.5.0.1版本中,新增了标注功能.多种标注模式,智能生成,随时查看.原型设计效率更高. Mockplus的标注功能有以下四种模式: 1.无选中标注 在未选中任何组件时,按住Ctrl ...