代碼: 選擇全部
mysql1:
container_name: mysql1
restart: always
image: vsamov/mysql-5.1.73
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=homestead
- MYSQL_USER=homestead
- MYSQL_PASSWORD=secret
volumes:
- /home/Dockers/mysql_master2master/mysql1.cnf:/etc/my.cnf:ro
- /home/Dockers/mysql1:/var/lib/mysql/
mysql2:
container_name: mysql2
restart: always
image: vsamov/mysql-5.1.73
ports:
- "3308:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=homestead
- MYSQL_USER=homestead
- MYSQL_PASSWORD=secret
volumes:
- /home/Dockers/mysql_master2master/mysql2.cnf:/etc/my.cnf:ro
- /home/Dockers/mysql2:/var/lib/mysql/
代碼: 選擇全部
[mysqld]
server-id = 1
log_bin = /var/lib/mysql/mysql-bin.log
bind-address = 0.0.0.0 # make sure to bind it to all IPs, else mysql listens on 127.0.0.1
character_set_server = utf8
collation_server = utf8_general_ci
[mysql]
default_character_set = utf8
代碼: 選擇全部
[mysqld]
server-id = 2
log_bin = /var/lib/mysql/mysql-bin.log
bind-address = 0.0.0.0 # make sure to bind it to all IPs, else mysql listens on 127.0.0.1
character_set_server = utf8
collation_server = utf8_general_ci
[mysql]
default_character_set = utf8
代碼: 選擇全部
use mysql;
create user 'replicator'@'%' identified by 'password';
grant replication slave on *.* to 'replicator'@'%';
# do note that the replicator permission cannot be granted on single database.
FLUSH PRIVILEGES;
SHOW MASTER STATUS;
代碼: 選擇全部
mysql2ip=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql2)
mysql1ip=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql1)
docker exec -i mysql1 sh -c "echo '$mysql1ip mysql1 mysql1' >> /etc/hosts"
docker exec -i mysql1 sh -c "echo '$mysql2ip mysql2 mysql2' >> /etc/hosts"
docker exec -i mysql2 sh -c "echo '$mysql1ip mysql1 mysql1' >> /etc/hosts"
docker exec -i mysql2 sh -c "echo '$mysql2ip mysql2 mysql2' >> /etc/hosts"
代碼: 選擇全部
stop slave;
CHANGE MASTER TO MASTER_HOST = 'mysql2',
MASTER_USER = 'replicator',
MASTER_PASSWORD = 'password',
MASTER_LOG_FILE = 'mysql-bin.000002',
MASTER_LOG_POS = 106;
start slave;
show slave status\g
代碼: 選擇全部
stop slave;
CHANGE MASTER TO MASTER_HOST = 'mysql1',
MASTER_USER = 'replicator',
MASTER_PASSWORD = 'password',
MASTER_LOG_FILE = 'mysql-bin.000002',
MASTER_LOG_POS = 106;
start slave;
show slave status\g