【减轻复制压力】复制过滤器,指定需要复制的白名单,或者需要忽略的黑名单

[root@localhost ~]# cd /etc/
[root@localhost etc]# cp my.cnf{,.master}
[root@localhost etc]# ll my.cnf*
-rw-r--r--. 1 root root 4686 10月 13 04:43 my.cnf
-rw-r--r--. 1 root root 4686 10月 14 20:00 my.cnf.master

vim my.cnf:

[mysqld]


master:

binlog_do_db=

binlog_ignore_db=


slave:

replicate_do_db=

replicate_ignore_db=


replicate_do_table= db_name.table_name

replicate_ignore_table=


replicate_wild_do_table=

replicate_wild_ignore_table=






6、双主模型


1)、在两台服务器上各自建立一个具有复制权限的用户;

2)、修改配置文件:

确保双方服务器数据一致,才能这么做!,本演示是在以前的一主一从的拓扑演变而来



#【主服务器A上】

[root@localhost etc]# cp /etc/my.cnf /etc/my.cnf.master
[root@localhost etc]# vim /etc/my.cnf +61
[mysqld]
datadir= /mydata/data
server-id = 1
log-bin=/mydata/binlogs/master-bin
relay_log = /mydata/relaylogs/relay-bin
auto-increment-offset = 1	 #注意起始值顺序不要写反
auto-increment-increment = 2 #注意步长顺序需要写反

skip_slave_start  跳过自动启动slave,因为自动的容易出错

[root@localhost etc]# mkdir -pv /mydata/relaylogs/
mkdir: 已创建目录 "/mydata/relaylogs/"
[root@localhost etc]# chown -R mysql:mysql /mydata/relaylogs/
[root@localhost etc]# service mysqld restart
[root@localhost ~]# mysql -p
Enter password:
mysql> show master status;
保留此时的状态,不要再操作这个mysql服务器
+-------------------+----------+
| File              | Position |
+-------------------+----------+
| master-bin.000007 |      107 |
+-------------------+----------+
1 row in set (0.00 sec)




# 【主服务器B上】

[root@localhost ~]# cp /etc/my.cnf /etc/my.cnf.slave
[mysqld]
server-id = 11
log-bin=/mydata/binlogs/master-log
binlog_format=mixed #启用
# read-only = ON # 注释原来的只读
auto-increment-offset = 2		#注意起始值顺序不要写反
auto-increment-increment = 2	 #注意步长顺序需要写反
skip_slave_start	#跳过自动启动slave,因为自动的容易出错

[root@localhost ~]# mkdir -pv /mydata/binlogs/
mkdir: 已创建目录 "/mydata/binlogs/"
[root@localhost ~]# chown -R mysql:mysql /mydata/binlogs/
[root@localhost etc]# service mysqld restart
[root@localhost ~]# mysql -p
Enter password: 

#为对方创建一个具有所有权限的用户
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.%.%' IDENTIFIED BY 'replpass';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec) 

#检查SLAVE的状态
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
	   Slave_IO_State: 
		  Master_Host: 192.168.81.132
		  Master_User: repluser
		  Master_Port: 3306
		Connect_Retry: 60
	  Master_Log_File: master-bin.000006
  Read_Master_Log_Pos: 107
	   Relay_Log_File: relay-bin.000007
		Relay_Log_Pos: 254
Relay_Master_Log_File: master-bin.000006
	 Slave_IO_Running: No  没启动
	Slave_SQL_Running: No  没启动


用对方服务器192.168.81.132的repluser账户,指向对方服务器的日志文件	
mysql>  CHANGE MASTER TO  MASTER_HOST='192.168.81.132',  MASTER_USER='repluser', MASTER_PASSWORD='replpass',MASTER_LOG_FILE = 'master-bin.000007',MASTER_LOG_POS = 107;
Query OK, 0 rows affected (0.05 sec)
	
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
	   Slave_IO_State: Waiting for master to send event
		  Master_Host: 192.168.81.132
		  Master_User: repluser
		  Master_Port: 3306
		Connect_Retry: 60
	  Master_Log_File: master-bin.000007
  Read_Master_Log_Pos: 107
	   Relay_Log_File: relay-bin.000002
		Relay_Log_Pos: 254
Relay_Master_Log_File: master-bin.000007
	 Slave_IO_Running: Yes
	Slave_SQL_Running: Yes
			
			
查看主服务器B的状态【注意,不要再把服务器B当slave的思维】
mysql> show master status;
+-------------------+----------+
| File              | Position |
+-------------------+----------+
| master-log.000002 |      370 |
+-------------------+----------+
1 row in set (0.00 sec)



【在服务器A操作,把主指向服务器B】注意你的iptables

注意:用对方服务器192.168.81.133的repluser账户,指向对方服务器的日志文件
mysql> CHANGE MASTER TO  MASTER_HOST='192.168.81.133',  MASTER_USER='repluser', MASTER_PASSWORD='replpass',MASTER_LOG_FILE = 'master-log.000002',MASTER_LOG_POS = 370;
Query OK, 0 rows affected (0.03 sec)

mysql> show slave status\G
*************************** 1. row ***************************
	   Slave_IO_State: 
		  Master_Host: 192.168.81.133
		  Master_User: repluser
		  Master_Port: 3306
		Connect_Retry: 60
	  Master_Log_File: master-master-log.000002
  Read_Master_Log_Pos: 370
	   Relay_Log_File: relay-bin.000001
		Relay_Log_Pos: 4
Relay_Master_Log_File: master-master-log.000002
	 Slave_IO_Running: No
	Slave_SQL_Running: No
			
启动互为主从
mysql> start slave ;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
	   Slave_IO_State: Waiting for master to send event
		  Master_Host: 192.168.81.133
		  Master_User: repluser
		  Master_Port: 3306
		Connect_Retry: 60
	  Master_Log_File: master-log.000002
  Read_Master_Log_Pos: 370
	   Relay_Log_File: relay-bin.000002
		Relay_Log_Pos: 254
Relay_Master_Log_File: master-log.000002
	 Slave_IO_Running: Yes
	Slave_SQL_Running: Yes

			
【测试】在服务器A上创建一个数据库,并使用这个库创建自动增长的表
mysql> create database newdb;
Query OK, 1 row affected (0.00 sec)



【在服务器B上】查看是否存在

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| haha               |
| hellodb            |
| mysql              |
| newdb              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.00 sec)

mysql> use newdb;
Database changed
mysql> create table t1(id int unsigned not null primary key auto_increment,name char(20));
Query OK, 0 rows affected (0.05 sec)
mysql> insert into t1 (name) values ('tom'),('jerry');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+----+-------+
| id | name  |
+----+-------+
|  2 | tom   |
|  4 | jerry |
+----+-------+
2 rows in set (0.00 sec)



【测试】在服务器A器上操作

mysql> use newdb;
Database changed
mysql> insert into t1 (name) values ('user1'),('user2');
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+----+-------+
| id | name  |
+----+-------+
|  2 | tom   |
|  4 | jerry |
|  5 | user1 |
|  7 | user2 |
+----+-------+
4 rows in set (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                5 |
+------------------+
1 row in set (0.00 sec)

【注意】如果再次修改过mysqld配置文件。要手动启动slave
[root@localhost etc]# service mysqld restart
[root@localhost ~]# mysql -p
Enter password: 
mysql> start slave;
Query OK, 0 rows affected (0.05 sec)
mysql> create table t2 like t1;  | 或者mysql> truncate t2;清空自动增长
Query OK, 0 rows affected (0.02 sec)
mysql> select * from t2;
+----+-------+
| id | name  |
+----+-------+
|  1 | user1 |
|  3 | user2 |
+----+-------+
2 rows in set (0.01 sec)


最简单的双主模型 就是这样!】





推荐:

多主,且高可用的解决方案:

MMM:Multi Master MySQL

MHA:MySQL HA




基于ssl的复制【大概的讲一下】

mysql> show variables like "%ssl%";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| have_openssl  | NO    |
| have_ssl      | NO    |
| ssl_ca        |       |
| ssl_capath    |       |
| ssl_cert      |       |
| ssl_cipher    |       |
| ssl_key       |       |
+---------------+-------+
7 rows in set (0.00 sec)

需要编译安装好后,才能enable ssl的功能
client发送证书给server
server验证客户端

使用方法
mysql> help change master to
Syntax:
CHANGE MASTER TO option [, option] ...

option:
    MASTER_BIND = 'interface_name'
  | MASTER_HOST = 'host_name'
  | MASTER_USER = 'user_name'
  | MASTER_PASSWORD = 'password'
  | MASTER_PORT = port_num
  | MASTER_CONNECT_RETRY = interval
  | MASTER_HEARTBEAT_PERIOD = interval
  | MASTER_LOG_FILE = 'master_log_name'
  | MASTER_LOG_POS = master_log_pos
  | RELAY_LOG_FILE = 'relay_log_name'
  | RELAY_LOG_POS = relay_log_pos
  | MASTER_SSL = {0|1}
  | MASTER_SSL_CA = 'ca_file_name'
  | MASTER_SSL_CAPATH = 'ca_directory_name'
  | MASTER_SSL_CERT = 'cert_file_name'
  | MASTER_SSL_KEY = 'key_file_name'
  | MASTER_SSL_CIPHER = 'cipher_list'
  | MASTER_SSL_VERIFY_SERVER_CERT = {0|1}
  | IGNORE_SERVER_IDS = (server_id_list)

server_id_list:
    [server_id [, server_id] ... ]		



复制相关的文件:

master.info: 文本文件,保存从服务器连接至主服务时所需要的信息,每行一个值;

有了这些才会自动启动change master to,删除了就不会自动连接了

relay-log.info: 文本文件,保存了复制位置:包括二进制日志和中继日志的文件及位置;

[root@localhost data]# cat /mydata/data/master.info 
18
master-log.000003
107
192.168.81.133
repluser
replpass
3306
60
0





0
1800.000

0
000

0


[root@localhost data]# cat /mydata/data/relay-log.info 
/mydata/relaylogs/relay-bin.000007
254
master-log.000003
107

370


[Action !]

这两个文件是实时更新的,如果遇到这种情况,maser服务器特别繁忙,

那么这两个文件会被频繁的修改,那么I/0的性能一定会下降

mysql> show global variables like "sync%";
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| sync_binlog         | 0     |
| sync_frm            | ON    |
| sync_master_info    | 0     |
| sync_relay_log      | 0     |
| sync_relay_log_info | 0     |
+---------------------+-------+
5 rows in set (0.05 sec)


为了复制的安全性:

	sync_master_info = 1
	sync_relay_log = 1
	sync_relay_log_info = 1





安装percona-toolkit

[root@localhost home]# yum install ./percona-toolkit-2.2.4-1.noarch.rpm


使用方法

[root@localhost home]# pt-heartbeat -h
[root@localhost home]# pt  工具这么多
pt-agent                  pt-fingerprint            pt-pmp                    pt-table-checksum
pt-align                  pt-fk-error-logger        pt-query-digest           pt-table-sync
pt-archiver               pt-heartbeat              pt-show-grants            pt-table-usage
pt-config-diff            pt-index-usage            pt-sift                   pt-upgrade
pt-deadlock-logger        pt-ioprofile              pt-slave-delay            pt-variable-advisor
pt-diskstats              pt-kill                   pt-slave-find             pt-visual-explain
pt-duplicate-key-checker  pt-mext                   pt-slave-restart          ptx
pt-fifo-split             pt-mysql-summary          pt-stalk                  
pt-find                   pt-online-schema-change   pt-summary


查看配置信息

[root@localhost home]# pt-summary 
# Percona Toolkit System Summary Report ######################
        Date | 2015-10-17 09:25:30 UTC (local TZ: CST +0800)
    Hostname | localhost.localdomain
      Uptime | 11:59,  3 users,  load average: 0.00, 0.00, 0.00
    Platform | Linux
     Release | CentOS release 6.7 (Final)
      Kernel | 2.6.32-573.el6.x86_64
Architecture | CPU = 64-bit, OS = 64-bit
   Threading | NPTL 2.12
     SELinux | Enforcing
 Virtualized | VMWare
# Processor ##################################################
  Processors | physical = 2, cores = 4, virtual = 4, hyperthreading = no
      Speeds | 4x2195.089
      Models | 4xIntel(R) Core(TM) i3-2328M CPU @ 2.20GHz
      Caches | 4x3072 KB
# Memory #####################################################
       Total | 2.0G
        Free | 1.5G
        Used | physical = 487.5M, swap allocated = 2.1G, swap used = 0.0, virtual = 487.5M
     Buffers | 50.1M
      Caches | 181.6M
       Dirty | 12 kB
     UsedRSS | 157.0M
  Swappiness | 60
 DirtyPolicy | 20, 10
 DirtyStatus | 0, 0
# Mounted Filesystems ########################################
  Filesystem                    Size Used Type  Opts                                          Mountpoint
  /dev/mapper/VolGroup-lv_home   37G   1% ext4  rw                                            /home
  /dev/mapper/VolGroup-lv_root   50G   5% ext4  rw                                            /
  /dev/sda1                     477M   9% ext4  rw                                            /boot
  tmpfs                        1002M   0% tmpfs rw,rootcontext="system_u:object_r:tmpfs_t:s0" /dev/shm
# Disk Schedulers And Queue Size #############################
        dm-0 | 128
        dm-1 | 128
        dm-2 | 128
         sda | [cfq] 128
         sr0 | [cfq] 128
# Disk Partioning ############################################
Device       Type      Start        End               Size
============ ==== ========== ========== ==================
/dev/dm-0    Disk                              53687091200
/dev/dm-1    Disk                               2248146944
/dev/dm-2    Disk                              40173043712
/dev/sda     Disk                              96636764160
/dev/sda1    Part          1         64          518192640
/dev/sda2    Part         64      11749        96112396800
/dev/sr0     Disk                               3895459840
/dev/sr0p1   Part          1       3715        15577645056
# Kernel Inode State #########################################
dentry-state | 45417	37192	45	0	0	0
     file-nr | 768	0	200279
    inode-nr | 41113	103
# LVM Volumes ################################################
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_home VolGroup -wi-ao---- 37.41g                                                    
  lv_root VolGroup -wi-ao---- 50.00g                                                    
  lv_swap VolGroup -wi-ao----  2.09g                                                    
# LVM Volume Groups ##########################################
  VG       VSize  VFree
  VolGroup 89.51g    0 
# RAID Controller ############################################
  Controller | No RAID controller detected
# Network Config #############################################
 FIN Timeout | 60
  Port Range | 61000
# Interface Statistics #######################################
  interface  rx_bytes rx_packets  rx_errors   tx_bytes tx_packets  tx_errors
  ========= ========= ========== ========== ========== ========== ==========
  lo              900         20          0        900         20          0
  eth0        7000000      20000          0    5000000      17500          0
# Network Devices ############################################
  Device    Speed     Duplex
  ========= ========= =========
  eth0       1000Mb/s   Full      
# Network Connections ########################################
  Connections from remote IP addresses
    192.168.81.1        3
    192.168.81.133      1
  Connections to local IP addresses
    192.168.81.132      4
  Connections to top 10 local ports
    22                  3
    50606               1
  States of connections
    ESTABLISHED         4
    LISTEN              5
# Top Processes ##############################################
   PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
     1 root      20   0 19232 1520 1228 S  0.0  0.1   0:03.21 init
     2 root      20   0     0    0    0 S  0.0  0.0   0:00.04 kthreadd
     3 root      RT   0     0    0    0 S  0.0  0.0   0:00.61 migration/0
     4 root      20   0     0    0    0 S  0.0  0.0   0:00.30 ksoftirqd/0
     5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/0
     6 root      RT   0     0    0    0 S  0.0  0.0   0:00.19 watchdog/0
     7 root      RT   0     0    0    0 S  0.0  0.0   0:00.80 migration/1
     8 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 stopper/1
     9 root      20   0     0    0    0 S  0.0  0.0   0:00.12 ksoftirqd/1
# Notable Processes ##########################################
  PID    OOM    COMMAND
11078    -17    sshd
# Simplified and fuzzy rounded vmstat (wait please) ##########
  procs  ---swap-- -----io---- ---system---- --------cpu--------
   r  b    si   so    bi    bo     ir     cs  us  sy  il  wa  st
   1  0     0    0     1     1      5      6   0   0 100   0   0
   0  0     0    0    35     0   2250   1250   4   9  88   0   0
   0  0     0    0     0     0      7     10   0   0 100   0   0
   0  0     0    0     0   250     25     45   0   0 100   0   0
   0  0     0    0     0     0      8     15   0   0 100   0   0
# The End ####################################################



从服务器意外崩溃时,建议使用pt-slave-start命令来启动slave;



基于行和基于语句复制:

基于语句:

数据量小;易于查看;适应性强;

有些语句无法做精确复制;无法对使用了触发器、存储过程等代码的应用实现精确复制;


基于行:

能够精确完成有着触发器、存储过程等代码场景中的复制;能完成几乎所有的复制功能;较少的CPU占用率;

无法判断执行了什么样的SQL语句;数据量可能略大;


从服务器落后于主服务器:

Seconds_Behind_Master: 0

查看落后时长,如果落后,请观测此值


mysql> show slave status\G

Seconds_Behind_Master: 0



如果数据不一致,解决办法

1、重新备份并在从服务器导入数据;

2、pt-table-sync


为了提高复制时的数据安全性,在主服务器上的设定

sync_binlog = 1

innodb_flush_log_at_trx_commit = 1

此参数的值设定为1,性能下降会较严重;因此,一般设定为2等,

此时,主服务器崩溃依然有可能导致从服务器无法获取到全部的二进制日志事件;


如果master意外崩溃导致二进制日志中的某事件损坏,可以在从服务器使用如下参数忽略:

sql_slave_skip_counter = 0



第三方复制解决方案:Tungsten, Galera







复制要考虑的事儿:

何为雪崩效应?

如何缓存,高效命中缓存?[轮巡,对Sql语句hash取模?]

从服务器写入的压力并不比主服务器的压力小,

事务的手动自动提交,频繁同步到磁盘、

二进制日志文件的频繁写入读出,中继日志的写入读出,

SQl I/O <---> Slave 单线程忙碌

从服务器落后于主服务器的必然,

中途断电,缓存中的二进制文件未写入到磁盘,怎么办?

长途跨地,如何同步数据?

有了这位么多的从服务器,数据还需要备份吗?





2015.10.18