您好,登錄后才能下訂單哦!
1.Percona XtraDB Cluster的搭建
安裝環境:
節點1:A: 192.168.91.18
節點2:B:192.168.91.20
節點3:C:192.168.91.21
innodb引擎層實現的復制
ABC server_id要不一樣
ABC:
下載軟件:
wget http://www.percona.com/downloads/Percona-XtraDB-Cluster-56/Percona-XtraDB-Cluster-5.6.21-25.8/binary/tarball/Percona-XtraDB-Cluster-5.6.21-rel70.1-25.8.938.Linux.x86_64.tar.gz
安裝依賴包:
yum install -y socat
yum install -y perl-DBD-MySQL.x86_64 perl-IO-Socket-SSL.noarch socat.x86_64 nc
(其中nc是一個強大的網絡工具)
yum install -y http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
#安裝xtrabackup備份軟件:
yum list |grep percona-xtrabackup
yum install -y percona-xtrabackup.x86_64
#rpm -qa |grep percona
percona-release-0.1-3.noarch
percona-xtrabackup-2.3.7-2.el6.x86_64
ABC:
解壓PXC包:
tar xf Percona-XtraDB-Cluster-5.6.21-rel70.1-25.8.938.Linux.x86_64.tar.gz
軟鏈接:
ln -s /home/tools/Percona-XtraDB-Cluster-5.6.21-rel70.1-25.8.938.Linux.x86_64 /usr/local/mysql
創建 mysql 的用戶及組
groupadd mysql
useradd –g msyql –s /sbin/nologin –d /usr/local/mysql mysql
創建啟動文件:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
創建 mysql 需要的基本目錄
mkdir -p /data/mysql3306/{data,logs,tmp}
chown -R mysql:mysql *
A 配置文件:
vim /etc/my.cnf
#pxc
default_storage_engine=Innodb
#innodb_locks_unsafe_for_binlog=1
innodb_autoinc_lock_mode=2
wsrep_cluster_name=pxc_cluster #集群名稱
wsrep_cluster_address=gcomm://192.168.91.18,192.168.91.20,192.168.91.21
wsrep_node_address=192.168.91.18
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
#wsrep_provider_options="gcache.size = 1G;debug = yes"
wsrep_provider_options="gcache.size = 1G;"
#wsrep_sst_method=rsync
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sst:147258
B配置文件:
#pxc
default_storage_engine=Innodb
#innodb_locks_unsafe_for_binlog=1
innodb_autoinc_lock_mode=2
wsrep_cluster_name=pxc_cluster
wsrep_cluster_address=gcomm://192.168.91.18,192.168.91.20,192.168.91.21
wsrep_node_address=192.168.91.20
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
#wsrep_provider_options="gcache.size = 1G;debug = yes"
wsrep_provider_options="gcache.size = 1G;"
#wsrep_sst_method=rsync
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sst:147258
C配置文件:
#pxc
default_storage_engine=Innodb
#innodb_locks_unsafe_for_binlog=1
innodb_autoinc_lock_mode=2
wsrep_cluster_name=pxc_cluster
wsrep_cluster_address=gcomm://192.168.91.18,192.168.91.20,192.168.91.21
wsrep_node_address=192.168.91.21
wsrep_provider=/usr/local/mysql/lib/libgalera_smm.so
#wsrep_provider_options="gcache.size = 1G;debug = yes"
wsrep_provider_options="gcache.size = 1G;"
#wsrep_sst_method=rsync
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth=sst:147258
ABC:
初始化:
[root@Darren1 mysql]# ./scripts/mysql_install_db
A:
第一個節點啟動:
/etc/init.d/mysql bootstrap-pxc
Bootstrapping PXC (Percona XtraDB Cluster)Starting MySQL (Percona XtraDB Cluster)......... SUCCESS!
>mysql
delete from mysql.user where user!='root' or host!='localhost';
truncate mysql.db;
drop database test;
grant all on *.* to sst@localhost identified by '147258'; #創建用于xtrabackup的用戶sst,密碼要和my.cnf中對應
flush privileges;
BC:
啟動節點二和節點三:
/etc/init.d/iptables stop
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
[root@Darren2 data]# /etc/init.d/mysqld start
Starting MySQL (Percona XtraDB Cluster).........State transfer in progress, setting sleep higher
... SUCCESS!
[root@Darren3 data]# /etc/init.d/mysqld start
ERROR! MySQL (Percona XtraDB Cluster) is not running, but lock file (/var/lock/subsys/mysql) exists
Starting MySQL (Percona XtraDB Cluster)..................State transfer in progress, setting sleep higher
... SUCCESS!
測試:
A:
root@localhost [testdb]> create database testdb;
root@localhost [testdb]>create table t1(c1 int auto_increment not null,c2 timestamp,primary key(c1));
root@localhost [testdb]>insert into t1 select 1,now();
root@localhost [testdb]>select * from testdb.t1;
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 1 | 2017-03-06 12:29:56 |
+----+---------------------+
B:
root@localhost [testdb]>select * from testdb.t1;
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 1 | 2017-03-06 12:29:56 |
+----+---------------------+
C:
root@localhost [testdb]>select * from testdb.t1;
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 1 | 2017-03-06 12:29:56 |
+----+---------------------+
關閉方式:
關閉:/etc/init.d/mysql stop
全部節點關閉后重啟:
第一個節點啟動的節點:/etc/init.d/mysql bootstrap-pxc
其它節點/etc/init.d/mysql start
SST和IST
State Snapshot Transfer(SST) 全量傳輸
發生在:新節點的加入,或者集群中節點故障(關閉)時間過長
wsrep_sst_method = xtrabackup-v2
這個參數有三個值:
(1)xtrabackup-v2
使用xtrabackup傳輸,需要提前創建用于備份的用戶并制定參數用戶名和密碼:wsrep_sst_auth=sst:147258
(2)rsync:最快的傳輸方式,不需要指定wsrep_sst_auth參數,拷貝數據的時候read-only(flush table with read lock)
(3)mysqldump:不建議使用,數據量大的時候不行,拷貝數據的時候read-only(flush table with read lock)
Incremental state Transfer(IST) 增量傳輸
發生在:一個節點數據的改變,把增量的部分拷貝到另幾個節點,通過一個緩存gcache控制,如果增量大于gcache會選擇全量傳輸,再有在增量小于等于gcache時候,才會選擇增量傳輸。
wsrep_provider_options="gcache.size = 1G"
如果去停止PXC其中的一個節點?
當 wsrep_local_state_comment 的狀態是 Synced 表示三個節點之間數據同步,這樣才能去停止其中一個的服務,滾動重啟;
每個節點能夠離線多長時間計算?
比如說想離線2h,算一下2個小時能夠生成多大的binlog,對應的gcache.size就設置多大。
如一個比較繁忙的訂單系統,5分鐘產生200M的binog,則一個小時產生2.4G,兩個小時4.8G,那么wsrep_provider_options="gcache.size = 6G",gcache是需要實際內存分配的,也不能設置太大,否則會出現oom-kill;
故障恢復后,加入集群的過程分析:
(1)如果數據量不是很大,重新初始化,搞一次SST;
(2)如果數據量很大,用rsync傳輸;
PXC的特點及注意事項:
(1)PCX每個節點都自動配置了自增初始值和步長,跟雙主一樣,這樣是為了防止主鍵沖突;
node1:
auto_increment_offset=1
auto_incremnet_increment=3
node2:
auto_increment_offset=2
auto_incremnet_increment=3
node3:
auto_increment_offset=3
auto_incremnet_increment=3
(2)PCX集群是樂觀控制,事物沖突情況可能發生在commit階段,當多個節點修改同一行數據,只有其中一個節點能夠成功,失敗的節點將終止,并且返回死鎖錯誤代碼:
如:
A:
root@localhost [testdb]>begin;
root@localhost [testdb]>update t1 set c2=now() where c1=3;
B:
root@localhost [testdb]>begin;
root@localhost [testdb]>update t1 set c2=now() where c1=3;
root@localhost [testdb]>commit;
A:
出現報錯deadlock:
root@localhost [testdb]>commit;
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
(3)PXC只支持innodb引擎,mysql庫下的表基本上都是myisam表怎么傳輸呢,PXC雖然不支持myisam表,但是支持DCL語句,如create user,drop user,grant,revoke等,可以通過開啟參數wsrep_replicate_myisam,使pxc支持myisam表,因此當PXC出現數據不一致的時候,首先要查看是否是myisam表;
如:
node1:
root@localhost [testdb]>show create table t2\G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`c1` int(11) NOT NULL AUTO_INCREMENT,
`c2` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`c1`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
root@localhost [testdb]>select * from t2;
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 2 | 2017-03-08 11:41:31 |
+----+---------------------+
在node2和node3節點上面都看不到,因為沒有傳送過來。
(4)PXC每個表必須要有主鍵,如果沒有主鍵,可能造成集群中每個節點的data page里的數據不一樣,select limit 可能在不同的節點產生不同的結果集;
(5)不支持表級鎖 (lock table),所有的DDL操作都是實例級別的鎖,需要用pt-osc工具
如:
例1:
node1:
root@localhost [testdb]>lock table t1 read;
root@localhost [testdb]>insert into t1 select 69,now();
ERROR 1099 (HY000): Table 't1' was locked with a READ lock and can't be updated
node2:節點2仍然可以插入,說明read lock沒有生效
root@localhost [testdb]>insert into t1 select 69,now();
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
例2:
node1:
root@localhost [testdb]>lock table t1 write;
root@localhost [testdb]>insert into t1 select 1,now();
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
root@localhost [testdb]>select * from t1;
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 1 | 2017-03-08 14:59:46 |
+----+---------------------+
node2: 節點二沒有受寫鎖影響,可以讀寫:
root@localhost [testdb]>insert into t1 select 2,now();
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
root@localhost [testdb]>select * from t1;
+----+---------------------+
| c1 | c2 |
+----+---------------------+
| 1 | 2017-03-08 14:59:46 |
| 2 | 2017-03-08 14:59:57 |
+----+---------------------+
(6)不支持XA 事物
(7)query log日志存放在文件中,不能放在表里,即需要指定參數log_output=file;
(8)整個集群的性能/吞吐量由性能最差的節點決定,木桶效應;
不考慮延遲的主從復制:每秒6萬insert,
考慮到延遲的主從復制:每秒3萬insert,
pxc:每秒1萬insert
(9)節點數量是3<=num<=8
(10)腦裂,所以至少需要三個節點,有個仲裁節點,防止腦裂;
演示腦裂:
強制干掉mysql進程:
node2:
[root@Darren1 mysql3306]# kill -9 10014
node3:
[root@Darren3 ~]# kill -9 10115
node1:
root@localhost [(none)]>use testdb;
ERROR 1047 (08S01): Unknown command
腦裂前的值:
show global status like '%wsrep%';
wsrep_local_state_comment | Synced
wsrep_cluster_status | Primary
wsrep_ready | ON
腦裂后的值:
wsrep_local_state_comment | Initialized
wsrep_cluster_status | non-Primary
wsrep_ready | OFF
重啟node2或者node3會報錯:
[root@Darren1 data]# /etc/init.d/mysqld start
ERROR! MySQL (Percona XtraDB Cluster) is not running, but PID file exists
解決方法:重啟node1,然后再重啟node2和node3
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。