Skip to content

Commit fbc1aa6

Browse files
committed
initial commit
1 parent 133e185 commit fbc1aa6

File tree

7 files changed

+184
-0
lines changed

7 files changed

+184
-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

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
deploy:
24+
resources:
25+
limits:
26+
cpus: '1'
27+
memory: 1024M
28+
reservations:
29+
memory: 256M
30+
31+
mysql_slave:
32+
image: mysql:5.7.22
33+
restart: unless-stopped
34+
tty: true
35+
ports:
36+
- "${SLAVE_PORT}:3306"
37+
volumes:
38+
- slaveData:/var/lib/mysql
39+
- ./config/slave/cus.cnf:/etc/mysql/conf.d/cus.cnf
40+
- ./app:/var/www
41+
environment:
42+
MYSQL_DATABASE: ${MYSQL_DATABASE}
43+
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
44+
MYSQL_USER: ${MYSQL_USER}
45+
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
46+
depends_on:
47+
- mysql_master
48+
networks:
49+
database-network:
50+
ipv4_address: ${SLAVE_IP}
51+
deploy:
52+
resources:
53+
limits:
54+
cpus: '1'
55+
memory: 1024M
56+
reservations:
57+
memory: 256M
58+
59+
networks:
60+
database-network:
61+
driver: bridge
62+
ipam:
63+
config:
64+
- subnet: ${SUBNET}
65+
#Volumes
66+
volumes:
67+
masterData:
68+
driver: local
69+
slaveData:
70+
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)