docker-compase搭建mysql主从复制

准备工作

  1. 版本说明

    版本如下所示:

    • mysql:5.7.22

    本教程所有工具和数据目录存放于/docker目录下

  2. 安装docker

    详情见docs.docker.com/install/lin…

  3. 安装docker-compose

    这里简单介绍下两种安装docker-compose的方式,第一种方式相对简单,但是由于网络问题,常常安装不上,并且经常会断开,第二种方式略微麻烦,但是安装过程比较稳定。

    • 方式一

      curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
      chmod +x /usr/local/bin/docker-compose
      复制代码

      查看版本信息:

      docker-compose --version
      复制代码

      但是此方法会经常因为网络的原因而无法安装

    • 方式二

      安装python-pip

        ```
        yum -y install epel-release
        yum -y install python-pip
        ```
      复制代码

      安装docker-compose

        ```
        pip install docker-compose
        ```
      复制代码

      待安装完成后,执行查询版本的命令,即可安装docker-compose

    • 安装完成如下所示:

      [root@VM_0_14_redhat ~]# docker-compose version
      docker-compose version 1.21.0, build 5920eb0
      docker-py version: 3.2.1
      CPython version: 2.7.5
      OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
      复制代码

    ps:参考www.ilanni.com/?p=13371

环境说明

  1. 要点说明

    • 使用docker bridge网络,设置静态IP
    • 挂载hosts文件,以便于使用hostname代替ip地址
  2. 简要说明

    • /docker/mysql/data/mysql-m1: 存放master配置文件
    • /docker/mysql/data/mysql-s1: 存放第一个slave配置文件
    • /docker/mysql/data/mysql-s2: 存放第二个slave配置文件
    • /docker/mysql/config/hosts: 本地路由

    **注意:配置完成后需要允许命令chown -R 1000:1000 /docker/mysql**

docker-compase.yml编写

配置完后需要允许命令chown -R 1000:1000 /docker/mysql以及chmod -R 777 /docker/mysql

version: '2'
services:
  mysql-m1:
    image: mysql:5.7.22
    container_name: mysql-m1 #容器名
    volumes:
      - /docker/mysql/data/mysql-m1/:/var/lib/mysql
      - /docker/mysql/config/mysql-m1/my.cnf:/etc/my.cnf 
      - /docker/mysql/config/hosts:/etc/hosts:ro
    ports:
      - "3406:3306"
    networks: #网络
      study_net: #见跟services平级的networks,在最下边
        ipv4_address: 192.168.8.2 #设置静态ipv4的地址
    ulimits:
      nproc: 65535
    hostname: mysql-m1
    mem_limit: 256m #最大内存使用不超过1024m,
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: {your password}
  mysql-s1:
    image: mysql:5.7.22
    container_name: mysql-s1
    volumes:
      - /docker/mysql/data/mysql-s1/:/var/lib/mysql
      - /docker/mysql/config/mysql-s1/my.cnf:/etc/my.cnf 
      - /docker/mysql/config/hosts:/etc/hosts:ro
    ports:
        - "3407:3306"
    networks:
      study_net:
        ipv4_address: 192.168.8.3
    links:
        - mysql-m1
    ulimits:
        nproc: 65535
    hostname: mysql-s1
    mem_limit: 256m
    restart: always
    environment:
        MYSQL_ROOT_PASSWORD: {your password}
  mysql-s2:
    image: mysql:5.7.22
    container_name: mysql-s2
    volumes:
      - /docker/mysql/data/mysql-s2/:/var/lib/mysql
      - /docker/mysql/config/mysql-s2/my.cnf:/etc/my.cnf 
      - /docker/mysql/config/hosts:/etc/hosts:ro
    ports:
      - "3408:3306"
    links:
      - mysql-m1
    networks:
      study_net:
        ipv4_address: 192.168.8.4
    ulimits:
      nproc: 65535
    hostname: mysql-s2
    mem_limit: 256m
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: {your password}
networks: # docker网络设置
  study_net: # 自定义网络名称
    ipam: # 要使用静态ip必须使用ipam插件
      driver: default
      config:
      - subnet: 192.168.8.0/24
复制代码

hosts文件配置

文件位置为:/docker/mysql/config/hosts

127.0.0.1   localhost
192.168.8.2  mysql-m1
192.168.8.3  mysql-s1
192.168.8.4  mysql-s2
复制代码

mysql配置文件

  1. mysql-m1配置

配置文件具体位置分别为:/docker/mysql/config/mysql-m1/my.cnf

配置如下:

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
lower_case_table_names=1
#给数据库服务的唯一标识,一般为大家设置服务器Ip的末尾号
server-id=2
log-bin=master-bin
log-bin-index=master-bin.index
复制代码
  1. mysql-s1配置

配置文件具体位置分别为:/docker/mysql/config/mysql-s1/my.cnf

配置如下:

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=3
log-bin=s1-bin.log
sync_binlog=1
lower_case_table_names=1
复制代码
  1. mysql-s2配置

配置文件具体位置分别为:/docker/mysql/config/mysql-s1/my.cnf

配置如下:

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id=4
log-bin=s2-bin.log
sync_binlog=1
lower_case_table_names=1
复制代码

设置mysql主从复制

  1. 配置mysql-m1

进入mysql-m1的mysql命令行

docker exec -it mysql-m1 /bin/bash

mysql -u root -p
#输入MYSQL_ROOT_PASSWORD:的值,进入mysql命令行模式.

mysql> create user repl;

#给repl用户授予slave的权限
#repl用户必须具有REPLICATION SLAVE权限,除此之外没有必要添加不必要的权限,密码为repl。说明一下192.168.8.%,这个配置是指明repl用户所在服务器,这里%是通配符,表示192.168.8.0-192.168.8.255的Server都可以以repl用户登陆主服务器。当然你也可以指定固定Ip。
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.8.%' IDENTIFIED BY 'repl';

mysql> flush privileges;

#锁库,不让数据再进行写入动作,这个命令在结束终端会话的时候会自动解锁
mysql> FLUSH TABLES WITH READ LOCK;

#查看master状态
mysql> show master status;

#记下master-bin.000003和636一会在slave中要用.

复制代码
  1. 配置mysql-s1

进入mysql-s1的mysql命令行

docker exec -it mysql-s1 /bin/bash

mysql -u root -p
#输入MYSQL_ROOT_PASSWORD:的值,进入mysql命令行模式.

#连接master
mysql> change master to master_host='mysql-m1',master_port=3306,master_user='repl',master_password='repl',master_log_file='master-bin.000003',master_log_pos=636;

#启动slave
mysql> start salve;

复制代码
  1. 配置mysql-s2

参照mysql-s1的配置

验证环境

测试master写入后是否能够同步到slave

#在master的mysql命令行下创建数据库:mytest
mysql> create database mytest;

#去两台slave上查看是否也有了mstest数据库.
mysql> show databases;

复制代码

你可能感兴趣的:(docker-compase搭建mysql主从复制)