mysql5.6版本基於GTID雙主-docker方式-主庫有數據
阿新 • • 發佈:2019-03-03
bee wait host cti file word global gtid soc #(0)環境規劃
mysql版本:5.6
master01 : 192.168.19.131
master02 : 192.168.19.132
#(1)安裝docker和docker-compose
curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install docker-ce-17.06.0.ce-1.el7.centos.x86_64 -y systemctl enable docker systemctl start docker cat > /etc/docker/daemon.json <<EOF { "registry-mirrors": ["https://ui5lsypg.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker sudo curl -L https://mirrors.aliyun.com/docker-toolbox/linux/compose/1.21.2/docker-compose-Linux-x86_64 > /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose docker-compose --version
#(2)準備目錄
mkdir /mysql3307/{data,conf} -pv
mkdir /temp/docker/compose/ -pv
#(3)master01 和 master02 的配置文件
註意修改server-id和 auto_increment_offset
#cat /mysql3307/conf/my.cnf [mysqld] #basic character-set-server=utf8 lower_case_table_names=1 skip-name-resolve max_connections = 10000 user=mysql port=3306 datadir=/mysql3307/data socket=/var/lib/mysql/mysql.sock pid-file=/var/run/mysql/mysql.pid log_error=/var/log/mysql/error.log default_time_zone = ‘+8:00‘ explicit_defaults_for_timestamp=true #slow log slow_query_log = 1 long_query_time = 1 #lock innodb_lock_wait_timeout=50 lock_wait_timeout=50 innodb_autoinc_lock_mode=2 #innodb innodb_file_per_table=ON innodb_buffer_pool_size=128M #redo log innodb_flush_log_at_trx_commit=1 innodb_support_xa=ON innodb_log_file_size=16M innodb_log_files_in_group=2 innodb_log_group_home_dir=./ #binlog sync_binlog=1 binlog_format=row max_binlog_size=1073741082 log-bin=master01 expire_logs_days = 7 #Replication availability master-info-repository=TABLE relay-log-info-repository=TABLE relay_log_recovery=1 sync_relay_log=1 sync_relay_log_info=1 #flush innodb_io_capacity=200 innodb_flush_neighbors=0 #gtid server-id=1 gtid-mode=on enforce-gtid-consistency=true auto_increment_offset=1 auto_increment_increment=2 log-slave-updates=true
#(4)compose文件
#cat /temp/docker/mysql3307/mysql3307.yaml version: ‘2.2‘ services: mysql3307: hostname: mysql3307 container_name: mysql3307 restart: always image: registry.cn-hangzhou.aliyuncs.com/wangfang-dev/mysql:5.6 ports: - 3307:3306 volumes: - /mysql3307/conf:/etc/mysql/conf.d - /mysql3307/data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: 123456 cpus: 1 mem_limit: 2048m
#(5)先啟動master01和準備一些數據
docker-compose -f /temp/docker/mysql3307/mysql3307.yaml up -d
啟動成功, 進入容器裏面的數據庫先創建一些數據
docker exec -it mysql3307 bash
mysql -uroot -p123456
創建復制和備份賬戶
grant replication slave,replication client on *.* to ‘rep‘@‘%‘ identified by ‘rep‘;
grant reload,process,lock tables,replication client on *.* to backup@‘%‘ identified by ‘backup‘;
#(6)在master01和master02上創建備份目錄和安裝percona備份軟件
mkdir -pv /backup/db3307
yum install epel-release -y
yum install percona-xtrabackup.x86_64 -y
#(7)在master01上進行完整備份
innobackupex --defaults-file=/mysql3307/conf/my.cnf --user=backup --password=backup --host=192.168.19.131 --port=3307 /backup/db3307
同時把備份傳到master02上:scp -r /backup/db3307/2019-03-02_23-13-39/ 192.168.19.132:/backup/db3307/
#(8)在master02上進行恢復備份和啟動mysql容器
docker-compose -f /temp/docker/mysql3307/mysql3307.yaml down
rm -rf /mysql3307/data/*
innobackupex --defaults-file=/mysql3307/conf/my.cnf --apply-log --redo-only /backup/db3307/2019-03-02_23-13-39/
innobackupex --defaults-file=/mysql3307/conf/my.cnf --copy-back /backup/db3307/2019-03-02_23-13-39/
docker-compose -f /temp/docker/mysql3307/mysql3307.yaml up -d
#(9)在master02上查看完整備份的已執行過的gtid
[root@fs02 ~]# cat /backup/db3307/2019-03-02_23-13-39/xtrabackup_binlog_info
master01.000004 2028 97c3fe88-3cfc-11e9-b08a-0242ac120002:1-7147
#(10)在master02上進入容器的mysql, 跳過已執行過的GTID和設置master01作為master02的從
docker exec -it mysql3307 bash
root@mysql3307:/# mysql -uroot -p123456
mysql> reset master;
mysql> set @@global.gtid_purged=‘97c3fe88-3cfc-11e9-b08a-0242ac120002:1-7147‘;
mysql> change master to master_host=‘192.168.19.131‘,master_user=‘rep‘,master_password=‘rep‘,master_port=3307,master_auto_position=1;
mysql> start slave;
主從復制沒有問題
#(11)回到master01上, 配置master02作為master01的從
docker exec -it mysql3307 bash
mysql -uroot -p123456
mysql>change master to master_host=‘192.168.19.132‘,master_user=‘rep‘,master_password=‘rep‘,master_port=3307,master_auto_position=1;
mysql>start slave
主從復制沒有問題
#(12)分別在master01上和master02上創建一些數據進行驗證
mysql5.6版本基於GTID雙主-docker方式-主庫有數據