上一篇博客中对比了各种mysql集群方案,本博客将讲解其中在同步复制功能方面比较出色的percona xtradb cluster的安装步骤
首先,如果服务器中已有默认的mysql(很多linux安装时自带了mysql)需要先删除,否则会报冲突
rpm -qa | grep -i mysql-server | xargs sudo rpm -e --nodeps
安装依赖的包
wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
wget http://mirror.centos.org/centos/6/os/x86_64/Packages/nc-1.84-22.el6.x86_64.rpm
rpm -ivh nc-1.84-22.el6.x86_64.rpm
wget http://apt.sw.be/redhat/el6/en/x86_64/rpmforge/RPMS/socat-1.7.2.4-1.el6.rf.x86_64.rpm
rpm -ivh socat-1.7.2.4-1.el6.rf.x86_64.rpm
安装Percona-XtraDB-Cluster-56
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
yum install Percona-XtraDB-Cluster-56
安装后启动
/etc/init.d/mysql start
然后创建用于同步复制的用户,这里使用的用户名和密码都是sstuser
CREATE USER ‘sstuser‘@‘localhost‘ IDENTIFIED BY ‘sstuser‘;
GRANT RELOAD, LOCK TABLES, REPLICATION CLIENT ON *.* TO ‘sstuser‘@‘localhost‘;
FLUSH PRIVILEGES;
如果create user 时报lost connection,需要升级,启动mysql后执行:
mysql_upgrade -uroot -p --skip-version-check
创建完用户后,停止Mysql,修改/etc/my.cnf,我的 my.cnf 为
[client] socket=/var/lib/mysql/mysql.sock [mysqld] server_id=1 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql binlog_format=ROW #bind-address=0.0.0.0 default-storage-engine=innodb innodb_autoinc_lock_mode=2 wsrep_provider=/usr/lib64/libgalera_smm.so wsrep_provider_options="gcache.size=300M; gcache.page_size=1G" wsrep_cluster_name="mycluster" wsrep_cluster_address="gcomm://200.200.168.24,200.200.168.25" wsrep_sst_method=rsync wsrep_node_address=200.200.168.24 # 本机IP地址 #wsrep_sst_method=xtrabackup-v2 wsrep_sst_auth="sstuser:sstuser" wsrep_node_name=pxc1 wsrep_slave_threads=4 [mysql_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
有两个地方需注意:
1、wsrep_node_address=200.200.168.24 安装每个mysql服务时,需要修改为自身的ip
2、wsrep_sst_method:在刚安装时,需要将slave节点设置为rsync,才能同步成功
每台集群中的mysql服务都按以上的步骤安装即可
安装完成后,即可启动Mysql。
启动mysql的方式,如果是主服务器,使用/etc/init.d/mysql bootstrap-pxc,如果是备份服务器,使用/etc/init.d/mysql start。
先启动主服务器,启动备份服务器时,即可看到备份服务器会自动同步数据。
另外,若启动失败,首先,看mysql日志,上面的my.cnf中指定了var/log/mysqld.log(若没有指定,默认是在目录/var/lib/mysql/中),看看有没有permission deny的问题,如果有,执行
yum -y install policycoreutils-python
semanage permissive -a mysqld_t
参考文档:
http://www.percona.com/doc/percona-xtradb-cluster/5.6/
http://hj192837.blog.51cto.com/655995/1546149
http://blog.chinaunix.net/uid-20776139-id-4041319.html
http://blog.csdn.net/china_world/article/details/25424903
http://blog.csdn.net/renfengjun/article/details/41379901