Skip to content

Commit 04cbfc7

Browse files
authored
Merge pull request #1 from thanhtaivtt/master
first version
2 parents 133e185 + bc8bf8b commit 04cbfc7

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

.env.example

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
MYSQL_DATABASE=test_db
2+
MYSQL_ROOT_PASSWORD=123456
3+
MYSQL_USER=test_user
4+
MYSQL_PASSWORD=123456
5+
6+
SUBNET=172.30.0.0/16
7+
8+
MASTER_PORT=3311
9+
MASTER_IP=172.30.0.2
10+
11+
SLAVE_PORT=3309
12+
SLAVE_IP=172.30.0.3

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env
2+
3+
.idea/*

app/.gitignore

Whitespace-only changes.

config/master/cus.cnf

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mysqld]
2+
bind-address = 172.30.0.2
3+
server-id = 1
4+
5+
log-bin
6+
binlog-format=row

config/slave/cus.cnf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[mysqld]
2+
bind-address = 172.30.0.3
3+
server-id = 2
4+
5+
log-bin
6+
binlog-format=row
7+
transaction-isolation=read-committed

docker-compose.yml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
version: '3'
2+
services:
3+
4+
# MySQL Service
5+
mysql_master:
6+
image: mysql:5.7.22
7+
restart: unless-stopped
8+
tty: true
9+
ports:
10+
- "${MASTER_PORT}:3306"
11+
volumes:
12+
- masterData:/var/lib/mysql
13+
- ./config/master/cus.cnf:/etc/mysql/conf.d/mysql.conf.cnf
14+
- ./app:/var/www
15+
environment:
16+
MYSQL_DATABASE: ${MYSQL_DATABASE}
17+
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
18+
MYSQL_USER: ${MYSQL_USER}
19+
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
20+
networks:
21+
database-network:
22+
ipv4_address: ${MASTER_IP}
23+
24+
mysql_slave:
25+
image: mysql:5.7.22
26+
restart: unless-stopped
27+
tty: true
28+
ports:
29+
- "${SLAVE_PORT}:3306"
30+
volumes:
31+
- slaveData:/var/lib/mysql
32+
- ./config/slave/cus.cnf:/etc/mysql/conf.d/cus.cnf
33+
- ./app:/var/www
34+
environment:
35+
MYSQL_DATABASE: ${MYSQL_DATABASE}
36+
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
37+
MYSQL_USER: ${MYSQL_USER}
38+
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
39+
depends_on:
40+
- mysql_master
41+
networks:
42+
database-network:
43+
ipv4_address: ${SLAVE_IP}
44+
45+
networks:
46+
database-network:
47+
driver: bridge
48+
ipam:
49+
config:
50+
- subnet: ${SUBNET}
51+
52+
#Volumes
53+
volumes:
54+
masterData:
55+
driver: local
56+
slaveData:
57+
driver: local

readme.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#Docker MySql Master Slave
2+
3+
### Install
4+
5+
Create `.env` file with keys or move `.env.example` to `.env` and edit it.
6+
7+
```
8+
MYSQL_DATABASE=
9+
MYSQL_ROOT_PASSWORD=
10+
MYSQL_USER=
11+
MYSQL_PASSWORD=
12+
13+
SUBNET=172.30.0.0/16
14+
MASTER_PORT=
15+
MASTER_IP=
16+
17+
SLAVE_PORT=
18+
SLAVE_IP=
19+
20+
21+
```
22+
23+
Change `bind-address` on `config/master/cus.cnf` to your master IP and change `bind-address` on `config/slave/cus.cnf` to your slave IP.
24+
25+
> If you want more configuration, you can add these 2 files
26+
27+
Run command:
28+
29+
```
30+
docker-compose up -d mysql_master mysql-salve
31+
```
32+
33+
### Config MySql.
34+
35+
You need login to mysql container then login to mysql console.
36+
37+
#### On MySql Master
38+
39+
On MySql console run below command:
40+
41+
```
42+
HOW MASTER STATUS;
43+
```
44+
45+
The result should look like below:
46+
47+
```
48+
mysql> show master status;
49+
+-------------------------+----------+--------------+------------------+-------------------+
50+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
51+
+-------------------------+----------+--------------+------------------+-------------------+
52+
| 55ee2c9739f6-bin.000003 | 816 | | | |
53+
+-------------------------+----------+--------------+------------------+-------------------+
54+
1 row in set (0.00 sec)
55+
56+
```
57+
58+
You need to pay attention to the following parameters:
59+
60+
```
61+
55ee2c9739f6-bin.000003
62+
63+
816
64+
```
65+
66+
#### On MySql Slave
67+
68+
On MySql console run below command:
69+
70+
```
71+
CHANGE MASTER TO MASTER_HOST='172.30.0.2',MASTER_USER='slave_user', MASTER_PASSWORD='123456', MASTER_LOG_FILE='55ee2c9739f6-bin.000003', MASTER_LOG_POS=816;
72+
```
73+
74+
With `55ee2c9739f6-bin.000003` and `816` is output of the mysql master command.
75+
76+
Continue to run the following command.
77+
78+
```
79+
START SLAVE;
80+
```
81+
82+
Finish. You can check status by command:
83+
84+
```
85+
SHOW SLAVE STATUS;
86+
```

0 commit comments

Comments
 (0)