1.准备2台机器
server1 192.168.0.37
server2 192.168.0.119
2.安装mysql,我这里用的docker-compose
server1的docker-compose
version: '3'
services:
db:
build: ./build
container_name: db
image: mysql:5.6
restart: always
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M
--max_connections=3000
--innodb_large_prefix=on
--log-bin=mysql-bin
--sync_binlog=1
--server-id=1
--auto_increment_increment=1
--auto_increment_offset=1
--log_slave_updates=1
--binlog-ignore-db=mysql
--sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
volumes:
- "./mysql:/var/lib/mysql"
ports:
- '3306:3306'
environment:
- MYSQL_ROOT_PASSWORD=xxxxxxx
- SET_CONTAINER_TIMEZONE=true
- CONTAINER_TIMEZONE=Asia/Shanghai
server2的docker-compose
version: '3'
services:
db:
build: ./build
container_name: db
image: mysql:5.6
restart: always
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M
--max_connections=3000
--innodb_large_prefix=on
--log-bin=mysql-bin
--sync_binlog=1
--server-id=2
--auto_increment_increment=1
--auto_increment_offset=1
--log_slave_updates=1
--binlog-ignore-db=mysql
--sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
volumes:
- "./mysql:/var/lib/mysql"
ports:
- '3306:3306'
environment:
- MYSQL_ROOT_PASSWORD=xxxxxx
- SET_CONTAINER_TIMEZONE=true
- CONTAINER_TIMEZONE=Asia/Shanghai
build目录是dockerfile和init.sql
dockerfile
FROM mysql:5.6
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
ADD ./init.sql /docker-entrypoint-initdb.d/init.sql
init.sql
create database testdb default character set utf8mb4;
然后docker-compose up -d 把mysql装好,当然,不用docker-compose也是可以的,自己装mysql就好了
安装好以后分别进入mysql
server1
进入容器,登录mysql
show master status;
根据查询结果编写sql(server的ip是192.168.0.37)
change master to master_host = '192.168.0.119',master_port=3306,master_user='root',master_password='xxxxxx',master_log_file='mysql-bin.000002',master_log_pos=120;
另外一台机器也是同理(server的ip是192.168.0.119)
change master to master_host = '192.168.0.37',master_port=3306,master_user='root',master_password='xxxxxx',master_log_file='mysql-bin.000002',master_log_pos=120;
两边都设置了以后,再分别启动
start slave;
启动了以后查看一下状态
show slave status\G
两边查询都显示这样,就表示成功了。