1. 配置主库 1.1. 配置主库binlog 1 2 3 4 5 6 7 8 [mysqld] server_id=1 log_bin=mysql-bin # 开启二进制日志功能,值为文件名,默认保存为 /var/lib/mysql/mysql-bin.000001 (序号递增,还有mysql-bin.index等) #要同步的数据库名,不配置则全部 binlog-do-db = cmdb #忽略的数据库名, binlog-ignore-db = mysql
1.2. 建立用户授权信息 1 2 3 4 5 6 7 # 这里使用mysql_native_password验证模式,因为mysql8默认使用了sha2验证,如果这里不加,那么从库change master to 的时候还需要添加get_master_public_key=1参数 create user 'slave1'@'%' identified with mysql_native_password by 'Slave1218.'; # 主要是需要replication slave权限 grant replication slave,replication client on *.* to 'slave1'@'%'; flush privileges;
重启主库,则会生成mysql-bin.000001 、 mysql-bin.index
等文件
show master status;
这个文件名和位置是会变的,从库订阅需要从现在的位置开始
1 2 3 4 5 +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000001 | 2066 | | | | +---------------+----------+--------------+------------------+-------------------+
2. 配置从库 2.1. 系统配置 首先关掉SELinux(当然应该有其它的好的不关闭安全的解决方案)
vi /etc/selinux/config
1 2 SELINUX=disabled # 这里改为disabled SELINUXTYPE=targeted
然后执行reboot
重启
2.2. 配置文件 跟主从相关的主要是下面的配置
1 2 3 4 5 6 [mysqld] server_id=2 # server_id必须和主不一样 log_bin=binlog2 # log_bin 填写bin的名字,会在程序目录生成 对应文件名的binglog relay_log=relay2 # relay_log,需要开启,也是生成对应的名字 read_only=1 # 从库就只读 log_slave_updates=ON # 大概是配置主丛丛使用的
2.3. SQL配置 先检查是否能连接到主库exit;
1 mysql -h192.168.1.101 -P3306 -uslave1 -pSlave1218.;
退出连接exit
,然后连接上从库,执行以下命令
1 2 3 4 5 # 根据密码类型确定是否添加get_master_public_key=1参数 change master to master_host='192.168.1.101',master_port=3306,master_user='slave1',master_password='Slave1218.',master_log_file='binlog.000001',master_log_pos=2066; start slave; show slave status \G;
如果需要调整订阅,则需要先停止stop slave;
再重置reset slave;
然后再执行change master
命令
**PS:**我这里一直没连上,用root账号也没连上,搞了不知多久,最后把selinux关了就可以了,哎还是linux知识浅薄呀。
当我们看到以下日志,没有报错,则说明成功了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for source to send event Master_Host: 192.168.1.101 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000003 Read_Master_Log_Pos: 4434 Relay_Log_File: relay2.000002 Relay_Log_Pos: 323 Relay_Master_Log_File: binlog.000003 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: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 4434 Relay_Log_Space: 524 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 Master_UUID: 72e1654e-bd0b-11ef-8f85-00163e049df0 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 1 Network_Namespace: 1 row in set, 1 warning (0.00 sec)
引用 引用 ,
3. Redis与MySQL同步 使用canal组件: 模拟 MySQL slave 的交互协议,伪装自己为 MySQL slave ,向 MySQL master 发送dump 协议
https://blog.csdn.net/xiyang_1990/article/details/132182468