準備工作
-
版本說明
版本如下所示:
- mysql:5.7.22
本教程所有工具和資料目錄存放於/docker目錄下
-
安裝docker
-
安裝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 複製程式碼
-
環境說明
-
要點說明
- 使用docker bridge網路,設定靜態IP
- 掛載hosts檔案,以便於使用hostname代替ip地址
-
簡要說明
- /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配置檔案
- 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
複製程式碼
- 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
複製程式碼
- 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主從複製
- 配置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中要用.
複製程式碼
- 配置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;
複製程式碼
- 配置mysql-s2
參照mysql-s1的配置
驗證環境
測試master寫入後是否能夠同步到slave
#在master的mysql命令列下建立資料庫:mytest
mysql> create database mytest;
#去兩臺slave上檢視是否也有了mstest資料庫.
mysql> show databases;
複製程式碼