PXC簡介
Percona XtraDB Cluster(簡稱PXC集羣)提供了MySQL高可用的一種實現方法。
1.集羣是有節點組成的,推薦配置至少3個節點,但是也可以運行在2個節點上。
2.每個節點都是普通的mysql/percona服務器,可以將現有的數據庫服務器組成集羣,反之,也可以將集羣拆分成單獨的服務器。
3.每個節點都包含完整的數據副本。
PXC集羣主要由兩部分組成:Percona Server with XtraDB和Write Set Replication patches(使用了Galera library,一個通用的用於事務型應用的同步、多主複製插件)。
PXC特性:
1,同步複製,事務要麼在所有節點提交或不提交。
2,多主複製,可以在任意節點進行寫操作。
3,在從服務器上並行應用事件,真正意義上的並行複製。
4,節點自動配置,數據一致性,不再是異步複製。
PXC劣勢:
1、 當前版本(5.6.20)的複製只支持InnoDB引擎,其他存儲引擎的更改不復制。然而,DDL(Data Definition Language) 語句在statement級別被複制,並且,對mysql.*表的更改會基於此被複制。例如CREATE USER...語句會被複制,但是 INSERT INTO mysql.user...語句則不會。(也可以通過wsrep_replicate_myisam參數開啓myisam引擎的複製,但這是一個實驗性的參數)。
2、PXC集羣一致性控制機制,事有可能被終止,原因如下:集羣允許在兩個節點上同時執行操作同一行的兩個事務,但是隻有一個能執行成功,另一個會被終止,集羣會給被終止的客戶端返回死鎖錯誤(Error: 1213 SQLSTATE: 40001 (ER_LOCK_DEADLOCK)).
3、寫入效率取決於節點中最弱的一臺,因爲PXC集羣採用的是強一致性原則,一個更改操作在所有節點都成功纔算執行成功。
環境介紹:
操作系統版本:Centos-6.5-X64
數據庫版本:Percona-Server-5.6.15-rel63.0.tar
192.168.3.81 node1
192.168.3.82 node2
192.168.3.83 node3
1.安裝 Percona mysql(所有節點安裝)
安裝源
rpm -ivh http://dl
.fedoraproject.org
/pub/epel/6/x86_64/epel-release-6-8
.noarch.rpm
安裝Precona XtraDB Cluster所需要的擴展包
yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bisonlibtool ncurses5-devel boost
安裝perl組件(xtrabackup需要的組件)
yum -y install perl-DBD-MySQL perl-DBI perl-Time-HiRes
安裝socat (Socat是一個在兩個獨立數據的雙向傳輸之間起到中繼作用的軟件)
配置好epel源之後,可直接執行 yum -y install socat
若無法yum安裝socat,則按照以下步驟編譯安裝
wget http://www.dest-unreach.org/socat/download/socat-1.7.2.4.tar.gz
tar zxvf socat-1.7.2.4.tar.gz
./configure
make && make install
安裝Percona-XtraDB-Cluster及其相關組件
wget http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
rpm -ivh percona-release-0.0-1.x86_64.rpm
yum -y
install
Percona-Server-shared-56 Percona-Server-client-56 Percona-Server-server-56
二.初始化Percona-XtraDB-Cluster集羣
Percona mysql配置文件修改
配置 /etc/my.cnf,(所有節點安裝):
# vim /etc/my.cnf [mysqld] innodb_buffer_pool_size = 1024M //innodb_buffer_pool_size內存的80% datadir = /data/mysql port = 3306 server_id = 81 //server_id必須要不一樣 socket = /tmp/mysql.sock log-bin=mysql-bin expire_logs_days = 10 sort_buffer_size = 1M join_buffer_size = 1M query_cache_size = 512M query_cache_limit = 2M query_cache_min_res_unit = 2k thread_stack = 192K tmp_table_size = 246M max_heap_table_size = 246M key_buffer_size = 300M read_buffer_size = 1M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M slow_query_log = 1 log-error = /data/mysql/log/error.log long_query_time = 2 log-queries-not-using-indexes = ON slow_query_log_file = /data/mysql/log/slowquery.log sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES general_log = 1 //數據庫執行日誌開關,一般不打開 general_log_file = /data/mysql/log/mysql-general.log //數據庫執行日誌文件,一般不打開
創建所需目錄,(所有節點安裝):
mkdir -p /data/mysql/log
chown mysql.mysql /data/mysql/log
初始化數據庫,(所有節點安裝):
mysql_install_db --user=mysql --datadir=
/data/mysql
/etc/init
.d
/mysql
start
mysqladmin -u root password
'123456'
-S
/tmp/mysql
.sock
//
首次修改mysql密碼
因爲XtraDB Cluster的軟件包與原有軟件包衝突:
/etc/init
.d
/mysql
stop
rpm -qa |
grep
Percona-Server |
grep
-
v
compat |
xargs
rpm -e --nodeps
安裝Percona XtraDB Cluster 5.6 (所有節點安裝)
yum -y install Percona-XtraDB-Cluster-server-56 Percona-XtraDB-Cluster-client-56 Percona-XtraDB-Cluster-galera-3
添加 XtraDB Cluster 的支持,(所有節點安裝)
vim /etc/my.cnf 在[mysqld]模塊下面增加下面內容: # xtradb cluster settings binlog_format = ROW wsrep_cluster_name = PXC wsrep_cluster_address = gcomm://192.168.3.81,192.168.3.82,192.168.3.83 wsrep_node_address = 192.168.3.81 //注意,每臺服務器需要將 wsrep_node_address 設置爲本機的IP地址或主機名。 wsrep_provider = /usr/lib64/libgalera_smm.so wsrep_sst_method = rsync innodb_locks_unsafe_for_binlog = 1 innodb_autoinc_lock_mode = 2 default_storage_engine=InnoDB
初始化第一個Node:
/etc/init.d/mysql bootstrap-pxc //
第一個節點才使用
mysql> show global status like 'wsrep%'; +------------------------------+--------------------------------------+ | Variable_name | Value | +------------------------------+--------------------------------------+ | wsrep_local_state_uuid | fdfec3ab-d9e8-11e4-94aa-ae45256a81fb | | wsrep_protocol_version | 7 | | wsrep_last_committed | 2 | | wsrep_replicated | 0 | | wsrep_replicated_bytes | 0 | | wsrep_repl_keys | 0 | | wsrep_repl_keys_bytes | 0 | | wsrep_repl_data_bytes | 0 | | wsrep_repl_other_bytes | 0 | | wsrep_received | 2 | | wsrep_received_bytes | 141 | | wsrep_local_commits | 0 | | wsrep_local_cert_failures | 0 | | wsrep_local_replays | 0 | | wsrep_local_send_queue | 0 | | wsrep_local_send_queue_max | 1 | | wsrep_local_send_queue_min | 0 | | wsrep_local_send_queue_avg | 0.000000 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_max | 1 | | wsrep_local_recv_queue_min | 0 | | wsrep_local_recv_queue_avg | 0.000000 | | wsrep_local_cached_downto | 0 | | wsrep_flow_control_paused_ns | 0 | | wsrep_flow_control_paused | 0.000000 | | wsrep_flow_control_sent | 0 | | wsrep_flow_control_recv | 0 | | wsrep_cert_deps_distance | 0.000000 | | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 0.000000 | | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 0.000000 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_cert_index_size | 0 | | wsrep_causal_reads | 0 | | wsrep_cert_interval | 0.000000 | | wsrep_incoming_addresses | 192.168.3.81:3306 | | wsrep_evs_delayed | | | wsrep_evs_evict_list | | | wsrep_evs_repl_latency | 0/0/0/0/0 | | wsrep_evs_state | OPERATIONAL | | wsrep_gcomm_uuid | 597ae492-dcc6-11e4-ba30-e6dfd92be1c6 | | wsrep_cluster_conf_id | 1 | | wsrep_cluster_size | 1 | | wsrep_cluster_state_uuid | fdfec3ab-d9e8-11e4-94aa-ae45256a81fb | | wsrep_cluster_status | Primary | | wsrep_connected | ON | | wsrep_local_bf_aborts | 0 | | wsrep_local_index | 0 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <[email protected]> | | wsrep_provider_version | 3.9(r93aca2d) | | wsrep_ready | ON | +------------------------------+--------------------------------------+
參數 "wsrep_cluster_size"
爲 1,因爲目前Cluster中只有一個Node。
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | log | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> create database liu; Query OK, 1 row affected (0.00 sec) mysql> use liu Database changed mysql> create table liu01 (id int,name char(4)); Query OK, 0 rows affected (0.05 sec) mysql> show tables; +---------------+ | Tables_in_liu | +---------------+ | liu01 | +---------------+ 1 row in set (0.01 sec)
添加新的 Node 到Cluster中:[root@M1905 mysql]# mysql -u root -p123456 -S /tmp/mysql.sock mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | liu | | log | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec) mysql> use liu Database changed mysql> show tables; +---------------+ | Tables_in_liu | +---------------+ | liu01 | +---------------+ 1 row in set (0.00 sec)
查看Cluster的狀態:
mysql> show global status like 'wsrep%'; +------------------------------+--------------------------------------+ | Variable_name | Value | +------------------------------+--------------------------------------+ | wsrep_local_state_uuid | fdfec3ab-d9e8-11e4-94aa-ae45256a81fb | | wsrep_protocol_version | 7 | | wsrep_last_committed | 7 | | wsrep_replicated | 0 | | wsrep_replicated_bytes | 0 | | wsrep_repl_keys | 0 | | wsrep_repl_keys_bytes | 0 | | wsrep_repl_data_bytes | 0 | | wsrep_repl_other_bytes | 0 | | wsrep_received | 4 | | wsrep_received_bytes | 510 | | wsrep_local_commits | 0 | | wsrep_local_cert_failures | 0 | | wsrep_local_replays | 0 | | wsrep_local_send_queue | 0 | | wsrep_local_send_queue_max | 1 | | wsrep_local_send_queue_min | 0 | | wsrep_local_send_queue_avg | 0.000000 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_max | 1 | | wsrep_local_recv_queue_min | 0 | | wsrep_local_recv_queue_avg | 0.000000 | | wsrep_local_cached_downto | 7 | | wsrep_flow_control_paused_ns | 0 | | wsrep_flow_control_paused | 0.000000 | | wsrep_flow_control_sent | 0 | | wsrep_flow_control_recv | 0 | | wsrep_cert_deps_distance | 1.000000 | | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 1.000000 | | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 1.000000 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_cert_index_size | 3 | | wsrep_causal_reads | 0 | | wsrep_cert_interval | 0.000000 | | wsrep_incoming_addresses | 192.168.3.82:3306,192.168.3.81:3306 | | wsrep_evs_delayed | | | wsrep_evs_evict_list | | | wsrep_evs_repl_latency | 0/0/0/0/0 | | wsrep_evs_state | OPERATIONAL | | wsrep_gcomm_uuid | 16e598fb-dccb-11e4-9ca9-cb37e03eb618 | | wsrep_cluster_conf_id | 2 | | wsrep_cluster_size | 2 | | wsrep_cluster_state_uuid | fdfec3ab-d9e8-11e4-94aa-ae45256a81fb | | wsrep_cluster_status | Primary | | wsrep_connected | ON | | wsrep_local_bf_aborts | 0 | | wsrep_local_index | 0 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <[email protected]> | | wsrep_provider_version | 3.9(r93aca2d) | | wsrep_ready | ON | +------------------------------+--------------------------------------+
參數
"wsrep_cluster_size"
變爲了 2。因爲目前Cluster中已經有2個Node了。
下面就可按照同樣步驟,添加更多節點到Cluster中了,在這個測試環境中,我們一共有3個Node。
添加第三個節點到集羣中
service mysql start
[root@M1905 mysql]# mysql -u root -p123456 -S /tmp/mysql.sock mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | liu | | log | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec) mysql> use liu Database changed mysql> show tables; +---------------+ | Tables_in_liu | +---------------+ | liu01 | +---------------+ 1 row in set (0.00 sec)
測試Cluster功能
7.1模擬192.168.3.81節點1宕機
[root@M1905 ~]# service mysql stop
Shutting down MySQL (Percona XtraDB Cluster).... SUCCESS!
[root@M1905 ~]# netstat -tunlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 809/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 898/master
//在任意其它節的查看cluster狀態
mysql> show global status like 'wsrep%'; //在任意其它節的查看cluster狀態 +------------------------------+---------------------------------------------------+ | Variable_name | Value | +------------------------------+---------------------------------------------------+ | wsrep_local_state_uuid | fdfec3ab-d9e8-11e4-94aa-ae45256a81fb | | wsrep_protocol_version | 7 | | wsrep_last_committed | 7 | | wsrep_replicated | 0 | | wsrep_replicated_bytes | 0 | | wsrep_repl_keys | 0 | | wsrep_repl_keys_bytes | 0 | | wsrep_repl_data_bytes | 0 | | wsrep_repl_other_bytes | 0 | | wsrep_received | 4 | | wsrep_received_bytes | 489 | | wsrep_local_commits | 0 | | wsrep_local_cert_failures | 0 | | wsrep_local_replays | 0 | | wsrep_local_send_queue | 0 | | wsrep_local_send_queue_max | 1 | | wsrep_local_send_queue_min | 0 | | wsrep_local_send_queue_avg | 0.000000 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_max | 1 | | wsrep_local_recv_queue_min | 0 | | wsrep_local_recv_queue_avg | 0.000000 | | wsrep_local_cached_downto | 0 | | wsrep_flow_control_paused_ns | 0 | | wsrep_flow_control_paused | 0.000000 | | wsrep_flow_control_sent | 0 | | wsrep_flow_control_recv | 0 | | wsrep_cert_deps_distance | 0.000000 | | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 0.000000 | | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 0.000000 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_cert_index_size | 0 | | wsrep_causal_reads | 0 | | wsrep_cert_interval | 0.000000 | | wsrep_incoming_addresses | 192.168.3.82:3306,192.168.3.83:3306 | | wsrep_evs_delayed | | | wsrep_evs_evict_list | | | wsrep_evs_repl_latency | 0.000483336/0.000670287/0.000857238/0.000186951/2 | | wsrep_evs_state | OPERATIONAL | | wsrep_gcomm_uuid | 6c70c34d-dccd-11e4-a9fd-a30f91414348 | | wsrep_cluster_conf_id | 4 | | wsrep_cluster_size | 2 | | wsrep_cluster_state_uuid | fdfec3ab-d9e8-11e4-94aa-ae45256a81fb | | wsrep_cluster_status | Primary | | wsrep_connected | ON | | wsrep_local_bf_aborts | 0 | | wsrep_local_index | 1 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <[email protected]> | | wsrep_provider_version | 3.9(r93aca2d) | | wsrep_ready | ON | +------------------------------+---------------------------------------------------+
#發現wsrep_incoming_addresses的192.168.3.81:3306不存在,並且wsrep_cluster_size 可用節點爲2.
7.2在192.168.3.83節點3上面插入新數據
mysql> select * from liu01; +------+------+ | id | name | +------+------+ | 1 | aa | +------+------+ 1 row in set (0.00 sec) mysql> insert into liu01 values(2,'bb'); mysql> select * from liu01; +------+------+ | id | name | +------+------+ | 1 | aa | | 2 | bb | +------+------+ 2 rows in set (0.00 sec
7.3恢復192.168.3.81節點1mysql服務,及查詢節點1數據是否正常。
[root@M1905 ~]# service mysql start Starting MySQL (Percona XtraDB Cluster).... SUCCESS! [root@M1905 ~]# mysql -u root -p123456 -S /tmp/mysql.sock mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | liu | | log | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.00 sec) mysql> use liu Database changed mysql> select * from liu01; +------+------+ | id | name | +------+------+ | 1 | aa | | 2 | bb | +------+------+ 2 rows in set (0.00 sec)
mysql> show global status like 'wsrep%'; +------------------------------+-------------------------------------------------------+ | Variable_name | Value | +------------------------------+-------------------------------------------------------+ | wsrep_local_state_uuid | fdfec3ab-d9e8-11e4-94aa-ae45256a81fb | | wsrep_protocol_version | 7 | | wsrep_last_committed | 8 | | wsrep_replicated | 0 | | wsrep_replicated_bytes | 0 | | wsrep_repl_keys | 0 | | wsrep_repl_keys_bytes | 0 | | wsrep_repl_data_bytes | 0 | | wsrep_repl_other_bytes | 0 | | wsrep_received | 3 | | wsrep_received_bytes | 287 | | wsrep_local_commits | 0 | | wsrep_local_cert_failures | 0 | | wsrep_local_replays | 0 | | wsrep_local_send_queue | 0 | | wsrep_local_send_queue_max | 1 | | wsrep_local_send_queue_min | 0 | | wsrep_local_send_queue_avg | 0.000000 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_max | 1 | | wsrep_local_recv_queue_min | 0 | | wsrep_local_recv_queue_avg | 0.000000 | | wsrep_local_cached_downto | 0 | | wsrep_flow_control_paused_ns | 0 | | wsrep_flow_control_paused | 0.000000 | | wsrep_flow_control_sent | 0 | | wsrep_flow_control_recv | 0 | | wsrep_cert_deps_distance | 0.000000 | | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 1.000000 | | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 1.000000 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_cert_index_size | 0 | | wsrep_causal_reads | 0 | | wsrep_cert_interval | 0.000000 | | wsrep_incoming_addresses | 192.168.3.82:3306,192.168.3.81:3306,192.168.3.83:3306 | | wsrep_evs_delayed | | | wsrep_evs_evict_list | | | wsrep_evs_repl_latency | 0/0/0/0/0 | | wsrep_evs_state | OPERATIONAL | | wsrep_gcomm_uuid | 4be90b42-dccf-11e4-83f4-5238aff4e818 | | wsrep_cluster_conf_id | 5 | | wsrep_cluster_size | 3 | | wsrep_cluster_state_uuid | fdfec3ab-d9e8-11e4-94aa-ae45256a81fb | | wsrep_cluster_status | Primary | | wsrep_connected | ON | | wsrep_local_bf_aborts | 0 | | wsrep_local_index | 1 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <[email protected]> | | wsrep_provider_version | 3.9(r93aca2d) | | wsrep_ready | ON | +------------------------------+-------------------------------------------------------+
發現wsrep_incoming_addresses的192.168.3.81:3306已經存在,並且wsrep_cluster_size可用節點爲3.
8.感覺"Percona XtraDB cluster“的高可用增加刪除節點很方便,自動切換故障,如果能配合atlas做讀寫分離就完美了,但是經過測試,cluster寫性能真的不咋的,如果對寫要求不高,還是不錯的選擇。
http://www.cnblogs.com/tae44/p/4691814.html