部署xtradb cluster時,建議使用3臺及以上服務器。緣由有二:html
1、默認狀況下,若是一個同伴死去或者兩個節點之間的通訊不穩定,兩個節點都將不接受查詢。固然這個能夠經過添加忽略仲裁來解決:node
set globalwsrep_provider_options=」pc.ignore_quorum=true」;mysql
2、當宕掉的那臺啓動時,會進行同步,負責提供數據的節點角色變爲Donor, Donor會有必定的時間沒法寫入。斷開狀況以下:linux
Mysqldump 適合小庫sql
Rsync 複製時間內斷開,速度快數據庫
XtraBackup 短期內斷開,速度略慢bootstrap
node #1centos
hostname:percona1服務器
IP:192.168.70.71ide
node #2
hostname:percona2
IP:192.168.70.72
node #3
hostname:percona3
IP:192.168.70.73
三個節點都安裝了centos系統
防火牆放通了端口:3306,4444,4567和4568
關閉selinux
先安裝percona官方源
yum installhttp://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
安裝epel源,以便安裝依賴包socat
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
安裝xtradb
yum installPercona-XtraDB-Cluster-56
官網安裝參考地址:
https://www.percona.com/doc/percona-xtradb-cluster/5.6/installation/yum_repo.html
官網參考地址:
https://www.percona.com/doc/percona-xtradb-cluster/5.6/howtos/virt_sandbox.html
第一個節點的/etc/my.cnf配置內容以下:
[mysqld]
datadir=/var/lib/mysql
user=mysql
# Path to Galeralibrary
wsrep_provider=/usr/lib64/libgalera_smm.so
# Clusterconnection URL contains the IPs of node#1, node#2 and node#3
wsrep_cluster_address=gcomm://192.168.70.71,192.168.70.72,192.168.70.73
# In order forGalera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storageengine has only experimental support
default_storage_engine=InnoDB
# This changeshow InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node #1address
wsrep_node_address=192.168.70.71
# SST method
wsrep_sst_method=xtrabackup-v2
# Cluster name
wsrep_cluster_name=my_centos_cluster
# Authenticationfor SST method
wsrep_sst_auth="sstuser:s3cret"
在此以後,第一個節點能夠啓動如下命令:
[root@percona1 ~]#/etc/init.d/mysql bootstrap-pxc
若是在centos7上運行本教程,系統服務以下:
[root@percona1 ~]# systemctl start mysql@bootstrap.service
此命令將啓動集羣初步wsrep_cluster_address設置爲gcomm://。這樣的集羣將自舉以防後面的節點或MySQL從新開始,這不須要改變該配置文件。
第一個節點啓動後,集羣狀態檢測:
mysql> show status like 'wsrep%';
+----------------------------+--------------------------------------+
| Variable_name |Value |
+----------------------------+--------------------------------------+
| wsrep_local_state_uuid | c2883338-834d-11e2-0800-03c9c68e41ec |
...
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
...
| wsrep_cluster_size | 1 |
| wsrep_cluster_status | Primary |
| wsrep_connected |ON |
...
| wsrep_ready |ON |
+----------------------------+--------------------------------------+
40 rows in set (0.01 sec)
該輸出顯示集羣已成功自舉
修改root密碼,以及刪除空用戶
UPDATE mysql.user SET password=PASSWORD("Passw0rd")where user='root';
delete from mysql.user whereuser='';
FLUSH PRIVILEGES;
爲了使xtrabackup成功創建快照,須要給新用戶創建適當的權限。
mysql@percona1> CREATE USER'sstuser'@'localhost' IDENTIFIED BY 's3cret';
mysql@percona1> GRANT RELOAD,LOCK TABLES, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
mysql@percona1> FLUSH PRIVILEGES;
注:MySQL的root帳戶也可用於創建PerconaXtrabackup的SST,但建議使用不一樣用戶作這一點。
第二個節點的/etc/my.cnf配置內容以下:
[mysqld]
datadir=/var/lib/mysql
user=mysql
# Path to Galera library
wsrep_provider=/usr/lib64/libgalera_smm.so
# Cluster connection URLcontains IPs of node#1, node#2 and node#3
wsrep_cluster_address=gcomm://192.168.70.71,192.168.70.72,192.168.70.73
# In order for Galera to workcorrectly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine hasonly experimental support
default_storage_engine=InnoDB
# This changes how InnoDBautoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node #2 address
wsrep_node_address=192.168.70.72
# Cluster name
wsrep_cluster_name=my_centos_cluster
# SST method
wsrep_sst_method=xtrabackup-v2
#Authentication for SSTmethod
wsrep_sst_auth="sstuser:s3cret"
第二個節點的啓動命令
/etc/init.d/mysql start
服務器已經啓動後,它會自動接收狀態快照傳送。所以,第二個節點不會有空root密碼了。
這是第二個節點的狀態
mysql> show status like 'wsrep%';
+----------------------------+--------------------------------------+
| Variable_name | Value |
+----------------------------+--------------------------------------+
| wsrep_local_state_uuid | c2883338-834d-11e2-0800-03c9c68e41ec |
...
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
...
| wsrep_cluster_size | 2 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
...
| wsrep_ready | ON |
+----------------------------+--------------------------------------+
40 rows in set (0.01 sec)
第三個節點的/etc/my.cnf配置文件
[mysqld]
datadir=/var/lib/mysql
user=mysql
# Path to Galera library
wsrep_provider=/usr/lib64/libgalera_smm.so
# Cluster connection URLcontains IPs of node#1, node#2 and node#3
wsrep_cluster_address=gcomm://192.168.70.71,192.168.70.72,192.168.70.73
# In order for Galera to workcorrectly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine hasonly experimental support
default_storage_engine=InnoDB
# This changes how InnoDBautoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node #3 address
wsrep_node_address=192.168.70.73
# Cluster name
wsrep_cluster_name=my_centos_cluster
# SST method
wsrep_sst_method=xtrabackup-v2
#Authentication for SSTmethod
wsrep_sst_auth="sstuser:s3cret"
而後啓動節點
/etc/init.d/mysql start
服務器已經啓動後,它會自動接收狀態快照傳送。
集羣狀態查看:
mysql> show status like 'wsrep%';
+----------------------------+--------------------------------------+
| Variable_name | Value |
+----------------------------+--------------------------------------+
| wsrep_local_state_uuid | c2883338-834d-11e2-0800-03c9c68e41ec |
...
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
...
| wsrep_cluster_size | 3 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
...
| wsrep_ready | ON |
+----------------------------+--------------------------------------+
40 rows in set (0.01 sec)
該輸出證明了第三個節點加入集羣。
在節點2上建立數據庫
mysql@percona2> CREATE DATABASE percona;
Query OK, 1row affected(0.01 sec)
在節點3上建立表
mysql@percona3> USE percona;
Database changed
mysql@percona3> CREATE TABLE example (node_id INT PRIMARY KEY, node_name VARCHAR(30));
Query OK, 0rows affected(0.05 sec)
在節點1上插入數據
mysql@percona1> INSERT INTO percona.example VALUES (1, 'percona1');
QueryOK, 1 rowaffected (0.02 sec)
在第二個節點上查看數據
mysql@percona2> SELECT * FROM percona.example;
+---------+-----------+
| node_id | node_name |
+---------+-----------+
| 1 | percona1 |
+---------+-----------+
1 row in set (0.00 sec)
集羣搭建完成