mysql数据库的级联复制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据库的级联复制相关的知识,希望对你有一定的参考价值。
今天分享的是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) cant 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.100s 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.161s 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数据库的级联复制的主要内容,如果未能解决你的问题,请参考以下文章