• MySQL数据库集群技术主从自动协商详细讲解


    MySQL主从自动协商

    需求

    • 实验2与上一个实验需求基本相同。master1 作为主mysql,slave1 作为从mysql。 不同之处,使用了 “gtid_mode=ON enforce_gtid_consistency=1” 该属性自动记录position位置。不需要手动指定了。

    环境

    主服务器配置

    • 部署一台新mysql服务器。准备好域名解析。

    • 准备数据

    1. mysql> create database master1db;
    2. Query OK, 1 row affected (0.05 sec)
    3. mysql> use master1db;
    4. Database changed
    5. mysql> create table master1db.master1tab(name char(50));
    6. Query OK, 0 rows affected (0.03 sec)
    7. mysql> insert into master1db.master1tab values (1111);
    8. Query OK, 1 row affected (0.02 sec)
    9. mysql> insert into master1db.master1tab values (2222);
    10. Query OK, 1 row affected (0.00 sec)
    • 开启二进制日志

    1. [root@service ~]# vim /etc/my.cnf
    2. server-id=1
    3. log_bin
    4. gtid_mode=ON
    5. enforce_gtid_consistency=1
    6. [root@service ~]# systemctl restart mysqld
    • 创建复制用户

    1. mysql> create user 'rep'@'192.168.142.%' identified by 'Wyxbuke00.';
    2. Query OK, 0 rows affected (0.03 sec)
    3. mysql> grant replication slave,replication client on *.* to 'rep'@'192.168.142.%' ;
    4. Query OK, 0 rows affected (0.00 sec)
    • 备份master数据库的数据

    1. [root@service ~]# mysqldump -p'Wyxbuke00.' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql
    2. [root@service ~]# ll
    3. 总用量 446108
    4. -rw-r--r--. 1 root root 0 2月 22 16:12 2024-02-22
    5. -rw-r--r--. 1 root root 1288580 2月 22 16:10 2024-02-22-mysql-all.sql
    6. -rw-r--r--. 1 root root 0 2月 23 10:20 2024-02-23
    7. -rw-r--r--. 1 root root 1288900 2月 23 10:21 2024-02-23-mysql-all.sql
    8. [root@service ~]# scp 2024-02-23-mysql-all.sql slave1:/tmp
    9. root@slave1's password:
    10. 2024-02-23-mysql-all.sql 100% 1259KB 48.4MB/s 00:00
    11. 观察二进制日志分割点
    12. -- CHANGE MASTER TO MASTER_LOG_FILE='service-bin.000003', MASTER_LOG_POS=157;
    • 准备数据2

    1. mysql> insert into master1db.master1tab values (6666666666);
    2. Query OK, 1 row affected (0.02 sec)
    3. mysql> use master1db;
    4. Reading table information for completion of table and column names
    5. You can turn off this feature to get a quicker startup with -A
    6. Database changed
    7. mysql> select * from master1db.master1tab;
    8. +------------+
    9. | name |
    10. +------------+
    11. | 1111 |
    12. | 2222 |
    13. | 33333333 |
    14. | 44444 |
    15. | 55555 |
    16. | 6666666666 |
    17. +------------+
    18. 6 rows in set (0.00 sec)
    从服务器配置
    • 重置从服务器数据库

    1. [root@server ~]# systemctl stop mysqld
    2. [root@server ~]# rm -rf /var/lib/mysql/*
    3. [root@server ~]# systemctl start mysqld
    4. [root@server ~]# grep password /var/log/mysqld.log
    5. [Server] A temporary password is generated for root@localhost: GsH5i*E&!m*V
    6. [root@server ~]# mysqladmin -uroot -p'GsH5i*E&!m*V' password 'Wyxbuke00.'
    • 测试rep用户是否可用

    1. [root@server ~]# mysql -h master1 -urep -p'Wyxbuke00.'
    2. mysql: [Warning] Using a password on the command line interface can be insecure.
    3. Welcome to the MySQL monitor. Commands end with ; or \g.
    4. Your MySQL connection id is 15
    5. Server version: 8.0.36 MySQL Community Server - GPL
    6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
    7. Oracle is a registered trademark of Oracle Corporation and/or its
    8. affiliates. Other names may be trademarks of their respective
    9. owners.
    10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    11. mysql>
    • 启动二进制日志,服务器ID,GTID

    1. vim /etc/my.cnf
    2. server-id=2
    3. log_bin
    4. gtid_mode=ON
    5. enforce_gtid_consistency=1
    6. [root@server ~]# systemctl restart mysqld
    • 还原恢复手动同步数据

    1. [root@server ~]# ll /tmp
    2. total 1260
    3. -rw-r--r--. 1 root root 1288580 Feb 22 16:35 2024-02-22-mysql-all.sql
    4. -rw-r--r--. 1 root root 0 Feb 23 10:06 2024-02-23-10
    5. mysql> set sql_log_bin=0;
    6. Query OK, 0 rows affected (0.00 sec)
    7. mysql> source /tmp/2024-02-23-10;
    8. mysql> source /tmp/2024-02-23-mysql-all.sql;
    9. mysql> select * from master1db.master1tab;
    10. +------------+
    11. | name |
    12. +------------+
    13. | 1111 |
    14. | 2222 |
    15. | 33333333 |
    16. | 44444 |
    17. | 55555 |
    18. | 6666666666 |
    19. +------------+
    20. 6 rows in set (0.00 sec)
    • 设置主服务器

    1. mysql> change master to master_host='master1',master_user='rep',master_password='Wyxbuke00.',master_auto_position=1;
    2. Query OK, 0 rows affected, 7 warnings (0.03 sec)
    3. mysql> start slave;
    4. Query OK, 0 rows affected, 1 warning (0.01 sec)
    5. mysql> show slave status\G;
    6. *************************** 1. row ***************************
    7. Slave_IO_State: Waiting for source to send event
    8. Master_Host: master1
    9. Master_User: rep
    10. Master_Port: 3306
    11. Connect_Retry: 60
    12. Master_Log_File: service-bin.000007
    13. Read_Master_Log_Pos: 197
    14. Relay_Log_File: server-relay-bin.000002
    15. Relay_Log_Pos: 377
    16. Relay_Master_Log_File: service-bin.000007
    17. Slave_IO_Running: Yes
    18. Slave_SQL_Running: Yes
    19. Replicate_Do_DB:
    20. Replicate_Ignore_DB:
    21. Replicate_Do_Table:
    22. Replicate_Ignore_Table:
    23. Replicate_Wild_Do_Table:
    24. Replicate_Wild_Ignore_Table:
    25. Last_Errno: 0
    26. Last_Error:
    27. Skip_Counter: 0
    28. Exec_Master_Log_Pos: 197
    29. Relay_Log_Space: 588
    30. Until_Condition: None
    31. Until_Log_File:
    32. Until_Log_Pos: 0
    33. Master_SSL_Allowed: No
    34. Master_SSL_CA_File:
    35. Master_SSL_CA_Path:
    36. Master_SSL_Cert:
    37. Master_SSL_Cipher:
    38. Master_SSL_Key:
    39. Seconds_Behind_Master: 0
    40. Master_SSL_Verify_Server_Cert: No
    41. Last_IO_Errno: 0
    42. Last_IO_Error:
    43. Last_SQL_Errno: 0
    44. Last_SQL_Error:
    45. Replicate_Ignore_Server_Ids:
    46. Master_Server_Id: 1
    47. Master_UUID: d5ddcb15-d063-11ee-a4cc-000c2904e4be
    48. Master_Info_File: mysql.slave_master_info
    49. SQL_Delay: 0
    50. SQL_Remaining_Delay: NULL
    51. Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
    52. Master_Retry_Count: 86400
    53. Master_Bind:
    54. Last_IO_Error_Timestamp:
    55. Last_SQL_Error_Timestamp:
    56. Master_SSL_Crl:
    57. Master_SSL_Crlpath:
    58. Retrieved_Gtid_Set:
    59. Executed_Gtid_Set: d5ddcb15-d063-11ee-a4cc-000c2904e4be:1
    60. Auto_Position: 1
    61. Replicate_Rewrite_DB:
    62. Channel_Name:
    63. Master_TLS_Version:
    64. Master_public_key_path:
    65. Get_master_public_key: 0
    66. Network_Namespace:
    67. 1 row in set, 1 warning (0.00 sec)

     

    • 返回主服务器更新数据,到从服务器查看数据

    1. 主服务器
    2. mysql> insert into master1db.master1tab values (777788);
    3. Query OK, 1 row affected (0.00 sec)
    4. 从服务器
    5. mysql> select * from master1db.master1tab;
    6. +------------+
    7. | name |
    8. +------------+
    9. | 1111 |
    10. | 2222 |
    11. | 33333333 |
    12. | 44444 |
    13. | 55555 |
    14. | 6666666666 |
    15. | 777788 |
    16. +------------+
    17. 7 rows in set (0.01 sec)

  • 相关阅读:
    人员定位在安全生产管理中的应用
    我的NVIDIA开发者之旅——为 NVIDIA Jetson Nano 运行 Docker 容器(学习笔记)
    学习笔记-支付宝支付
    Python字符串处理:掌握文本的艺术
    Libuv 源码解析 - QUEUE(双向循环队列)
    Diagrams——制作短小精悍的流程图
    定位java程序中占用cpu最高的线程堆栈信息
    1.读dubbo spi机制源码
    Redis稳定性之战:AOF日志支撑数据持久化
    ROS MoveIT1(Noetic)安装总结
  • 原文地址:https://blog.csdn.net/m0_66011019/article/details/136249788