MySQL 多源數據合併到一個db的兩種方式(mysqldump 和xtrabackup )

server-id = 330
log-bin = mysql-bin           
binlog_format=row  



grant replication slave on *.* to repl identified by '123456';

 systemctl stop firewalld.service
 systemctl disable firewalld.service
 
 
 
 mysqldump -h127.0.0.1 --master-data=2 --single-transaction --databases  --add-drop-database  db1  >db1.sql
  mysqldump -h127.0.0.1 --master-data=2 --single-transaction --databases  --add-drop-database  db2  >db2.sql
  

    scp db1.sql 192.168.56.103:~
	scp db2.sql 192.168.56.103:~
	
	
CHANGE MASTER TO MASTER_HOST='192.168.56.101',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='on.000002', MASTER_LOG_POS=362 FOR CHANNEL 'Master_1';
CHANGE MASTER TO MASTER_HOST='192.168.56.102',MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_LOG_FILE='on.000001', MASTER_LOG_POS=801 FOR CHANNEL 'Master_2'; 	


-- CHANGE MASTER TO MASTER_LOG_FILE='on.000001', MASTER_LOG_POS=593;
-- CHANGE MASTER TO MASTER_LOG_FILE='on.000001', MASTER_LOG_POS=593;



 start slave for CHANNEL  'Master_1';
 

 start slave for CHANNEL  'Master_2';
 
 
 create table test2(id int auto_increment  primary key,addr varchar(32));
 
 sed -i 's/db1/db3/g' db1.sql 
 sed -i 's/db2/db3/g' db2.sql 
 
 
 
 
 
 配置slave:
 [root@test3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server-id = 430
log-bin = on
binlog_format=row

master_info_repository=TABLE
relay_log_info_repository=TABLE

#ignore
replicate_wild_ignore_table= 'mysql.%'
replicate_wild_ignore_table= 'information_schema.%'
replicate_wild_ignore_table= 'performance_schema.%'
replicate_wild_ignore_table= 'sys.%'
replicate_wild_ignore_table= 'test.%'

#rewrite

rds3.replicate-rewrite-db = db1->db3
rds3.replicate-rewrite-db = db2->db3

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[root@test3 ~]# 

主二:
[root@test2 mysql]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server-id = 331
log-bin = on
binlog_format=row

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[root@test2 mysql]# 

主一:
[root@test1 mysql]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server-id = 330
log-bin = on 
binlog_format=row 

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[root@test1 mysql]# 






以下是xtrabackup處理,單標獨立表空間


配置文件主一:
[root@test1 ~]# cat /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server-id = 330
log-bin = on 
binlog_format=row 
innodb_file_per_table = 1

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[root@test1 ~]# 

配置文件主二:
[root@test2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server-id = 331
log-bin = on
binlog_format=row
innodb_file_per_table = 1

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[root@test2 ~]# 

配置文件slave多源:
[root@test3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server-id = 430
log-bin = on
binlog_format=row
innodb_file_per_table = 1

master_info_repository=TABLE
relay_log_info_repository=TABLE

#ignore
replicate_wild_ignore_table= 'mysql.%'
replicate_wild_ignore_table= 'information_schema.%'
replicate_wild_ignore_table= 'performance_schema.%'
replicate_wild_ignore_table= 'sys.%'
replicate_wild_ignore_table= 'test.%'

#rewrite

rds3.replicate-rewrite-db = db1->db3
rds3.replicate-rewrite-db = db2->db3

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[root@test3 ~]# 




主一:操作
 innobackupex    --databases="db1" --host=127.0.0.1  /data/backups/
 innobackupex --apply-log  2020-03-30_14-51-47/
 cd 2020-03-30_14-51-47/db1/
 scp test1.ibd 192.168.56.103:~
 cat xtrabackup_binlog_info

主二:操作
 innobackupex    --databases="db2" --host=127.0.0.1  /data/backups/
 innobackupex --apply-log  2020-03-30_14-45-03/
 cd 2020-03-30_14-45-03/db2/
 scp test2.ibd 192.168.56.103:~
 cat xtrabackup_binlog_info 


slave:操作
mysql> create database db3;
Query OK, 1 row affected (0.00 sec)

mysql> use db3
Database changed
mysql> create table test2(id int auto_increment primary key,addr varchar(32)) engine=innodb charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE `test1` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `name` varchar(32) DEFAULT NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> alter table test1 discard tablespace;
Query OK, 0 rows affected (0.00 sec)

mysql> alter table test2 discard tablespace; 
Query OK, 0 rows affected (0.00 sec)


系統層面:
 cd /var/lib/mysql/db3/
 cp /root/*.ibd .
 chown mysql.mysql test* 
 
數據庫繼續操作:

mysql> alter table test1 import tablespace;       
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> alter table test2 import tablespace; 
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> select * from test1;
+----+------+
| id | name |
+----+------+
|  1 | xing |
|  2 | zhen |
+----+------+
2 rows in set (0.00 sec)

mysql> select * from test2;
+----+------+
| id | addr |
+----+------+
|  1 | jin  |
|  2 | di   |
+----+------+
2 rows in set (0.00 sec)

mysql> CHANGE MASTER TO MASTER_HOST='192.168.56.101',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='on.000003', MASTER_LOG_POS=660 FOR CHANNEL 'Master_1';
CHANGE MASTER TO MASTER_HOST='192.168.56.102',MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_LOG_FILE='on.000002', MASTER_LOG_POS=657 FOR CHANNEL 'Master_2'; Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> CHANGE MASTER TO MASTER_HOST='192.168.56.102',MASTER_USER='repl', MASTER_PASSWORD='123456', MASTER_LOG_FILE='on.000002', MASTER_LOG_POS=657 FOR CHANNEL 'Master_2'; 
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql>  start slave for CHANNEL  'Master_1';
Query OK, 0 rows affected (0.00 sec)

mysql>  
mysql> 
mysql>  start slave for CHANNEL  'Master_2';
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.101
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: on.000003
          Read_Master_Log_Pos: 932
               Relay_Log_File: test3-relay-bin-master_1.000002
                Relay_Log_Pos: 585
        Relay_Master_Log_File: on.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: mysql.%,information_schema.%,performance_schema.%,sys.%,test.%
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 932
              Relay_Log_Space: 801
              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: 330
                  Master_UUID: 46569fb9-7236-11ea-b7a7-08002717ad31
             Master_Info_File: mysql.slave_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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: (db1,db3),(db2,db3)
                 Channel_Name: master_1
           Master_TLS_Version: 
*************************** 2. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.102
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: on.000002
          Read_Master_Log_Pos: 926
               Relay_Log_File: test3-relay-bin-master_2.000002
                Relay_Log_Pos: 582
        Relay_Master_Log_File: on.000002
             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: mysql.%,information_schema.%,performance_schema.%,sys.%,test.%
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 926
              Relay_Log_Space: 798
              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: 331
                  Master_UUID: 46569fb9-7236-11ea-b7a7-08002717ad32
             Master_Info_File: mysql.slave_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: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: (db1,db3),(db2,db3)
                 Channel_Name: master_2
           Master_TLS_Version: 
2 rows in set (0.00 sec)


主一 增加數據:
mysql> use db1
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 into test1(name) values('xing'),('zhen');

slave 驗證:

mysql> select * from test1;
+----+------+
| id | name |
+----+------+
|  1 | xing |
|  2 | zhen |
|  3 | xing |
|  4 | zhen |
+----+------+
4 rows in set (0.00 sec)


主二 增加數據:
mysql> use db2
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> 
mysql> insert into test2(addr) values('jin'),('di');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> exit
Bye



slave 驗證:

mysql> select * from test2;
+----+------+
| id | addr |
+----+------+
|  1 | jin  |
|  2 | di   |
|  3 | jin  |
|  4 | di   |
+----+------+
4 rows in set (0.00 sec)

mysql>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章