版本說明python
版本以下所示:mysql
本教程全部工具和數據目錄存放於/docker目錄下
linux
安裝dockergit
詳情見docs.docker.com/install/lin…github
安裝docker-composesql
這裏簡單介紹下兩種安裝docker-compose的方式,第一種方式相對簡單,可是因爲網絡問題,經常安裝不上,而且常常會斷開,第二種方式略微麻煩,可是安裝過程比較穩定。docker
方式一數據庫
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
複製代碼
查看版本信息:centos
docker-compose --version
複製代碼
可是此方法會常常由於網絡的緣由而沒法安裝bash
方式二
安裝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
複製代碼
要點說明
簡要說明
**注意:配置完成後須要容許命令chown -R 1000:1000 /docker/mysql**
配置完後須要容許命令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
複製代碼
文件位置爲:/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
複製代碼
配置文件具體位置分別爲:/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
複製代碼
配置文件具體位置分別爲:/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
複製代碼
配置文件具體位置分別爲:/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-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命令行
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-s1的配置
測試master寫入後是否可以同步到slave
#在master的mysql命令行下建立數據庫:mytest
mysql> create database mytest;
#去兩臺slave上查看是否也有了mstest數據庫.
mysql> show databases;
複製代碼