操作步骤
- 修改 master 主节点 的配置( server-id log-bin )
- master 主节点 完全备份( mysqldump )
- master 主节点 创建复制用户并授权
- master 主节点 将完全备份文件拷贝至从节点
- 修改 slave 从节点 的配置( server-id read-only )
- 修改 完全备份数据文件( 添加主节点的相关信息 )
- slave 从节点 还原备份( souce /data/all.sql )
- slave 从节点 开启复制( start slave; )
- 验证 主从复制架构
// 主节点# 修改 master 主节点的配置[root@master ~] vim /etc/my.cnf.d/mysql-server.cnf[mysqld]server-id=8log_binlog-bin=/data/mysql/logbin/mysql-bin[root@master ~] systemctl restart mysqld# 完全备份[root@master ~] mysqldump -A -F --master-data=1 --single-transaction > /root/all.sql// 创建复制用户并授权# 注意: 修改 IP 地址mysql> create user repluser@"192.168.80.%" identified by "123456"mysql> grant replication slave on *.* to repluser@"192.168.80.%"";# 将备份复制到从节点[root@master ~] scp /root/all.sql 192.168.80.150:/root// 配置从节点[root@slave ~] yum install mysql-server -y[root@slave ~] vim /etc/my.cnf.d/mysql-server.cnf[mysqld]server-id=18log_binread-only[root@slave ~] systemctl enable --now mysqld# 从节点修改备份文件[root@centos8 ~] vim /root/all.sql CHANGE MASTER TOMASTER_HOST='192.168.80.130', MASTER_USER='repluser',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=156;
# 从节点还原备份mysql> set sql_log_bin=0;mysql> source /root/all.sql;mysql> set sql_log_bin=1;# 从节点开始主从复制mysql> start slave;// 验证线程运行状态 ( 重要 )mysql> show slave status\G;*************************** 1. row *************************** Slave_IO_State: Waiting for source to send eventMaster_Host: 192.168.80.130Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000005Read_Master_Log_Pos: 367 Relay_Log_File: slave-relay-bin.000002Relay_Log_Pos: 535Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes# 运行中Slave_SQL_Running: Yes# 运行中Replicate_Do_DB: Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: // 验证主从同步效果mysql> drop table coc; # 主节点删除mysql> show tables;# 从节点验证