一、独立容器部署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 主从的更多相关文章

  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. [z]spring boot gradle build

    I had the same problem. I believe it is caused by the JRE that gradle is configured to use rather th ...

  2. SqlServer添加触发器不让删除数据

    触发器是:instead of delete 类型,注意了:instead类型的触发器相当于: DELETE命令过来后,直接走触发器中的代码,再往下,没有了…… 就是说,这个触发器会屏蔽掉你所有的DE ...

  3. JS-事件心得

    写在前面的话:就我目前的水平来看,这两种方法不能一起使用,用on添加的事件removeEventListener()没办法删除,反之一样 注册事件的两种方式: on+事件名称 addEventList ...

  4. 796B Find The Bone

    B. Find The Bone time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. appache压力测试

    apache自带压力工具测试说明: Usage: ab [options] [http[s]://]hostname[:port]/pathOptions are: -n requests Numbe ...

  6. Codeforces 791C. Bear and Different Names 模拟构造

    C. Bear and Different Names time limit per test:1 second memory limit per test:256 megabytes input:s ...

  7. MySQL学习笔记-数据库后台线程

    数据库后台线程 默认情况下讲述的InnoDB存储引擎,以后不再重复声明.后台线程有7个--4个IO thread,1个master thread,1个锁监控线程,1个错误监控线程.IO thread的 ...

  8. How to Change MAC Address on Ubuntu

    1 Open Terminal.   2 Log in as root so type: sudo -i and then write your password.   3 View your cur ...

  9. Increase PHP script execution time with Nginx

    If you have a large WordPress setup or a server with limited resources, then you will often see the ...

  10. 微信JSSDK接口previewImage

    <div class="pics"> <img src="http://pic1.ytqmx.com:82/2015/0409/01/15.jpg!96 ...