MySQL Galera介绍
主要功能:
- 同步复制
- 真正的multi-master,即所有节点可以同时读写数据库
- 自动的节点成员控制,失效节点自动被清除
- 新节点加入数据自动复制
- 真正的并行复制,行级
- 用户可以直接连接集群,使用感受上与mysql完全一致
优势:
- 因为是多主,所以不存在Slave lag(延迟)
- 不存在丢失交易的情况
- 同时具有读和写的扩展能力
- 更小的客户端延迟
- 节点间数据是同步的,而Master/Slave模式是异步的,不同slave上的binlog可能是不同的
环境
IP 地址 主机名 描述 10.0.0.10 node1 第一个启动 10.0.0.11 node2 10.0.0.12 node3
版本信息
[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) [[email protected] ~]# uname -a Linux node2 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
关闭防火墙和SElinux
[[email protected] ~]# systemctl stop firewalld.service [[email protected] ~]# setenforce 0
清除原有Mysql
[[email protected] ~]# yum erase mysql mysql-devel mysql-libs -y [[email protected] ~]# rpm -qa |grep mysql
组件下载地址
http://releases.galeracluster.com/mysql-wsrep-5.6/centos/7/x86_64/ http://releases.galeracluster.com/galera-3/centos/7/x86_64/
安装组件(yum 安装,依次安装了,如果失败,等其他安装包安装好以后,重新安装一遍)
[[email protected] ~]# yum install -y http://releases.galeracluster.com/mysql-wsrep-5.6/centos/7/x86_64/mysql-wsrep-client-5.6-5.6.38-25.21.el7.x86_64.rpm [[email protected] ~]# yum install -y http://releases.galeracluster.com/mysql-wsrep-5.6/centos/7/x86_64/mysql-wsrep-devel-5.6-5.6.38-25.21.el7.x86_64.rpm [[email protected] ~]# yum install -y http://releases.galeracluster.com/mysql-wsrep-5.6/centos/7/x86_64/mysql-wsrep-shared-5.6-5.6.38-25.21.el7.x86_64.rpm [[email protected] ~]# yum install -y http://releases.galeracluster.com/mysql-wsrep-5.6/centos/7/x86_64/mysql-wsrep-test-5.6-5.6.38-25.21.el7.x86_64.rpm [[email protected] ~]# yum install -y http://releases.galeracluster.com/galera-3/centos/7/x86_64/galera-3-25.3.22-2.el7.x86_64.rpm [[email protected] ~]# yum install -y http://releases.galeracluster.com/mysql-wsrep-5.6/centos/7/x86_64/mysql-wsrep-server-5.6-5.6.38-25.21.el7.x86_64.rpm
mysql 首次启动
[[email protected] ~]# service mysql start --skip-grant-tables
输入 mysql 登录,添加用户名、密码和对应数据库的用户名、密码权限
mysql> use mysql; mysql> update user set password=PASSWORD("123456") where USER="root" mysql> quit
重启数据库
[[email protected] ~]# service mysql restart
重启mysql服务,使用创建的用户名和密码登录.再次进入的时候回提示你设置密码
mysql> SET PASSWORD=PASSWORD("123456")
给对应需要访问MySQL的用户名和密码进行授权,如果没有授权,是无法远程连接MySQL的(两条密令二选一)
授权指定端口用户连接mysql数据库的命令 mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘10.0.0.10‘ IDENTIFIED BY ‘123456‘ WITH GRANT OPTION; 授权无端口限制的用户登录mysql数据库 mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘123456‘ WITH GRANT OPTION;
创建同步账号
mysql> grant all on *.* to ‘wsrep‘@‘%‘ identified by ‘nx111111‘; mysql> quit
设置Mysql为开机启动
[[email protected] ~]# chkconfig mysql on
去掉Postfix,这个可能跟MySQL配置有冲突
[[email protected] ~]# yum remove postfix -y
关闭Mysql
[[email protected] ~]# service mysql stop
配置文件
[[email protected] ~]# vim /etc/my.cnf !includedir /etc/my.cnf.d/
拷贝swrep.conf 文件到/etc/my.cnf.d/下
[[email protected] ~]# cp -r /usr/share/doc/mysql-wsrep-server-5.6-5.6.38/wsrep.cnf /etc/my.cnf.d/
将以下配置替原有配置
vim /etc/my.cnf.d/wsrep.conf [mysqld] server_id = 10 # 每个节点都不一样,分别是 10、11、12 default_storage_engine = InnoDB datadir =/var/lib/mysql/ binlog_format = ROW log-bin = mysql-bin log-slave-updates = 1 innodb_autoinc_lock_mode = 2 lower_case_table_names = 1 # 修改这个配置必须重启 log_bin_trust_function_creators = 1 wsrep_provider = /usr/lib64/galera-3/libgalera_smm.so wsrep_sst_auth = wsrep:nx111111 wsrep_sst_method = xtrabackup wsrep_cluster_name = ‘nx_wsrep‘ # 集群名,一定要一致。 wsrep_node_name = node1 #本节点主机名 wsrep_cluster_address = gcomm://10.0.0.11,10.0.0.12 # 其它节点 IP wsrep_node_address = 10.0.0.10 # 本节点 IP wsrep_sst_donor=node2,node3, # 其它节点的名称 wsrep_sst_method = rsync wsrep_slave_threads=8 innodb_buffer_pool_size=10737418240 innodb_log_file_size=134217728 innodb_flush_log_at_trx_commit=2 #innodb_buffer_pool_dump_at_shutdown=1 #innodb_buffer_pool_load_at_startup=1 #innodb_log_file_buffer_size=16777216 skip-name-resolve [client] user = wsrep password = nx111111
启动 mysql-wsrep
集群中的第一个节点,以空地址启动
[[email protected] ~]# mysqld_safe --wsrep_cluster_address=gcomm:// >/dev/null &
其它节点,启动方式
[[email protected] ~]# mysqld_safe > /dev/null &
查看集群状态
确认MySQL的3306端口和wsrep的4567端口处于监听状态
[[email protected] ~]# netstat -tulpn | grep -e 4567 -e 3306 tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN 5036/mysqld tcp6 0 0 :::3306 :::* LISTEN 5036/mysqld
集群功能是否开启
mysql> show status like ‘wsrep_ready‘; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | wsrep_ready | ON | +---------------+-------+ 1 row in set (0.00 sec)
集群ID号
mysql> SHOW GLOBAL STATUS LIKE ‘wsrep_cluster_state_uuid‘ ; +--------------------------+--------------------------------------+ | Variable_name | Value | +--------------------------+--------------------------------------+ | wsrep_cluster_state_uuid | 176a8d84-e0ba-11e7-9e7b-d7b3a39130ff | +--------------------------+--------------------------------------+ 1 row in set (0.00 sec)
集群中的节点数量
mysql> SHOW GLOBAL STATUS LIKE ‘wsrep_cluster_size‘ ; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+ 1 row in set (0.00 sec)
集群(其他)节点地址
mysql> show variables like ‘wsrep_cluster_address‘; +-----------------------+----------+ | Variable_name | Value | +-----------------------+----------+ | wsrep_cluster_address | gcomm:// | +-----------------------+----------+ 1 row in set (0.00 sec)
参考
http://blog.csdn.net/yangchuan_csdn91/article/details/52689981?locationNum=2