[关闭]
@zhangyy 2021-10-28T15:26:55.000000Z 字数 3290 阅读 155

MySQL 主从复制之一: 传统异步复制模式

MySQL系列


一: 实验要求:

image_1fir0rj5j1m712o0ib193nv1h9.png-931.8kB

1.1 配置主从复制:

  1. 配置启动主从复制:
  2. 主库从库都要创建复制用户:
  3. mysql -uroot -proot
  4. create user 'repuser'@'%' identified by 'repuser123';
  5. grant replication slave on *.* to 'repuser'@'%';
  6. GRANT ALL PRIVILEGES ON *.* TO 'repuser'@'%'IDENTIFIED BY 'repuser123' WITH GRANT OPTION;
  7. flush privileges;
  8. select user,host from mysql.user;

image_1fir33e2kee2qc3vksb0faq79.png-114.1kB

image_1fir33qjhude1t1e1pta14574bum.png-95.3kB

  1. 192.168.100.11
  2. #master add parameter
  3. vi /mysql/data/3306/my.cnf
  4. -----
  5. log_bin=/mysql/log/3306/binlog/mysql-binlog
  6. log_bin_index=/mysql/log/3306/binlog/mysql-binlog.index
  7. binlog_format=row
  8. binlog_rows_query_log_events=on
  9. #master modify parameter
  10. bind-address=0.0.0.0
  11. server_id=113306
  12. expire_logs_days = 7
  13. innodb_support_xa =1
  14. binlog_cache_size = 32M
  15. max_binlog_size = 2048M
  16. log_bin_trust_function_creators = 1
  17. innodb_flush_log_at_trx_commit =1
  18. sync_binlog = 1
  19. transaction-isolation = read-committed
  20. ----

image_1fir3n4ld1d3uuum1qsr1op8sg1j.png-104.2kB


  1. #slave parameter
  2. #relay_log = /mysql/log/3306/relaylog/itpuxdb-relay.log
  3. #log-slave-updates = 1
  4. #read_only=1
  5. #slave-parallel-type=LOGICAL_CLOCK
  6. #slave-parallel-workers=4
  7. #master_info_repository=TABLE
  8. #relay_log_info_repository=TABLE
  9. #relay_log_recovery=1
  10. #slave_skip_errors = ddl_exist_errors
  11. #slave_preserve_commit_order=1

  1. ## 192.168.1.12(slave)
  2. vi /mysql/data/3306/my.cnf
  3. ----
  4. #slave add parameter
  5. log_bin=/mysql/log/3306/binlog/itpuxdb-binlog
  6. log_bin_index=/mysql/log/3306/binlog/itpuxdb-binlog.index
  7. binlog_format=row
  8. binlog_rows_query_log_events=on
  9. #slave modify parameter
  10. bind-address=0.0.0.0
  11. server_id=123306
  12. skip_name_resolve = on
  13. expire_logs_days = 7
  14. innodb_support_xa =1
  15. binlog_cache_size = 1M
  16. max_binlog_size = 2048M
  17. log_bin_trust_function_creators = 1
  18. innodb_flush_log_at_trx_commit =1
  19. sync_binlog = 1
  20. transaction-isolation = read-committed
  21. #slave parameter
  22. relay_log = /mysql/log/3306/relaylog/mysql-relay.log
  23. log-slave-updates = 1
  24. read_only=1
  25. slave-parallel-type=LOGICAL_CLOCK
  26. slave-parallel-workers=4
  27. master_info_repository=table
  28. relay_log_info_repository=table
  29. relay_log_recovery=1
  30. slave_skip_errors = ddl_exist_errors
  31. slave_preserve_commit_order=1
  32. ------
  33. 从新启动MySQL
  34. systemctl restart mysqld

image_1fir408bca2c7a1oo31au410bk20.png-160.1kB

image_1fir41b2dui0e421tbt1vte1ulq2d.png-36.1kB

image_1fir4210015pb1dt4c3s1rudquq3a.png-48.3kB


  1. 从主库上看binlog 日志:
  2. 主库: 192.168.100.11
  3. mysql -uroot -proot
  4. show master status;

image_1fir45h2k1f9ejavr6u145d15i847.png-166.2kB

  1. 从库上面 复制主库
  2. 从库:192.168.100.12
  3. mysql -uroot -proot
  4. stop slave;
  5. change master to master_host='192.168.100.11', master_user='repuser', master_password='repuser123', master_log_file='mysql-binlog.000001', master_log_pos=154;
  6. start slave;
  7. show slave status \G

image_1fir4o3pg1mr1hl21ofm1dcf18ga5k.png-22.6kB

image_1fir4p748t9m18fh1ohq10v65bv61.png-67.1kB

image_1firokgt91l0v9e01smr137a1iqs9.png-138.7kB

  1. show processlist\G ;

image_1firolp5s17m91c4r1dln1f9o1k2jm.png-137kB

image_1firomnf21db112581mgl1nobmp713.png-119.9kB

  1. 在主库上创建库表测试:
  2. create database itpuxdb1;
  3. use itpuxdb1;
  4. create table itpuxbak11 (id int primary key,name varchar(40));
  5. insert into itpuxbak11 values (1,'itpux111'),(2,'itpux112'),(3,'itpux113'),(4,'itpux114'),(5,'itpux1 15');
  6. commit;
  7. select * from itpuxbak11;
  8. 备库检查:
  9. select * from itpuxdb1.itpuxbak11;

  1. 涉及相关参数介绍与总结
  2. show slave status 参数介绍:
  3. mysql> show slave status \G
  4. *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event -- IO 线程 的状态
  5. master_host: 192.168.1.51 -- 主库 IP
  6. master_user: repuser -- 主库同步的用户
  7. master_port: 3306 -- 主库同步的端口
  8. Connect_Retry: 60 -- 连接尝试次数,使用 change master 时可以使用master-connect-retry 选项指定当前值。
  9. master_log_file: itpuxdb-binlog.000005 -- IO 线程读取到主服务器二进制的文件
  10. Read_Master_Log_Pos: 4117 --IO 线程读取主服务器二进制文件中的位置
  11. relay_log_file: itpuxdb-relay.000002 --Slave SQL
  12. 线程当前正在读取和执行的中继日志文件的名称。
  13. relay_log_pos: 4288 线程读取 Slave 服务器中继日志文件中的位置
  14. Relay_Master_Log_File: itpuxdb-binlog.000005 -- SQL 线程执行到的文
  15. Slave_IO_Running: Yes -- io thread 启动成功
  16. Slave_SQL_Running: Yes -- sql thread 启动成功
  17. Replicate_Do_DB:
  18. Replicate_Ignore_DB:
  19. Replicate_Do_Table:
  20. Replicate_Ignore_Table:
  21. Replicate_Wild_Do_Table:
  22. Replicate_Wild_Ignore_Table:
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注