当前位置:   article > 正文

mysql数据库—主从同步服务_数据库主从同步

数据库主从同步

1、主从同步

1.1、主从同步的两种角色:

主服务器(master):接受客户端访问连接

从服务器(slave):自动同步主服务器数据

1.2、主从同步实现的原理:

        主(master)数据服务器启动binlog日志,当binlog日志在备份的时候。从(slave)数据服务器的的I/O线程会自动复制主服务器的binlog日志里面的sql命令到relay_log文件中,从服务器的sql线程会执行relay_log中的sql语句实现与主服务器中的数据一致

1.3、数据库恢复初始化设置

  1. [root@master ~]# systemctl stop mysqld.service
  2. [root@master ~]# rm -rf /var/lib/mysql/*
  3. [root@master ~]# rm -rf /var/log/mysqld.log
  4. [root@master ~]# systemctl start mysqld
  5. [root@master ~]# grep "password" /var/log/mysqld.log
  6. 2022-05-16T16:05:03.715052Z 1 [Note] A temporary password is generated for root@localhost: Hm1=3D&y?0lA
  7. [root@master ~]# mysql -uroot -p"Hm1=3D&y?0lA"

2、一主一从服务器架构配置:主从角色的数据库服务器各有一台

2.1、环境配置

master服务器:192.168.4.10

slave服务器:192.168.4.20

两台服务器都安装mysql数据库,并关闭防火墙和selinux

2.2、配置master服务器

  1. 1)启用binlog日志文件
  2. [root@master ~]# vim /etc/my.cnf
  3. [mysqld]
  4. server_id=10
  5. log_bin=master10
  6. [root@master ~]# systemctl restart mysqld
  7. 2)用户授权,让用户有复制命令权限
  8. [root@master ~]# mysql -uroot -pJY1987...zy2011
  9. mysql> grant replication slave on *.* to repluser@"%" identified by "JY1987...zy2011";
  10. Query OK, 0 rows affected, 1 warning (0.00 sec)
  11. 3)日志查看
  12. mysql> show master status;
  13. +-----------------+----------+--------------+------------------+-------------------+
  14. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  15. +-----------------+----------+--------------+------------------+-------------------+
  16. | master10.000001 | 441 | | | |
  17. +-----------------+----------+--------------+------------------+-------------------+
  18. 1 row in set (0.00 sec)

2.2、配置slave服务器

  1. 1) 指定server_id,并重启mysqld服务
  2. [root@slave ~]# vim /etc/my.cnf
  3. [mysqld]
  4. server_id=20
  5. 2)确保数据一致(如果一致的此步骤可以省略)
  6. 3)指定主服务器信息
  7. [root@slave ~]# mysql -uroot -pJY1987...zy2011
  8. mysql> show slave status \G #不是从服务器显示结果
  9. Empty set (0.00 sec)
  10. mysql> change master to master_host="192.168.4.10",
  11. -> master_user="repluser",
  12. -> master_password="JY1987...zy2011",
  13. -> master_log_file="master10.000001",
  14. -> master_log_pos=441;
  15. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  16. 4)启动slave线程
  17. mysql> start slave;
  18. Query OK, 0 rows affected (0.00 sec)
  19. 5)查看状态,IO线程和SQL线程必须同时是YES状态
  20. mysql> show slave status \G
  21. *************************** 1. row ***************************
  22. Slave_IO_State: Connecting to master
  23. Master_Host: 192.168.4.10
  24. Master_User: repluser
  25. Master_Port: 3306
  26. Connect_Retry: 60
  27. Master_Log_File: master10.000001
  28. Read_Master_Log_Pos: 441
  29. Relay_Log_File: slave-relay-bin.000001
  30. Relay_Log_Pos: 4
  31. Relay_Master_Log_File: master10.000001
  32. Slave_IO_Running: Yes
  33. Slave_SQL_Running: Yes

3、一主多从服务器架构配置:一台主服务器配置了多台从服务器

3.1、环境配置

master服务器:192.168.4.10

slave服务器:192.168.4.20、192.168.4.30(增加一台从服务器)

延续一主一从的架构,新添加一台从服务器,并安装mysql数据库,并关闭防火墙和selinux

3.2、配置新的slave服务器

  1. 1)指定slave_id,并重启mysqld服务
  2. [root@slave2 ~]# vim /etc/my.cnf
  3. [mysqld]
  4. server_id=30
  5. [root@slave2 ~]# systemctl restart mysqld
  6. 2)确保和主服务器数据一致性,主做完全备份并拷贝给从。
  7. --master-data做完全备份数据时,在备份文件里记录使用的日志名和偏移量
  8. [root@master ~]# mysqldump -uroot -pJY1987...zy2011 --master-data -A > /root/master.sql
  9. mysqldump: [Warning] Using a password on the command line interface can be insecure.
  10. [root@master ~]# scp /root/master.sql 192.168.4.30:/root/
  11. 3)从服务器使用备份文件恢复数据。
  12. [root@slave2 ~]# mysql -uroot -pJY1987...zy2011 < master.sql
  13. 3)指定主服务器信息,在备份文件里查看日志名和偏移量
  14. [root@slave2 ~]# grep master10 /root/master.sql
  15. CHANGE MASTER TO MASTER_LOG_FILE='master10.000001', MASTER_LOG_POS=685;
  16. [root@slave2 ~]# mysql -uroot -pJY1987...zy2011
  17. mysql> change master to master_host="192.168.4.10",
  18. -> master_user="repluser",
  19. -> master_password="JY1987...zy2011",
  20. -> master_log_file="master10.000001",
  21. -> master_log_pos=685;
  22. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  23. 4)启动slave进程,并查看状态(IO线程和SQL线程必须同时是YES状态)
  24. mysql> start slave;
  25. Query OK, 0 rows affected (0.01 sec)
  26. mysql> show slave status \G
  27. *************************** 1. row ***************************
  28. Slave_IO_State: Waiting for master to send event
  29. Master_Host: 192.168.4.10
  30. Master_User: repluser
  31. Master_Port: 3306
  32. Connect_Retry: 60
  33. Master_Log_File: master10.000001
  34. Read_Master_Log_Pos: 685
  35. Relay_Log_File: slave2-relay-bin.000002
  36. Relay_Log_Pos: 319
  37. Relay_Master_Log_File: master10.000001
  38. Slave_IO_Running: Yes
  39. Slave_SQL_Running: Yes

4、主从从服务器架构配置:给一主一从结构中的从服务器也配置从服务器

4.1、环境配置

master服务器:192.168.4.10                      #为slave【1】的主

slave【1】服务器:192.168.4.20               #即为master的从也为slave【2】的主

slave【2】服务器:192.168.4.30               #为slave【1】的从

服务器安装mysql数据库,并关闭防火墙和selinux

4.2、主服务器的配置

  1. [root@master ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server_id=10
  4. log_bin=master10
  5. [root@master ~]# systemctl restart mysqld
  6. [root@master ~]# mysql -uroot -pJY1987...zy2011
  7. mysql> grant replication slave on *.* to slave@"%" identified by "JY1987...zy2011";
  8. Query OK, 0 rows affected, 1 warning (0.00 sec)
  9. mysql> show master status;
  10. +-----------------+----------+--------------+------------------+-------------------+
  11. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  12. +-----------------+----------+--------------+------------------+-------------------+
  13. | master10.000002 | 438 | | | |
  14. +-----------------+----------+--------------+------------------+-------------------+
  15. 1 row in set (0.00 sec)
  16. [root@master ~]# ls /var/lib/mysql/master10.*
  17. /var/lib/mysql/master10.000001 /var/lib/mysql/master10.index

4.3、从服务器【1】的配置:它有两种角色,即为主服务器也为从服务器,因为从服务器也有从服务器所以就要启用级联复制功能。

  1. 1)配置主
  2. [root@slave ~]# vim /etc/my.cnf
  3. [mysqld]
  4. server_id=20
  5. log_bin=zucong20
  6. log_slave_updates #允许级联复制,当前将自己主服务器的数据拷贝给从服务器
  7. [root@slave ~]# systemctl restart mysqld
  8. [root@slave ~]# mysql -uroot -pJY1987...zy2011
  9. mysql> grant replication slave on *.* to slave2@"%" identified by "JY1987...zy2011";
  10. Query OK, 0 rows affected, 1 warning (0.00 sec)
  11. mysql> show master status;
  12. +-----------------+----------+--------------+------------------+-------------------+
  13. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  14. +-----------------+----------+--------------+------------------+-------------------+
  15. | zucong20.000001 | 439 | | | |
  16. +-----------------+----------+--------------+------------------+-------------------+
  17. 1 row in set (0.00 sec)
  18. 2)配置从
  19. mysql> change master to master_host="192.168.4.10",
  20. -> master_user="slave",
  21. -> master_password="JY1987...zy2011",
  22. -> master_log_file="master10.000002",
  23. -> master_log_pos=438;
  24. Query OK, 0 rows affected, 2 warnings (0.00 sec)
  25. mysql> start slave;
  26. Query OK, 0 rows affected (0.00 sec)
  27. mysql> show slave status \G
  28. *************************** 1. row ***************************
  29. Slave_IO_State: Waiting for master to send event
  30. Master_Host: 192.168.4.10
  31. Master_User: slave
  32. Master_Port: 3306
  33. Connect_Retry: 60
  34. Master_Log_File: master10.000002
  35. Read_Master_Log_Pos: 438
  36. Relay_Log_File: slave-relay-bin.000002
  37. Relay_Log_Pos: 319
  38. Relay_Master_Log_File: master10.000002
  39. Slave_IO_Running: Yes
  40. Slave_SQL_Running: Yes

4.4、从服务器【2】的配置,将这台从服务器配置为服务器【1】的从

  1. 1)指定server_id,并重启mysqld服务
  2. [root@slave2 ~]# vim /etc/my.cnf
  3. [mysqld]
  4. server_id=30
  5. [root@slave2 ~]# systemctl restart mysqld
  6. 2)指定主服务器的信息
  7. [root@slave2 ~]# mysql -uroot -pJY1987...zy2011
  8. mysql> change master to master_host="192.168.4.20",
  9. -> master_user="slave2",
  10. -> master_password="JY1987...zy2011",
  11. -> master_log_file="zucong20.000001",
  12. -> master_log_pos=439;
  13. Query OK, 0 rows affected, 2 warnings (0.00 sec)
  14. mysql> start slave;
  15. Query OK, 0 rows affected (0.00 sec)
  16. mysql> show slave status \G
  17. *************************** 1. row ***************************
  18. Slave_IO_State: Waiting for master to send event
  19. Master_Host: 192.168.4.20
  20. Master_User: slave2
  21. Master_Port: 3306
  22. Connect_Retry: 60
  23. Master_Log_File: zucong20.000001
  24. Read_Master_Log_Pos: 439
  25. Relay_Log_File: slave2-relay-bin.000002
  26. Relay_Log_Pos: 319
  27. Relay_Master_Log_File: zucong20.000001
  28. Slave_IO_Running: Yes
  29. Slave_SQL_Running: Yes

5、主主同步:2台数据库服务器,彼此是对方的主服务器,同时还是对方的从服务器 

5.1、环境配置

slave【1】服务器:192.168.4.20

slave【2】服务器:192.168.4.30

服务器安装mysql数据库,并关闭防火墙和selinux

5.2、 slave【1】的配置

  1. [root@slave ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server_id=20
  4. log_bin=slave20
  5. [root@slave ~]# systemctl restart mysqld
  6. [root@slave ~]# mysql -uroot -pJY1987...zy2011
  7. mysql> grant replication slave on *.* to slave20@"%" identified by "JY1987...zy2011";
  8. Query OK, 0 rows affected, 1 warning (0.01 sec)
  9. mysql> show master status;
  10. +----------------+----------+--------------+------------------+-------------------+
  11. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  12. +----------------+----------+--------------+------------------+-------------------+
  13. | slave20.000001 | 440 | | | |
  14. +----------------+----------+--------------+------------------+-------------------+
  15. 1 row in set (0.00 sec)

5.3、slave【2】的配置

  1. [root@slave2 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server_id=30
  4. log_bin=slave30
  5. [root@slave2 ~]# systemctl restart mysqld
  6. [root@slave2 ~]# mysql -uroot -pJY1987...zy2011
  7. mysql> grant replication slave on *.* to slave30@"%" identified by "JY1987...zy2011";
  8. Query OK, 0 rows affected, 1 warning (0.00 sec)
  9. mysql> show master status;
  10. +----------------+----------+--------------+------------------+-------------------+
  11. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  12. +----------------+----------+--------------+------------------+-------------------+
  13. | slave30.000001 | 440 | | | |
  14. +----------------+----------+--------------+------------------+-------------------+
  15. 1 row in set (0.00 sec)
  16. 1)把自己配置为slave【1】的从服务器,日志名和偏移量要在slave【1】主机查看后填写
  17. mysql> change master to master_host="192.168.4.20",
  18. -> master_user="slave20",
  19. -> master_password="JY1987...zy2011",
  20. -> master_log_file="slave20.000001",
  21. -> master_log_pos=440;
  22. Query OK, 0 rows affected, 2 warnings (0.00 sec)
  23. mysql> start slave;
  24. Query OK, 0 rows affected, 1 warning (0.00 sec)
  25. mysql> show slave status \G
  26. *************************** 1. row ***************************
  27. Slave_IO_State: Waiting for master to send event
  28. Master_Host: 192.168.4.20
  29. Master_User: slave20
  30. Master_Port: 3306
  31. Connect_Retry: 60
  32. Master_Log_File: slave20.000001
  33. Read_Master_Log_Pos: 440
  34. Relay_Log_File: slave2-relay-bin.000004
  35. Relay_Log_Pos: 318
  36. Relay_Master_Log_File: slave20.000001
  37. Slave_IO_Running: Yes
  38. Slave_SQL_Running: Yes

 5.4、把slave【1】配置为slave【2】的从服务器,日志名和偏移量要在slave【2】主机查看后填写

  1. mysql> change master to master_host="192.168.4.30",
  2. -> master_user="slave30",
  3. -> master_password="JY1987...zy2011",
  4. -> master_log_file="slave30.000001",
  5. -> master_log_pos=440;
  6. Query OK, 0 rows affected, 2 warnings (0.00 sec)
  7. mysql> start slave;
  8. Query OK, 0 rows affected (0.00 sec)
  9. mysql> show slave status \G
  10. *************************** 1. row ***************************
  11. Slave_IO_State: Waiting for master to send event
  12. Master_Host: 192.168.4.30
  13. Master_User: slave30
  14. Master_Port: 3306
  15. Connect_Retry: 60
  16. Master_Log_File: slave30.000001
  17. Read_Master_Log_Pos: 440
  18. Relay_Log_File: slave-relay-bin.000002
  19. Relay_Log_Pos: 318
  20. Relay_Master_Log_File: slave30.000001
  21. Slave_IO_Running: Yes
  22. Slave_SQL_Running: Yes

6、主从同步复制数据工作模式,可以在任意数据库服务器启用半同步服务模式

异步复制:主服务器执行完一次事务后,立即将结果返回给客户端,不关心从服务器是否已经同步数据。

半同步复制:介于异步复制和全同步复制之间,主服务器在执行完一次事务后,等待至少一台从服务器数据完成,才将结果返回给客户端。

6.1、环境配置

master【1】服务器:192.168.4.10

slave【2】服务器:192.168.4.30

服务器安装mysql数据库,并关闭防火墙和selinux

6.2、方法一:命令行设置,不用重启服务马上生效

  1. 1)安装模块 
  2. mysql> install plugin rpl_semi_sync_master soname "semisync_master.so"; #master模块
  3. Query OK, 0 rows affected (0.00 sec)
  4. mysql> install plugin rpl_semi_sync_slave soname "semisync_slave.so"; #slave模块
  5. Query OK, 0 rows affected (0.00 sec)
  6. 2)查看模块
  7. mysql> select plugin_name,plugin_status from information_schema.plugins where plugin_name like "%semi%";
  8. +----------------------+---------------+
  9. | plugin_name | plugin_status |
  10. +----------------------+---------------+
  11. | rpl_semi_sync_master | ACTIVE |
  12. | rpl_semi_sync_slave | ACTIVE |
  13. +----------------------+---------------+
  14. 2 rows in set (0.00 sec)
  15. 3)启动模块
  16. mysql> set global rpl_semi_sync_master_enabled=1; #主模块
  17. Query OK, 0 rows affected (0.00 sec)
  18. mysql> set global rpl_semi_sync_slave_enabled=1; #从模块
  19. Query OK, 0 rows affected (0.00 sec)
  20. 4)查看模块是否启动
  21. mysql> show variables like "rpl_semi_sync_%_enabled";
  22. +------------------------------+-------+
  23. | Variable_name | Value |
  24. +------------------------------+-------+
  25. | rpl_semi_sync_master_enabled | ON |
  26. | rpl_semi_sync_slave_enabled | ON |
  27. +------------------------------+-------+
  28. 2 rows in set (0.00 sec)

6.2、永久配置,编辑主配置文件

  1. [root@master ~]# vim /etc/my.cnf
  2. [mysqld]
  3. #安装模块
  4. plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
  5. #启用模块
  6. rpl_semi_sync_slave_enabled=1
  7. rpl_semi_sync_master_enabled=1
  8. [root@master ~]# systemctl restart mysqld
  9. [root@master ~]# mysql -uroot -pJY1987...zy2011
  10. mysql> show variables like "rpl_semi_sync_%_enabled";
  11. +------------------------------+-------+
  12. | Variable_name | Value |
  13. +------------------------------+-------+
  14. | rpl_semi_sync_master_enabled | ON |
  15. | rpl_semi_sync_slave_enabled | ON |
  16. +------------------------------+-------+
  17. 2 rows in set (0.00 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/538984
推荐阅读
相关标签
  

闽ICP备14008679号