草庐IT

mysql数据库的级联复制

陈皮糖爱悦读 2023-03-28 原文
今天分享的是mysql数据库中的级联复制,要想实现mysql的级联复制首先要进行建立主从复制,在其基础上增加新的主机。

范例:实现级联复制

需要在中间的从服务器启用以下配置 ,实现中间slave节点能将master的二进制日志在本机进行数据库更新,并且也同时更新本机的二进制,从而实现级联复制

1.# 级联复制主节点指定server-id并开启二进制日志
[root@ubuntu2204 ~]#vim /etc/my.cnf
server-id=200 #id
log-bin #二进制日志
[root@ubuntu2204 ~]#systemctl start mysql

2.#查看二进制默认和数据库放在一起
[root@ubuntu2204 ~]#ls /data/mysql/

3.在数据库中创建用户
mysql> create user repluser@'10.0.0.%' indentified by '123456';
Query ok,0 rows affected (0.01 sec)
mysql> select * from mysql.user\G
*************************** 1. row ***************************
Host: 10.0.0.%
User: repluser
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: Y
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
Create_tablespace_priv: N
ssl_type:
ssl_cipher: 0x
x509_issuer: 0x
x509_subject: 0x
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
plugin: mysql_native_password
authentication_string: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
password_expired: N
password_last_changed: 2022-11-15 22:32:48
password_lifetime: NULL
account_locked: N
Create_role_priv: N
Drop_role_priv: N
Password_reuse_history: NULL
Password_reuse_time: NULL
Password_require_current: NULL
User_attributes: NULL
User_attributes: NULL
6 rows in set (0.00 sec)

4.#授权
mysql> grant replication slave on *.* to repluser@'10.0.0.%';
Query OK, 0 rows affected (0.03 sec)

5.#备份
mysql> show master logs;
+-----------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+-----------------------+-----------+-----------+
| ubuntu2204-bin.000001 | 209 | No |
| ubuntu2204-bin.000002 | 180 | No |
| ubuntu2204-bin.000003 | 1168 | No |
| ubuntu2204-bin.000004 | 180 | No |
| ubuntu2204-bin.000005 | 388 | No |
+-----------------------+-----------+-----------+
5rows in set (0.01 sec)
#-F是刷新日志加查看
[root@ubuntu2204 ~]#mysqldump -uroot -p123456 -A --single-transaction --source-data=1 -f > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@ubuntu2204 ~]#vim all.sql
CHANGE MASTER TO MASTER_LOG_FILE='ubuntu2204-bin.000005', MASTER_LOG_POS=388;

6.把复制文件拷贝到从节点
[root@ubuntu2204 ~]#scp all.sql 10.0.0.100:
The authenticity of host '10.0.0.161 (10.0.0.161)' can't be established.
ED25519 key fingerprint is SHA256:rziuY3aU2QPQ9Aj+y//KUMlP8fkRzlREsMZcUn2sRuk.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.161' (ED25519) to the list of known hosts.
root@10.0.0.100's password:
all.sql 100% 1258KB 33.1MB/s 00:00

7.#在从节点更改all.sql文件
[root@ubuntu2004 ~]#vim all.sql
CHANGE MASTER TO MASTER_HOST='10.0.0.200',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='ubuntu2204-bin.000005', MASTER_LOG_POS=388;

#上面文件和下面类似
//CHANGE MASTER TO MASTER_HOST='10.0.0.200',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mariadb-bin.xxxxxx',
MASTER_LOG_POS=#//

8.#登入到10.0.0.100的MySQL中,防止产生二进制文件就关掉二进制日志
[root@ubuntu2004 ~]#mysql
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.01 sec)

9.#导入,然后再恢复二进制日志
mysql> source all.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
.........................................
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 10.0.0.200
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: ubuntu2204-bin.000005
Read_Master_Log_Pos: 388
Relay_Log_File: relay-log.000010
Relay_Log_Pos: 614
Relay_Master_Log_File: ubuntu2204-bin.000005
Slave_IO_Running: NO
Slave_SQL_Running: NO
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: 388
Relay_Log_Space: 992
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: 200
Master_UUID: 8a22d97f-576e-11ed-8984-000c29a89959
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: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)

10.开启线程
mysql> start slave;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 10.0.0.200
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: ubuntu2204-bin.000005
Read_Master_Log_Pos: 388
Relay_Log_File: relay-log.000010
Relay_Log_Pos: 614
Relay_Master_Log_File: ubuntu2204-bin.000005
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: 388
Relay_Log_Space: 992
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: 200
Master_UUID: 8a22d97f-576e-11ed-8984-000c29a89959
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: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)

11.#在主节点导入数据库
[root@ubuntu2204 ~]#mysql -p123456 < hellodb_innodb.sql

12.#在从节点加入数据,查看主从数据不一致
mysql> use hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
#从节点
mysql> insert teachers(name,age)values('q',22);
Query OK, 1 row affected (0.01 sec)

mysql> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | chenxiyu | 19 | NULL |
| 6 | chenxiao | 30 | NULL |
| 7 | c | 30 | NULL |
| 8 | p | 48 | NULL |
| 9 | q | 22 | NULL |
+-----+---------------+-----+--------+
9 rows in set (0.00 sec)

#从节点有两个线程
mysql> show processlist;
+----+-----------------+-----------------+---------+---------+--------+----------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+---------+---------+--------+----------------------------------------------------------+------------------+
| 5 | event_scheduler | localhost | NULL | Daemon | 153152 | Waiting on empty queue | NULL |
| 23 | system user | connecting host | NULL | Connect | 152161 | Waiting for source to send event | NULL |
| 24 | system user | | NULL | Query | 665 | Replica has read all relay log; waiting for more updates | NULL |
| 25 | system user | | NULL | Query | 665 | Waiting for an event from Coordinator | NULL |
| 26 | system user | | NULL | Connect | 152161 | Waiting for an event from Coordinator | NULL |
| 27 | system user | | NULL | Connect | 152161 | Waiting for an event from Coordinator | NULL |
| 28 | system user | | NULL | Connect | 152161 | Waiting for an event from Coordinator | NULL |
| 31 | root | localhost | NULL | Sleep | 18608 | | NULL |
| 32 | root | localhost | hellodb | Query | 0 | init | show processlist |
+----+-----------------+-----------------+---------+---------+--------+----------------------------------------------------------+------------------+
9 rows in set (0.01 sec)

#主节点
mysql> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | chenxiyu | 19 | NULL |
| 6 | chenxiao | 30 | NULL |
| 7 | c | 30 | NULL |
| 8 | p | 48 | NULL |
+-----+---------------+-----+--------+
8 rows in set (0.00 sec)
#主节点有一个线程
mysql> show processlist;
+----+-----------------+------------------+---------+-------------+-------+-----------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+------------------+---------+-------------+-------+-----------------------------------------------------------------+------------------+
| 5 | event_scheduler | localhost | NULL | Daemon | 15486 | Waiting on empty queue | NULL |
| 9 | repluser | 10.0.0.100:40864 | NULL | Binlog Dump | 15429 | Source has sent all binlog to replica; waiting for more updates | NULL |
| 11 | root | localhost | hellodb | Sleep | 366 | | NULL |
| 12 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+-----------------+------------------+---------+-------------+-------+-----------------------------------------------------------------+------------------+
4 rows in set (0.00 sec)

13.在从节点上重新执行节点备份到拷贝到次节点
[root@ubuntu2004 ~]#mysqldump -uroot -p123456 -A --single-transaction --source-data=1 -f > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

[root@ubuntu2004 ~]#scp all.sql 10.0.0.161:
root@10.0.0.161's password: all.sql 100% 2163KB 26.6MB/s 00:00

14.二个从节点配置,执行server配置并关闭二进制日志
[root@rocky8 ~]#vim /etc/my.cnf
[client-server]
server-id=161
log-bin

15清空之前的记录
mysql> reset slave all;

[root@rocky8 ~]#mysql
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.01 sec)

16导入数据
mysql> source all.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
..............................................

17#开启二进制日志,执行看线程没有,因为缺少步骤,操作如下步骤即可
#空之前的记录mysql> reset slave all;
mysql> set sql_log_bin=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
Empty set, 1 warning (0.01 sec)
注意:#级联复制中第二节点要连接到中间节点而不是主节点
mysql> CHANGE MASTER TO MASTER_HOST='10.0.0.100',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='123456',
-> MASTER_LOG_FILE='ubuntu2004-bin.000004', MASTER_LOG_POS=2002;
Query OK, 0 rows affected, 8 warnings (0.01 sec)

mysql> show replica status\G;
*************************** 1. row ***************************
Replica_IO_State: Waiting for source to send event
Source_Host: 10.0.0.100
Source_User: repluser
Source_Port: 3306
Connect_Retry: 60
Source_Log_File: ubuntu2004-bin.000004
Read_Source_Log_Pos: 2002
Relay_Log_File: rocky8-relay-bin.000002
Relay_Log_Pos: 331
Relay_Source_Log_File: ubuntu2004-bin.000004
Replica_IO_Running: Yes
Replica_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_Source_Log_Pos: 2002
Relay_Log_Space: 542
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Source_SSL_Allowed: No
Source_SSL_CA_File:
Source_SSL_CA_Path:
Source_SSL_Cert:
Source_SSL_Cipher:
Source_SSL_Key:
Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Source_Server_Id: 100
Source_UUID: 81e38a3f-5e97-11ed-b23e-000c2955b8f0
Source_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
Source_Retry_Count: 86400
Source_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Source_SSL_Crl:
Source_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Source_TLS_Version:
Source_public_key_path:
Get_Source_public_key: 0
Network_Namespace:
1 row in set (0.01 sec)

ERROR:
No query specified
今天分享的内容就到这里,谢谢各位观看!

有关mysql数据库的级联复制的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  3. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  4. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  5. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  6. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  7. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

  8. STM32读取串口传感器数据(颗粒物传感器,主动上传) - 2

    文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,

  9. SPI接收数据异常问题总结 - 2

    SPI接收数据左移一位问题目录SPI接收数据左移一位问题一、问题描述二、问题分析三、探究原理四、经验总结最近在工作在学习调试SPI的过程中遇到一个问题——接收数据整体向左移了一位(1bit)。SPI数据收发是数据交换,因此接收数据时从第二个字节开始才是有效数据,也就是数据整体向右移一个字节(1byte)。请教前辈之后也没有得到解决,通过在网上查阅前人经验终于解决问题,所以写一个避坑经验总结。实际背景:MCU与一款芯片使用spi通信,MCU作为主机,芯片作为从机。这款芯片采用的是它规定的六线SPI,多了两根线:RDY和INT,这样从机就可以主动请求主机给主机发送数据了。一、问题描述根据从机芯片手

  10. 微信小程序通过字典表匹配对应数据 - 2

    前言一般来说,前端根据后台返回code码展示对应内容只需要在前台判断code值展示对应的内容即可,但要是匹配的code码比较多或者多个页面用到时,为了便于后期维护,后台就会使用字典表让前端匹配,下面我将在微信小程序中通过wxs的方法实现这个操作。为什么要使用wxs?{{method(a,b)}}可以看到,上述代码是一个调用方法传值的操作,在vue中很常见,多用于数据之间的转换,但由于微信小程序诸多限制的原因,你并不能优雅的这样操作,可能有人会说,为什么不用if判断实现呢?但是if判断的局限性在于如果存在数据量过大时,大量重复性操作和if判断会让你的代码显得异常冗余。wxswxs相当于是一个独立

随机推荐