数据库集群系列(十五)使用docker-compose构建Mysql 5.7数据库主从复制、解决UUID重复导致复制失败问题
一、基本情况
项目服务器,因经常频繁断电,出现过核心数据丢失的情况,现需要重构部署,考虑采用docker-compose容器集群方式实现。
俩台主机:
192.168.1.137【主数据库】、my.cnf开启binlog日志
192.168.1.138【从数据库】、my.cnf未开启binlog日志
二、Master部署
1、创建挂载目录
mkdir -p mysql/master/{mydir,datadir,conf,source}
2、配置yaml文件
vim docker-compose.yaml
version: '3'services: mysql: restart: always image: mysql:5.7.34 container_name: mysql-5.7master volumes: - mysql/mydir:/mydir - mysql/master/datadir:/var/lib/mysql - mysql/master/conf/my.cnf:/etc/my.cnf environment: - "MYSQL_ROOT_PASSWORD=123456" - "TZ=Asia/Shanghai" ports: - 3308:3306
3、配置my.cnf文件
vim conf/my.cnf
[mysqld]character-set-server = utf8collation-server = utf8_general_cisql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZEROlower_case_table_names = 1transaction-isolation = READ-COMMITTEDdefault-time_zone = '+8:00'log_timestamps = SYSTEMlog-bin=mysql-bin #开启日志 server-id=137 #唯一编号expire_logs_days=15 #日志保存天数###############################innodb_buffer_pool_size = 2Ginnodb_log_file_size=128Minnodb_log_files_in_group=4innodb_log_buffer_size=16Minnodb_write_io_threads = 8innodb_read_io_threads = 8innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb-file-per-table=1 auto_increment_increment=1auto_increment_offset=1connect_timeout=10group_concat_max_len=1024innodb_thread_concurrency=0innodb_thread_sleep_delay=10000innodb_write_io_threads=12interactive_timeout=28800lock_wait_timeout=31536000long_query_time=10.000000low_priority_updates=OFFmax_allowed_packet=500Mmax_connect_errors=999999999max_connections=1600max_length_for_sort_data=1024max_prepared_stmt_count=16382max_user_connections=0net_read_timeout=30net_retry_count=10net_write_timeout=60ngram_token_size=2open_files_limit=102400performance_schema=OFFquery_alloc_block_size=8192query_cache_limit=1048576query_cache_size=0query_cache_type=OFFquery_cache_wlock_invalidate=OFFquery_prealloc_size=8192slow_launch_time=2table_definition_cache=768table_open_cache=512table_open_cache_instances=16thread_cache_size=512tmp_table_size=1073741824wait_timeout=2147483interactive_timeout=31536000explicit_defaults_for_timestamp = truelog-bin-trust-function-creators = 1[client]default-character-set=utf8[mysql]default-character-set=utf8
4、启动master服务
docker-compose up -ddocker-compose down
5、连接并创建测试账号
# mysql -uroot -p123456 -P3308 -h127.0.0.1
-- 创建用户CREATE USER 'sync'@'%' IDENTIFIED BY '123456';-- 赋予复制数据权限grant replication slave on *.* to 'sync'@'%' IDENTIFIED BY '123456';-- 刷新授权,使之立即生效flush privileges;
6、查看Master相关信息
show master status;
mysql> show master status;+------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------------+----------+--------------+------------------+-------------------+| mysql-bin.000004 | 154 | | | |+------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)
三、Slave部署
1、创建挂载目录
mkdir -p mysql/slave/{mydir,datadir,conf,source}
2、配置yaml文件
vim docker-compose.yaml
version: '3'services: mysql: restart: always image: mysql:5.7.34 container_name: mysql-5.7slave volumes: - mysql/slave/datadir:/var/lib/mysql - mysql/slave/conf/my.cnf:/etc/my.cnf environment: - "MYSQL_ROOT_PASSWORD=123456" - "TZ=Asia/Shanghai" ports: - 3308:3306
3、修改my.cnf配置文件
[mysqld]server-id=138character-set-server = utf8collation-server = utf8_general_cisql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZEROlower_case_table_names = 1transaction-isolation = READ-COMMITTEDdefault-time_zone = '+8:00'log_timestamps = SYSTEM###############################innodb_buffer_pool_size = 2Ginnodb_log_file_size=128Minnodb_log_files_in_group=4innodb_log_buffer_size=16Minnodb_write_io_threads = 8innodb_read_io_threads = 8innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb-file-per-table=1 auto_increment_increment=1auto_increment_offset=1connect_timeout=10group_concat_max_len=1024innodb_thread_concurrency=0innodb_thread_sleep_delay=10000innodb_write_io_threads=12interactive_timeout=28800lock_wait_timeout=31536000long_query_time=10.000000low_priority_updates=OFFmax_allowed_packet=500Mmax_connect_errors=999999999max_connections=1600max_length_for_sort_data=1024max_prepared_stmt_count=16382max_user_connections=0net_read_timeout=30net_retry_count=10net_write_timeout=60ngram_token_size=2open_files_limit=102400performance_schema=OFFquery_alloc_block_size=8192query_cache_limit=1048576query_cache_size=0query_cache_type=OFFquery_cache_wlock_invalidate=OFFquery_prealloc_size=8192slow_launch_time=2table_definition_cache=768table_open_cache=512table_open_cache_instances=16thread_cache_size=512tmp_table_size=1073741824wait_timeout=2147483interactive_timeout=31536000explicit_defaults_for_timestamp = truelog-bin-trust-function-creators = 1[client]default-character-set=utf8[mysql]default-character-set=utf8
4、启动slave服务
docker-compose up -ddocker-compose down
5、连接并配置连接到Master
mysql -uroot -P3308 -p123456 -h127.0.0.1
stop slave;reset master;change master tomaster_host='192.168.1.137',master_port=3308,master_user='sync',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=4162,MASTER_AUTO_POSITION=0;
start slave;
6、查看开启slave是否成功
show slave status;

7、报错处理The slave I/O thread stops
查看日志信息:docker logs -f mysql-5.7master
mysql-5.7slave | 2024-01-03T17:05:52.332922+08:00 8 [ERROR] Slave I/O for channel '':Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593mysql-5.7slave | 2024-01-03T17:05:52.332951+08:00 8 [Note] Slave I/O thread exiting for channel '', read up to log 'mysql-bin.000002', position 154
解决:
先查看数据文件路径:SHOW VARIABLES LIKE 'datadir';
删除 auto.cnf文件
重新执行docker-compose up 启动即可
三、主从模式验证
在master数据库创建一个test库,并导入sql;可以发现slave库也同步导入。
证明主从模式配置无问题,一切使用正常。
