参考博客:Docker搭建Mysql5.6配置主从数据库
修改主-MySQL数据库my.cnf配置文件
暂时设置只读模式,防止通过SQL同步从库后又有新数据产生导致数据不一致问题
[mysqld] ... innodb_force_recovery = 4
同步mysql数据库,主从数据库数据需保持一致
主MySQL:192.168.209.130 从MySQL:192.168.209.131 导出主-MySQL数据库的数据SQL文件
mysqldump -h127.0.0.1 -uroot -p'123456' --socket=/var/lib/mysql/mysql.sock --all-databases --lock-all-tables > /root/master_5.5_db.sql
通过scp命令传递
scp master_5.5_db.sql root@192.168.209.131:/root
在从-MySQL中执行导入master_5.5_db.sql
mysql -uroot -p123456 -h127.0.0.1 < master_5.5_db.sql
修改从-MySQL数据库的my.cnf配置文件,添加从库-Slave配置信息
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 socket=/var/lib/mysql/mysql.sock [mysqld] skip-name-resolve #设置3306端口 port = 3306 socket=/var/lib/mysql/mysql.sock # 设置mysql的安装目录, 这里的目录一定要是你解压后并且改了名的目录哟.. basedir=/tgdata/mysql-5.5.62 # 设置mysql数据库的数据的存放目录, 这里的目录一定要是你解压后并且改了名的目录哟.. datadir=/tgdata/mysql-5.5.62/data # 允许最大连接数 max_connections=2000 # 服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf8 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB # 大小写不敏感 lower_case_table_name=1 # 单个数据包的最大大小(一般:16M-64M,大数据/多媒体:256M-1GB) max_allowed_packet=16M # 开启独立表空间(不开启,则默认所有文件数据存ibdata1下,且删除表空间不释放!-> mysql5.6.6及其后续版本默认开启) innodb_file_per_table=1 # 从库日志 log_bin = slave-bin server_id = 2 # 中继日志文件名 relay_log = slave-relay-bin # 从库执行中继日志中的事件后,会将这些事件写入自己的二进制日志(便于级联复制或数据恢复) log_slave_updates = 1 # 只读模式(super权限账号可执行) read_only = 1 #binlog过期清理时间 expire_logs_days = 15 #binlog每个日志文件大小 max_binlog_size = 500m binlog_format=mixed # 忽略同步表 replicate_ignore_db=information_schema replicate_ignore_db=performance_schema replicate_ignore_db=mysql replicate_ignore_db=sys replicate_ignore_db=test # MySQL 8.0+ 支持 #slave_parallel_workers=4 # 设置并行线程数,4 是推荐的并行线程数 #slave_parallel_type=LOGICAL # 设置并行复制的类型为 LOGICAL #跳过错误(多个错误,用逗号分隔) ## all:跳过所有错误(不推荐生产环境使用) ## 1062 跳过主键冲突错误 ## 1146 表不存在 #slave-skip-errors=1062
配置主-MySQL授权对从-MySQL的复制权限
MySQL 5.7及以下
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl'@'%' IDENTIFIED BY 'GrantT^Slave130';
MySQL 8.0+
CREATE USER 'repl'@'192.168.1.%' IDENTIFIED BY 'GrantT^Slave130'; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'192.168.1.%';
检查权限赋予结果
SHOW GRANTS FOR 'repl'@'%';
刷新日志文件
获取主-MySQL的bin-log日志及日志点位置
+-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000008 | 107 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
配置从-MySQL数据库的复制操作
CHANGE MASTER TO MASTER_HOST='192.168.209.130',MASTER_USER='repl',MASTER_PASSWORD='GrantT^Slave130',MASTER_LOG_FILE='master-bin.000008',MASTER_LOG_POS=107;
启动从库
查看主从同步状态
关注从库信息:
Slave_IO_Running: Yes和Slave_SQL_Running: Yes :表示主从同步状态正常Seconds_Behind_Master:表示主从数据库同步延迟(单位:秒)
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.209.130 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000011 Read_Master_Log_Pos: 107 Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 254 Relay_Master_Log_File: master-bin.000011 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: information_schema,performance_schema,mysql,sys,test Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 107 Relay_Log_Space: 410 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) ERROR: No query specified