Percona XtraDB Cluster是MySQL高可用性和可扩展性的解决方案,Percona XtraDB Cluster完全兼容MySQL和Percona Server。
Percona XtraDB Cluster提供的特性有:
- 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
- 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
- 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配置启动同理。
- [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 &
- 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)
- 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博客 ” ,链接:https://blog.itpub.net/29733787/viewspace-2074784/ ,如需转载,请注明出处,否则将追究法律责任。