mysql master-slave replication setup

This commit is contained in:
Vladyslav Babak 2017-11-13 23:43:21 +02:00
commit 3de1538f07
12 changed files with 190 additions and 0 deletions

1
.env Normal file
View File

@ -0,0 +1 @@
# default environment variables https://docs.docker.com/compose/env-file/

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.DS_Store
.idea
data

76
README.md Normal file
View File

@ -0,0 +1,76 @@
Docker MySQL master-slave replication
========================
MySQL master-slave replication with using Docker.
## Run
To run this examples you will need to start containers with "docker-compose"
and after starting setup replication. See commands inside ./build.sh.
#### Create 2 MySQL containers with master-slave row-based replication
```
./build.sh
```
#### Make changes to master
```
docker exec mysql_master sh -c "export MYSQL_PWD=111; mysql -u root mydb -e 'create table code(code int); insert into code values (100), (200)'"
```
#### Read changes from slave
```
docker exec mysql_slave sh -c "export MYSQL_PWD=111; mysql -u root mydb -e 'select * from code \G'"
```
## Troubleshooting
#### Check Logs
```
docker-compose logs
```
#### Start containers in "normal" mode
> Go through "build.sh" and run command step-by-step.
#### Check running containers
```
docker-compose ps
```
#### Clean data dir
```
rm -rf ./master/data/*
rm -rf ./slave/data/*
```
#### Run command inside "mysql_master"
```
docker exec mysql_master sh -c 'mysql -u root -p111 -e "SHOW MASTER STATUS \G"'
```
#### Run command inside "mysql_slave"
```
docker exec mysql_slave sh -c 'mysql -u root -p111 -e "SHOW SLAVE STATUS \G"'
```
#### Enter into "mysql_master"
```
docker exec -it mysql_master bash
```
#### Enter into "mysql_slave"
```
docker exec -it mysql_slave bash
```

38
build.sh Executable file
View File

@ -0,0 +1,38 @@
#!/bin/bash
docker-compose down
rm -rf ./master/data/*
rm -rf ./slave/data/*
docker-compose build
docker-compose up -d
until docker exec mysql_master sh -c 'export MYSQL_PWD=111; mysql -u root -e ";"'
do
echo "Waiting for mysql_master database connection..."
sleep 4
done
priv_stmt='GRANT REPLICATION SLAVE ON *.* TO "mydb_slave_user"@"%" IDENTIFIED BY "mydb_slave_pwd"; FLUSH PRIVILEGES;'
docker exec mysql_master sh -c "export MYSQL_PWD=111; mysql -u root -e '$priv_stmt'"
until docker-compose exec mysql_slave sh -c 'export MYSQL_PWD=111; mysql -u root -e ";"'
do
echo "Waiting for mysql_slave database connection..."
sleep 4
done
docker-ip() {
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$@"
}
MS_STATUS=`docker exec mysql_master sh -c 'export MYSQL_PWD=111; mysql -u root -e "SHOW MASTER STATUS"'`
CURRENT_LOG=`echo $MS_STATUS | awk '{print $6}'`
CURRENT_POS=`echo $MS_STATUS | awk '{print $7}'`
start_slave_stmt="CHANGE MASTER TO MASTER_HOST='$(docker-ip mysql_master)',MASTER_USER='mydb_slave_user',MASTER_PASSWORD='mydb_slave_pwd',MASTER_LOG_FILE='$CURRENT_LOG',MASTER_LOG_POS=$CURRENT_POS; START SLAVE;"
start_slave_cmd='export MYSQL_PWD=111; mysql -u root -e "'
start_slave_cmd+="$start_slave_stmt"
start_slave_cmd+='"'
docker exec mysql_slave sh -c "$start_slave_cmd"
docker exec mysql_slave sh -c "export MYSQL_PWD=111; mysql -u root -e 'SHOW SLAVE STATUS \G'"

35
docker-compose.yml Normal file
View File

@ -0,0 +1,35 @@
version: '3'
services:
mysql_master:
# image: mysql:5.7
build:
context: ./master
env_file:
- ./master/mysql_master.env
container_name: "mysql_master"
restart: "no"
ports:
- 4406:4406
volumes:
- ./master/conf/mysql.conf.cnf:/etc/mysql/conf.d/mysql.conf.cnf
- ./master/data:/var/lib/mysql
networks:
- overlay
mysql_slave:
image: mysql:5.7
env_file:
- ./slave/mysql_slave.env
container_name: "mysql_slave"
restart: "no"
ports:
- 5506:5506
volumes:
- ./slave/conf/mysql.conf.cnf:/etc/mysql/conf.d/mysql.conf.cnf
- ./slave/data:/var/lib/mysql
networks:
- overlay
networks:
overlay:

5
master/Dockerfile Normal file
View File

@ -0,0 +1,5 @@
# see https://hub.docker.com/_/mysql/
FROM mysql:5.7
# RUN mysql -u root -e "GRANT REPLICATION SLAVE ON *.* TO 'mydb_slave_user'@'%' IDENTIFIED BY 'mydb_slave_pwd'; FLUSH PRIVILEGES;"

View File

@ -0,0 +1,9 @@
[mysqld]
skip-host-cache
skip-name-resolve
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_format = ROW
binlog_do_db = mydb

0
master/data/.gitkeep Normal file
View File

7
master/mysql_master.env Normal file
View File

@ -0,0 +1,7 @@
# Note, by default mysql root does not have a password. You need to restart a server to bring MYSQL_ROOT_PASSWORD working. Use "docker-compose restart" command.
MYSQL_ROOT_PASSWORD=111
MYSQL_PORT=4406
MYSQL_USER=mydb_user
MYSQL_PASSWORD=mydb_pwd
MYSQL_DATABASE=mydb
MYSQL_LOWER_CASE_TABLE_NAMES=0

View File

@ -0,0 +1,9 @@
[mysqld]
skip-host-cache
skip-name-resolve
server-id=2
relay-log = /var/log/mysql/mysql-relay-bin.log
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = mydb

0
slave/data/.gitkeep Normal file
View File

7
slave/mysql_slave.env Normal file
View File

@ -0,0 +1,7 @@
# Note, by default mysql root does not have a password. You need to restart a server to bring MYSQL_ROOT_PASSWORD working. Use "docker-compose restart" command.
MYSQL_ROOT_PASSWORD=111
MYSQL_PORT=5506
MYSQL_USER=mydb_slave_user
MYSQL_PASSWORD=mydb_slave_pwd
MYSQL_DATABASE=mydb
MYSQL_LOWER_CASE_TABLE_NAMES=0