当前位置:   article > 正文

MySQL之主从同步(openEuler版)

MySQL之主从同步(openEuler版)

实验目的:

基于binlog和gtid两种方法实现主从同步

实验过程:

一、使用binlog方法实现MySQL的主从同步

库配置

  1. 配置文件:
  2. [root@openeuler ~]# vim /etc/my.cnf//进入mysql配置文件配置server_id
  3. [root@openeuler ~]# tail -1 /etc/my.cnf
  4. server_id=1
  5. [root@openeuler ~]# systemctl restart mysql//重启MySQL服务
  6. 主库备份:
  7. [root@openeuler ~]# mysqldump -uroot -pMySQL@123 --opt -B school > db.sql//备份数据库school
  8. mysql> create user rep@'192.168.%.%' identified with myysql_native_password by '123456';//在主库中创建用户(注:授权时可以使用大网段授权)
  9. Query OK, 0 rows affected (0.01 sec)
  10. mysql> grant replication slave on *.* to rep@'192.168..%.%';//给用户进行授权
  11. Query OK, 0 rows affected (0.01 sec)
  12. mysql> show master status;//查看主库的binlog信息
  13. +---------------+----------+--------------+------------------+-------------------+
  14. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  15. +---------------+----------+--------------+------------------+-------------------+
  16. | binlog.000004 | 678 | | | |
  17. +---------------+----------+--------------+------------------+-------------------+
  18. 1 row in set (0.00 sec)
  19. 拷贝给从库:
  20. [root@openeuler ~]# scp db.sql 192.168.27.139:/root/
  21. Authorized users only. All activities may be monitored and reported.
  22. root@192.168.27.139's password:
  23. db.sql 100% 3558 1.5MB/s 00:00
  24. [root@openeuler ~]# scp db.sql 192.168.27.140:/root/
  25. Authorized users only. All activities may be monitored and reported.
  26. root@192.168.27.140's password:
  27. db.sql 100% 3558 3.9MB/s 00:00

从库配置

  1. 配置文件:
  2. [root@node1 ~]# vim /etc/my.cnf//进入配置文件配置server_id
  3. [root@node1 ~]# tail -1 /etc/my.cnf
  4. server_id=2
  5. [root@node1 ~]# systemctl restart mysql//重启MySQL服务
  6. [root@node2 ~]# vim /etc/my.cnf//进入配置文件配置server_id
  7. [root@node2 ~]# tail -1 /etc/my.cnf
  8. server_id=3
  9. [root@node2 ~]# systemctl restart mysql//重启MySQL服务
  10. 还原主库备份:
  11. [root@node1 ~]# mysql -uroot -pMySQL@123 < db.sql
  12. [root@node1 ~]# mysql -uroot -pMySQL@123 -e 'show databases'
  13. +--------------------+
  14. | Database |
  15. +--------------------+
  16. | information_schema |
  17. | mysql |
  18. | performance_schema |
  19. | school |
  20. | sys |
  21. +--------------------+
  22. [root@node2 ~]# mysql -uroot -pMySQL@123 < db.sql
  23. [root@node2 ~]# mysql -uroot -pMySQL@123 -e 'show databases'
  24. +--------------------+
  25. | Database |
  26. +--------------------+
  27. | information_schema |
  28. | mysql |
  29. | performance_schema |
  30. | school |
  31. | sys |
  32. +--------------------+
  33. 设置从库change master:
  34. mysql> change master to
  35. -> master_host='192.168.27.137',
  36. -> master_user='rep',
  37. -> master_password='123456',
  38. -> master_log_file='binlog.000004',
  39. -> master_log_pos=756;//两从库配置相同
  40. Query OK, 0 rows affected, 8 warnings (0.02 sec)
  41. mysql> start slave;
  42. mysql> show slave status \G//查看主从同步状态
  43. *************************** 1. row ***************************
  44. Slave_IO_State:
  45. Master_Host: 192.168.27.137
  46. Master_User: rep
  47. Master_Port: 3306
  48. Connect_Retry: 60
  49. Master_Log_File: binlog.000004
  50. Read_Master_Log_Pos: 756
  51. Relay_Log_File: node1-relay-bin.000001
  52. Relay_Log_Pos: 4
  53. Relay_Master_Log_File: binlog.000004
  54. Slave_IO_Running: Yes
  55. Slave_SQL_Running: Yes

二、使用gtid方法实现MySQL的主从同步

主库配置:

  1. [root@openeuler ~]# cat /etc/my.cnf //开启gtid
  2. gtid_mode=ON
  3. enforce-gtid-consistency=ON
  4. [root@openeuler ~]# systemctl restart mysql//重启MySQL服务

从库配置:

  1. 配置文件:
  2. [root@node1 ~]# cat /etc/my.cnf //开启gtid
  3. gtid_mode=ON
  4. enforce-gtid-consistency=ON
  5. [root@node1 ~]# systemctl restart mysql//重启MySQL服务
  6. [root@node2 ~]# cat /etc/my.cnf //开启gtid
  7. gtid_mode=ON
  8. enforce-gtid-consistency=ON
  9. [root@node2 ~]# systemctl restart mysql//重启MySQL服务
  10. 设置从库change master:
  11. mysql> change master to
  12. -> master_host='192.168.27.137',
  13. -> master_user='rep',
  14. -> master_password='123456',
  15. -> master_auto_position=1;//两从库配置相同
  16. Query OK, 0 rows affected, 7 warnings (0.01 sec)
  17. mysql> start slave;
  18. Query OK, 0 rows affected, 1 warning (0.01 sec)
  19. mysql> show slave status \G;
  20. *************************** 1. row ***************************
  21. Slave_IO_State: Waiting for source to send event
  22. Master_Host: 192.168.27.137
  23. Master_User: rep
  24. Master_Port: 3306
  25. Connect_Retry: 60
  26. Master_Log_File: binlog.000005
  27. Read_Master_Log_Pos: 157
  28. Relay_Log_File: node1-relay-bin.000002
  29. Relay_Log_Pos: 367
  30. Relay_Master_Log_File: binlog.000005
  31. Slave_IO_Running: Yes
  32. Slave_SQL_Running: Yes
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/230655
推荐阅读
相关标签
  

闽ICP备14008679号