Percona XtraDB Cluster简介
Percona XtraDB Cluster是MySQL高可用性和可扩展性的解决方案,Percona XtraDB Cluster完全兼容MySQL和Percona Server。
Percona XtraDB Cluster提供的特性有:
- 同步复制,事务要么在所有节点提交或不提交。
- 多主复制,可以在任意节点进行写操作。
- 在从服务器上并行应用事件,真正意义上的并行复制。
- 节点自动配置。
- 数据一致性,不再是异步复制。
- 当执行一个查询时,在本地节点上执行。因为所有数据都在本地,无需远程访问。
- 无需集中管理。可以在任何时间点失去任何节点,但是集群将照常工作。
- 良好的读负载扩展,任意节点都可以查询。
- 加入新节点,开销大。需要复制完整的数据。
- 不能有效的解决写缩放问题,所有的写操作都将发生在所有节点上。
- 有多少个节点就有多少重复的数据。
Percona XtraDB Cluster局限性
- 目前的复制仅仅支持InnoDB存储引擎。任何写入其他引擎的表,包括mysql.*表将不会复制。但是DDL语句会被复制的,因此创建用户将会被复制,但是insert into mysql.user…将不会被复制的。
- DELETE操作不支持没有主键的表。没有主键的表在不同的节点顺序将不同,如果执行SELECT…LIMIT… 将出现不同的结果集。
- 在多主环境下LOCK/UNLOCK TABLES不支持。以及锁函数GET_LOCK(), RELEASE_LOCK()…
- 查询日志不能保存在表中。如果开启查询日志,只能保存到文件中。
- 允许最大的事务大小由wsrep_max_ws_rows和wsrep_max_ws_size定义。任何大型操作将被拒绝。如大型的LOAD DATA操作。
- 由于集群是乐观的并发控制,事务commit可能在该阶段中止。如果有两个事务向在集群中不同的节点向同一行写入并提交,失败的节点将中止。对于集群级别的中止,集群返回死锁错误代码(Error: 1213 SQLSTATE: 40001 (ER_LOCK_DEADLOCK)).
- XA事务不支持,由于在提交上可能回滚。
- 整个集群的写入吞吐量是由最弱的节点限制,如果有一个节点变得缓慢,那么整个集群将是缓慢的。为了稳定的高性能要求,所有的节点应使用统一的硬件。
- 集群节点建议最少3个。
- 如果DDL语句有问题将破坏集群。
安装Percona XtraDB 集群
- shell> tar zxvf Percona-XtraDB-Cluster-5.6.22-72.0.tar.gz
- shell> cd Percona-XtraDB-Cluster-5.6.22-72.0
- 安装依赖包
- shell> yum install cmake gcc gcc-c++ libaio libaio-devel automake autoconf bzr bison libtool ncurses5-devel
- shell> BUILD/compile-pentium64
- 默认安装路径/usr/local/mysql/
- shell> make && make install
- shell> rpm -ivh Percona-XtraDB-Cluster-galera-3-3.14-1.rhel6.x86_64.rpm
- 创建数据文件目录
- shell> mkdir -p /data/pxc
- shell> mv /usr/local/mysql/ /opt/pxc
- shell> chown -R mysql.mysql /opt/pxc
- shell> chown -R mysql.mysql /data/pxc
配置节点node1:
- shell> more /opt/pxc/my.cnf
- [mysqld]
- socket=/tmp/mysql-node1.sock
- port=3307
- datadir=/data/pxc
- user=mysql
- binlog_format=ROW
- default-storage-engine=innodb
- innodb_autoinc_lock_mode=2
- innodb_locks_unsafe_for_binlog=1
- query_cache_size=0
- query_cache_type=0
- bind-address=0.0.0.0
- wsrep_provider=/usr/lib64/libgalera_smm.so
- wsrep_cluster_address=gcomm://10.106.58.211,10.106.58.212,10.106.58.213
- wsrep_cluster_name=my_wsrep_cluster
- wsrep_slave_threads=1
- wsrep_certify_nonPK=1
- wsrep_max_ws_rows=131072
- wsrep_max_ws_size=1073741824
- wsrep_debug=1
- wsrep_convert_LOCK_to_trx=0
- wsrep_retry_autocommit=1
- wsrep_auto_increment_control=1
- wsrep_causal_reads=0
- #wsrep_notify_cmd=
- # SST method
- #wsrep_sst_method=xtrabackup-v2
- wsrep_sst_method=rsync
- # Authentication for SST method
- #wsrep_sst_auth="sstuser:s3cret"
- # Node #1 address
- wsrep_node_address=10.106.58.211
- server-id = 1
启动node1:
- shell> /opt/pxc/scripts/mysql_install_db --basedir=/opt/pxc/ --datadir=/data/pxc/ \
- --user=mysql --defaults-file=/opt/pxc/my.cnf
- shell> /opt/pxc/bin/mysqld_safe --defaults-file=/opt/pxc/my.cnf --wsrep-new-cluster &
- shell> /opt/pxc/bin/mysql -u root -S /tmp/mysql-node1.sock
- mysql> delete from mysql.user where user = '';
- mysql> GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO sstuser@'localhost' identified by 's3cret';
- mysql> flush privileges;
配置启动节点node2、node3:
node2、node3配置启动同理。
- [mysqld]
- socket=/tmp/mysql-node1.sock
- wsrep_node_address=10.106.58.212
- server-id = 2
- shell> /opt/pxc/bin/mysqld_safe --defaults-file=/opt/pxc/my.cnf &
检查pxc状态
- mysql> show status like 'wsrep%';
- +------------------------------+----------------------------------------------------------+
- | Variable_name | Value |
- +------------------------------+----------------------------------------------------------+
- | wsrep_local_state_uuid | 17b9d472-5ace-11e5-b22f-ab14cb9dcc7b |
- | 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 | 3 |
- | wsrep_received_bytes | 311 |
- | wsrep_local_commits | 0 |
- | wsrep_local_cert_failures | 0 |
- .
- | wsrep_incoming_addresses | 10.106.58.213:3307,10.106.58.212:3307,10.106.58.211:3307 |
- | wsrep_evs_delayed | |
- | wsrep_evs_evict_list | |
- | wsrep_evs_repl_latency | 0/0/0/0/0 |
- | wsrep_evs_state | OPERATIONAL |
- | wsrep_gcomm_uuid | c21183b0-5acf-11e5-930d-9bfcbb0bb24c |
- | wsrep_cluster_conf_id | 5 |
- | wsrep_cluster_size | 3 |
- | wsrep_cluster_state_uuid | 17b9d472-5ace-11e5-b22f-ab14cb9dcc7b |
- | wsrep_cluster_status | Primary |
- | wsrep_connected | ON |
- | wsrep_local_bf_aborts | 0 |
- | wsrep_local_index | 2 |
- | wsrep_provider_name | Galera |
- | wsrep_provider_vendor | Codership Oy <info@codership.com> |
- | wsrep_provider_version | 3.11(r93aca2d) |
- | wsrep_ready | ON |
- +------------------------------+----------------------------------------------------------+
- 58 rows in set (0.01 sec)
简单测试pxc
- mysql@node1> create database svoid;
- Query OK, 1 row affected (0.00 sec)
- mysql@node2> use svoid
- Database changed
- mysql@node2> create table test(id int);
- Query OK, 0 rows affected (0.01 sec)
- mysql@node3> use svoid
- Database changed
- mysql@node3> show tables;
- +-----------------+
- | Tables_in_svoid |
- +-----------------+
- | test |
- +-----------------+
- 1 row in set (0.00 sec)
- mysql@node3> insert into test select 1;
- Query OK, 1 row affected (0.01 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysqlnode2> update test set id = 2 where id = 1;
- Query OK, 1 row affected (0.01 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysqlnode1> alter table test add primary key(id);
- Query OK, 1 row affected (0.02 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysqlnode1> select * from test;
- +----+
- | id |
- +----+
- | 2 |
- +----+
- 1 row in set (0.00 sec)
- 模拟故障node1宕机
- shell> /opt/pxc/bin/mysqladmin -S /tmp/mysql-node1.sock -u root shutdown
- mysqlnode2> insert into test select 1;
- Query OK, 1 row affected (0.00 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysqlnode2> insert into test select 3;
- Query OK, 1 row affected (0.00 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- 启动node1
- shell> /opt/pxc/bin/mysqld_safe --defaults-file=/opt/pxc/my.cnf &
- mysqlnode1> select * from test;
- +----+
- | id |
- +----+
- | 1 |
- | 2 |
- | 3 |
- +----+
- 3 rows in set (0.00 sec)
- 可以看出其中一个节点故障不影响其他节点正常使用,启动节点后会自动同步数据
参考:
http://www.ttlsa.com/mysql/mysql-high-availability-with-nginx-and-galera/
http://www.ttlsa.com/mysql/mysqlgalera-multi-master/
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29733787/viewspace-2074784/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29733787/viewspace-2074784/