雙主模型、SSL、percona-toolkit、MySQL複製概念深刻

【減輕複製壓力】複製過濾器,指定須要複製的白名單,或者須要忽略的黑名單node

[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:mysql

[mysqld]sql


master:數據庫

binlog_do_db=vim

binlog_ignore_db=緩存


slave:安全

replicate_do_db=bash

replicate_ignore_db=服務器


replicate_do_table= db_name.table_nameapp

replicate_ignore_table=


replicate_wild_do_table=

replicate_wild_ignore_table=






六、雙主模型


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



若是數據不一致,解決辦法

一、從新備份並在從服務器導入數據;

二、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

相關文章
相關標籤/搜索