过滤复制:只将某一个数据库备份到从库
1登陆从库查看从库里面库级别的白名单:
[root@instance-r5y0pf5d ~]# mysql -uroot -p123456 -S /data/3308/mysql.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.7.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 106.13.186.56
Master_User: repl
Master_Port: 3307
Connect_Retry: 10
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1988
Relay_Log_File: instance-r5y0pf5d-relay-bin.000002
Relay_Log_Pos: 470
Relay_Master_Log_File: mysql-bin.000003
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: 1988
Relay_Log_Space: 689
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: 7
Master_UUID: 213ccec3-75a8-11eb-b95e-fa270000fffa
Master_Info_File: /data/3308/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave 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: 213ccec3-75a8-11eb-b95e-fa270000fffa:8
Executed_Gtid_Set: 213ccec3-75a8-11eb-b95e-fa270000fffa:1-8
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql>
2如果我们只想接收xyz这个库,那么我们只需要在从库的配置文件/data/3308/data/my.cnf中添加如下参数(注意是小写字母)后,重启数据库即可:
replicate_do_db=xyz
具体操作步骤如下:
[root@instance-r5y0pf5d ~]# vim /data/3308/my.cnf
[root@instance-r5y0pf5d ~]# cat /data/3308/my.cnf
[mysqld]
user=mysql
basedir=/application/mysql
datadir=/data/3308/data
socket=/data/3308/mysql.sock
server_id=8
port=3308
gtid-mode=on
enforce-gtid-consistency=true
log_error=/data/3308/data/mysql.err
log_bin=/data/3308/mysql-bin
secure-file-priv=/tmp
innodb_flush_method=O_DIRECT
lower_case_table_names=1
slow_query_log=1
slow_query_log_file=/data/3308/data/slow.log
long_query_time=0.1
log_queries_not_using_indexes
replicate_do_db=xyz
[client]
port=3308
socket=/data/3308/mysql.sock
[mysql]
socket=/data/3308/mysql.sock
[root@instance-r5y0pf5d ~]# systemctl restart mysqld3308.service
[root@instance-r5y0pf5d ~]#
3登陆从库数据库后通过sow slave status \G查看配置是否生效:
[root@instance-r5y0pf5d ~]# mysql -uroot -p123456 -S /data/3308/mysql.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 106.13.186.56
Master_User: repl
Master_Port: 3307
Connect_Retry: 10
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1988
Relay_Log_File: instance-r5y0pf5d-relay-bin.000004
Relay_Log_Pos: 360
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: xyz
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: 1988
Relay_Log_Space: 619
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: 7
Master_UUID: 213ccec3-75a8-11eb-b95e-fa270000fffa
Master_Info_File: /data/3308/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave 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: 213ccec3-75a8-11eb-b95e-fa270000fffa:8
Executed_Gtid_Set: 213ccec3-75a8-11eb-b95e-fa270000fffa:1-8
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
mysql>
4在主库中创建一个xyz库,看看从库是否能同步过来,当我们在主库中创建xyz数据库后,就能在从库中查到这个数据库了
|
mysql -uroot -p123456 -S /data/3307/mysql.sock |
mysql -uroot -p123456 -S /data/3308/mysql.sock |
|
mysql> show databases;
mysql> create database xyz; mysql> create database abb; mysql> |
mysql> show databases; mysql> show databases; mysql> |
5我们校验一下是哪个线程执行的过滤任务的:
如果在io线程过滤的,那么从库中的master_log_file和read_master_log_pos的值是不变的,如果是在sql线程过滤的,那么从库中的这两个参数的值会变化的。下面的实例证明当我在主库里进行操作的时候,从库里的master_log_file和read_master_log_pos的值是变化的,也就是说,日志已经全部被复制到了从库只是有一部分日志没有运行,没有运行的日志就是被sql线程回放的时候过滤掉的:
|
mysql -uroot -p123456 -S /data/3307/mysql.sock |
mysql -uroot -p123456 -S /data/3308/mysql.sock |
|
mysql> create database abb; mysql> create database acc; mysql> |
mysql> show slave status \G mysql> show slave status \G mysql> |

浙公网安备 33010602011771号