环境:centos-6.5 Mariadb:10.1.13-MariaDB
多源复制:企业数据库中写的需求较大,以至于I/O负载比较大,那么就必须把写的操作分摊到多台主服务器上进行,然后在将主服务器上的数据汇总到从服务器上去进行数据分析。或者是将异地数据库的数据汇总到一起。注意:每台主服务上的库是不能相同的。
proxy-mysql:192.168.88.139
master1:192.168.88.147
master2:192.168.88.148
slave1:192.168.88.149
slave2:192.168.88.150
一、1.编辑master1的配置文件:
1 编辑/etc/my.cnf,修改如下两行: 2 [root@www ~]# vim /etc/my.cnf 3 log-bin = /mylogs/mysql-bin #二进制日志存放位置 4 server-id = 10 #mysql服务器id,同一集群里的所有server-id都不能相同 5 6
2.创建二进制日志目录,并重启:
1 [root@www ~]# mkdir /mylogs
2 [root@www ~]# chown mysql.mysql -R /mylogs 3 [root@www ~]# service mysqld restart
3.连接mysql服务器,进行复制用户授权:
1 MariaDB [(none)]> grant replication slave,replication client on *.* to 'daixiang'@'192.168.88.%' identified by 'daixiang'; #在生产环境,为了安全起见,千万记住要指定单台被授权的主机
2 MariaDB [(none)]> flush privileges;
二、1.编辑master2的配置文件:
1 编辑/etc/my.cnf,修改如下两行:
2 [root@www ~]# vim /etc/my.cnf
3 log-bin = /mylogs/mysql-bin #二进制日志存放位置
4 server-id = 20 #mysql服务器id,同一集群里的所有server-id都不能相同
5
6
2.创建二进制日志目录,并重启:
1 [root@www ~]# mkdir /mylogs
2 [root@www ~]# chown mysql.mysql -R /mylogs
3 [root@www ~]# service mysqld restart
3.连接mysql服务器,进行复制用户授权:
1 MariaDB [(none)]> grant replication slave,replication client on *.* to 'daixiang'@'192.168.88.%' identified by 'daixiang';
2 MariaDB [(none)]> flush privileges;
三、1.编辑slave1配置文件并重启mysql:
1 编辑/etc /my.cnf修改如下几行: 2 [root@www ~]# vim /etc/my.cnf 3 server-id = 30 4 relay-log = relay-mysql #启用中继日志 5 #log-bin=mysql-bin #不启用二进制日志
6 [root@www ~]# service mysqld restart
2.连接slave1,分别指定master1和master2的位置,并启动I/O_thread和SQL_thread:
1 MariaDB [(none)]> change master 'm1' to MASTER_HOST='192.168.88.147',MASTER_USER='daixiang',MASTER_PASSWORD='daixiang'; 2 MariaDB [(none)]> change master 'm2' to MASTER_HOST='192.168.88.148',MASTER_USER='daixiang',MASTER_PASSWORD='daixiang'; #分别指定主服务器并设置别名为m1和m2
3 MariaDB [(none)]> start all slaves;
4 MariaDB [(none)]> help change master to; #查看change master to命令用法
3.查看从服务器slave1的状态:
MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.88.147 Master_User: daixiang Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 586 Relay_Log_File: relay-mysql.000006 Relay_Log_Pos: 874 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes #表示I/O_thread启动成功 Slave_SQL_Running: Yes #表示SQL_thread启动成功 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: #如果此处出现错误提示,一般情况是需要指定主服务器上position:下面有详细解释 Skip_Counter: 0 Exec_Master_Log_Pos: 586 Relay_Log_Space: 1736 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: 10 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative 1 row in set (0.00 sec)
#注意:如果上面启动复制线程和执行中继日志语句线程时,使用show all slaves status\G时,出现错误提示;一般情况是因为初始化数据库时,主从服务器上的库是相同的,而从服务器是从最开始的位置同步主服务器上的二进制日志进自己中继日志并执行,那么从服务器上原本就已经有了系统库,如果在执行一遍中继日志那么就会产生冲突,所以必须要指定position,我使用的是Mariadb:10.1.13-MariaDB,很幸运,没出现此错误:
解决方法:1.切换到主服务器上查看其状态:
MariaDB [mage]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000005 | 721 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
2.在到从服务器上执行下面语句:
MariaDB [(none)]> STOP ALL SLAVES;
MariaDB [(none)]> change master 'm1' to MASTER_HOST='192.168.88.147',MASTER_USER='daixiang',MASTER_PASSWORD='daixiang',MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=721;
MariaDB [(none)]> START ALL SLAVES;
四、1.编辑slave2配置文件并重启mysql:
1 编辑/etc /my.cnf修改如下几行:
2 [root@www ~]# vim /etc/my.cnf
3 server-id = 40
4 relay-log = relay-mysql #启用中继日志
5 #log-bin=mysql-bin #不启用二进制日志
6 [root@www ~]# service mysqld restart
2.连接slave1,分别指定master1和master2的位置,并启动I/O_thread和SQL_thread:
1 MariaDB [(none)]> change master 'm1' to MASTER_HOST='192.168.88.147',MASTER_USER='daixiang',MASTER_PASSWORD='daixiang';
2 MariaDB [(none)]> change master 'm2' to MASTER_HOST='192.168.88.148',MASTER_USER='daixiang',MASTER_PASSWORD='daixiang';
3 MariaDB [(none)]> start all slaves;
MariaDB [(none)]> help change master to; #查看change master to命令用法
3.查看从服务器slave2的状态:
MariaDB [(none)]> show all slaves status\G *************************** 1. row *************************** Connection_name: m1 Slave_SQL_State: Slave has read all relay log; waiting for the slave I/O thread to update it Slave_IO_State: Waiting for master to send event Master_Host: 192.168.88.147 #master1的地址 Master_User: daixiang Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 586 Relay_Log_File: mysql-relay-m1.000006 Relay_Log_Pos: 874 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes #已启动I/O_thread线程 Slave_SQL_Running: Yes #已启动SQL_thread线程 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: 586 Relay_Log_Space: 1739 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: 10 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative Retried_transactions: 0 Max_relay_log_size: 1073741824 Executed_log_entries: 37 Slave_received_heartbeats: 8 Slave_heartbeat_period: 1800.000 Gtid_Slave_Pos: 0-20-3 *************************** 2. row *************************** Connection_name: m2 Slave_SQL_State: Slave has read all relay log; waiting for the slave I/O thread to update it Slave_IO_State: Waiting for master to send event Master_Host: 192.168.88.148 #master2的地址 Master_User: daixiang Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 585 Relay_Log_File: mysql-relay-m2.000006 Relay_Log_Pos: 873 Relay_Master_Log_File: mysql-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: 585 Relay_Log_Space: 1738 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: 20 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative Retried_transactions: 0 Max_relay_log_size: 1073741824 Executed_log_entries: 37 Slave_received_heartbeats: 8 Slave_heartbeat_period: 1800.000 Gtid_Slave_Pos: 0-20-3 2 rows in set (0.00 sec)
五、验证:
1.在master1上创建一个库dxdb ,在dxdb库里面创建表daixiang,并在里面插入一些数据:
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | dxdb | | information_schema | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.05 sec) MariaDB [(none)]> use dxdb Database changed MariaDB [dxdb]> show tables; +----------------+ | Tables_in_dxdb | +----------------+ | daixiang | +----------------+ 1 row in set (0.00 sec)
MariaDB [dxdb]> select * from daixiang;
+------+
| name |
+------+
| tom |
| jeck |
+------+
2 rows in set (0.01 sec)
2.在master2上创建一个库mage_db,在mage_db库里面创建表mage_tb,并在里面插入一些数据:
MariaDB [mage]> show databases; +------------------------+ | Database | +------------------------+ | information_schema | | mage_db | | mysql | | performance_schema | | test | +------------------------+ 5 rows in set (0.00 sec)
3.在slave1上验证是否已经成功汇总:
MariaDB [mage]> show databases; +--------------------+ | Database | +--------------------+ | dxdb | | information_schema | | mage_db | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec)
4.在slave2上验证是否已经成功汇总:
MariaDB [mage]> show databases; +--------------------+ | Database | +--------------------+ | dxdb | | information_schema | | mage | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec)