docker下的mysql主從複製初探

記錄一下本身配置調試的過程python

使用的環境及工具

  • docker-compose,docker-ce
  • navcat
  • vm,ubuntu_server-14.04,mysql-5.7.25

步奏

  • 拉取mysql鏡像
docker pull mysql=5.7
複製代碼
  • 使用docker-compose.yml 編排 容器, 個人配置以下
version: '3'
services:
    db_m:
      image: mysql:5.7
      container_name: master
      ports:
        - "3310:3306"
      environment:
        MYSQL_ROOT_PASSWORD: future
        MYSQL_DATABASE: my_test
        LANG: C.UTF-8
      volumes:
        - ./data/master:/var/lib/mysql
        - ./config/master:/etc/mysql/mysql.conf.d/

      command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
      #restart:always
    db_s:
      image: mysql:5.7
      container_name: slave
      ports:
        - "3311:3306"
      environment:
        MYSQL_ROOT_PASSWORD: future
        MYSQL_DATABASE: my_test
        LANG: C.UTF-8
      volumes:
        - ./data/slave:/var/lib/mysql
        - ./config/slave:/etc/mysql/mysql.conf.d/
      command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
      #restart:always

複製代碼

目的是建立一主一從兩個mysql的容器,將mysql端口分別帶出來,設置好root用戶的密碼以及字符格式,最重要的是 將mysql的data數據映射出來,再映射一個配置文件的目錄mysql

  • 建立容器
docker-compose up -d
複製代碼
  • 容器建立好了以後就要配置2個mysql,個人配置以下

主:sql

[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

# my settings
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION, NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
server-id = 100
log-bin=/var/lib/mysql/mysql-bin.log
binlog-do-db=my_test

複製代碼

從:docker

[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

# my settings
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION, NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
server-id = 101
log-bin=/var/lib/mysql/mysql-bin_slave.log
replicate-do-db=my_test
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60 

複製代碼

須要設置sql_mode,否則有些sql語句不能執行,設置2個mysql的server-id,設置成不一樣的就能夠了,再打開mysql的bin-log.ubuntu

  • 在主庫建立從庫同步的用戶
grant replication client , replication slave on *.* to sla@'%' identified by 'sla_123';
flush privileges;
複製代碼

這裏我是在navcat裏執行的bash

  • 手動同步數據
cp ./data/master ./data/slave
複製代碼

這裏是將保證從庫跟主庫數據文件一致,再查看gtidsocket

show global variables like 'gtid_executed'
複製代碼

在主庫跟從庫中分別執行,檢查gtid是否一致。ide

  • 設置主從關係,在從庫中執行
top slave;
複製代碼
change master to master_host = '192.168.40.132',
master_port = 3310,master_user = 'sla',master_password='sla_123';
複製代碼
start slave;
複製代碼

這裏的master_host 是我虛擬機的ip,port是主庫帶出來的端口工具

驗證

  • 檢查主從關係是否配置成功,驗證主從複製功能是否實現

在從庫中執行ui

show slave status
複製代碼
  • 驗證主從複製功能

在主庫中新建一張表,並添加一些數據,打開從庫,可發現從庫也存在該表,數據也是一致的,此時,docker下的mysql主從複製功能就基本上實現了

結尾

另外喜歡封面圖片的朋友還能夠去這裏看看

此文章僅爲記錄所用 by chuandong

相關文章
相關標籤/搜索