mysql master to master

回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

mysql master to master

文章 yehlu »

https://vnextcoder.wordpress.com/2016/0 ... on-docker/

代碼: 選擇全部

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/    
mysql1.cnf

代碼: 選擇全部

[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
mysql2.cnf

代碼: 選擇全部

[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
create mysql user

代碼: 選擇全部

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;
get mysql ip

代碼: 選擇全部

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"
mysql1 slave sql

代碼: 選擇全部

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
mysql2 slave sql

代碼: 選擇全部

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
回覆文章

回到「Docker」