Mysql高可用方案haproxy+keepalive+MariaDB Galera Cluster实现:MariaDB Galera Cluster部署 (二)

第一步:环境介绍

1、服务器列表

节点名称IP端口说明
node1192.168.6.111

3306:mysql数据库端口

4567:wsrep使用端口

数据节点1
node2192.168.6.112

3306:mysql数据库端口

4567:wsrep使用端口

数据节点2
node3192.168.6.113

3306:mysql数据库端口

4567:wsrep使用端口

数据节点3

2、系统版本(所有节点都要)

linux系统版本: CentOS 7.3 64位

含wsrep Patch的MySQL:mysql-5.7.25_wsrep_25.10-linux-x86_64

安装前,我们可以检测系统是否自带安装 MySQL:

rpm -qa | grep mysql

如有,类似

mysql-libs-5.1.52-1.el6_0.1.x86_64

那可以选择进行卸载:

rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64  // 普通删除模式
rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64  // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除

检查否存在 mariadb 数据库,如有,卸载之,卸载同上

rpm -qa | grep mariadb

 如有,类似

mariadb-libs-5.5.56-2.el7.x86_64

则 卸载

rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

如果是源码编译安装的卸载方式:https://blog.csdn.net/lch520baby/article/details/91860848

3、关闭防火墙(所有节点都要)

#关闭防火墙
systemctl stop firewalld
#查看状态
systemctl status firewalld
#禁止开机启动防火墙
systemctl disable firewalld

永久生效,如果防火墙因其他原因不可关闭,开放3306和4567端口;生产环境下还是开放对应的端口

4、关闭SElinux(所有节点都要)

命令:getenforce

永久关闭(修改配置文件,即可永久关闭)

vi /etc/selinux/config

把SELINUX=enforcing改为SELINUX=disabled ,改完后记得重启服务器生效,我这里是永久关闭,大家可以根据自己的需求临时关闭

第二步:下载安装(所有节点都要)

1、设置mariadb的yum源并安装

# 创建文件
touch /etc/yum.repos.d/MariaDB.repo
# 修改内容
vi /etc/yum.repos.d/MariaDB.repo

添加如下内容

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

上面是国际镜像, 可以配置国内镜像,下载速度快一些,如下:

[mariadb]
name = MariaDB
baseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64/
gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1

 2、安装MariaDB

yum install -y MariaDB-server MariaDB-client galera

3、数据库配置

初始化数据库

# 安装成功之后启动MariaDB服务
systemctl start mariadb
# 配置数据库密码,测试情况使用 root  % 123456
/usr/bin/mysql_secure_installation

 执行上面的命令,会有一些问题需要设置,如下:

[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):<–初次运行直接回车
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n]    #是否设置root用户密码,输入y并回车或直接回车
New password:               #设置root用户的密码
Re-enter new password:      #再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]   #是否删除匿名用户,生产环境建议删除,所以直接回车
… Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] #是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] #是否删除test数据库,直接回车
- Dropping test database…
… Success!
- Removing privileges on test database…
… Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] #是否重新加载权限表,直接回车
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
[root@localhost ~]#

# 登录数据库
mysql -u root -p

 

修改远程登录(根据自己需求设置)

# 设置远程登录
grant all privileges on *.* to root@"%" identified by "123456";
# 使设置生效
flush privileges;

4、配置完成之后关闭数据库

systemctl stop mariadb

第三步:集群配置

1、配置server.cnf

我们查看/etc/my.cnf 文件变成了如下配置

然后我们修改/etc/my.cnf.d/server.cnf 配置文件

2、192.168.6.111配置如下:

[server]

[mysqld]

[galera]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.6.111,192.168.6.112,192.168.6.113"
wsrep_node_name = node1
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0

wsrep_cluster_name="MariaDB_Cluster"
wsrep_node_address=192.168.6.111
wsrep_sst_method=rsync

wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=2
innodb_buffer_pool_size=1024M
wsrep_sst_auth=root:123456


[embedded]

[mariadb]

[mariadb-10.1]

配置说明:

wsrep_provider:galera的库文件的地址

wsrep_on:表示开启mariadb-gelera集群模式,否则为单机mariadb模式

wsrep_cluster_address:集群中其它节点的IP地址

binlog_format=row:记录binlog的格式

default_storage_engine=InnoDB :使用的默认引擎

wsrep_slave_threads:并行复制线程数

innodb_flush_log_at_trx_commit=2:

# 0:log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行。该模式下在事务提交的时候,不会主动触发写入磁盘的操作。

# 1:每次事务提交时MySQL都会把log buffer的数据写入log file,并且flush(刷到磁盘)中去,该模式为系统默认。

# 2:每次事务提交时MySQL都会把log buffer的数据写入log file,但是flush(刷到磁盘)操作并不会同时进行。该模式下,MySQL会每秒执行一次 flush(刷到磁盘)操作

innodb_buffer_pool_size=1024M       #设置缓存池大小

wsrep_cluster_name:设置集群的名字

wsrep_node_address:设置当前节点的IP

wsrep_node_name='test':设置当前节点的名字,可以任意起名字

wsrep_sst_method:设置集群中所有节点数据同步方式

wsrep_sst_auth:设置集群中所有集群互相认证的密码

3、配置其它节点

192.168.6.112、192.168.6.113按上面的方式配置/etc/my.cnf.d/server.cnf 文件,注意要把 wsrep_node_name 和 wsrep_node_address 改成相应节点的 hostname 和 ip。

4、启动集群
在192.168.6.111服务器上输入galera_new_cluster命令启动集群

# 启动新的集群
galera_new_cluster

查看集群状态

#登录mysql服务
mysql -u root -p
#查看节点数
show  status like "wsrep_cluster_size";
#查看加入的节点ip
show  global status like 'wsrep_incoming_addresses';

5、打开其它节点

打开 192.168.6.112、192.168.6.113 上面的mariadb数据库:

systemctl start mariadb

查看集群状态 

#登录mysql服务
mysql -u root -p
#查看节点数
show  status like "wsrep_cluster_size";
#查看加入的节点ip
show  global status like 'wsrep_incoming_addresses';

 第四步:测试集群

可以用navicat分别连接集群中的数据库

在192.168.6.111上创建test1数据库,然后刷新192.168.6.112和192.168.6.113就可以看到test1数据库了

到这里就已经实现了,mysql的多住同步复制。停掉一个数据库之后一样可以正常使用

MariaDB galera cluster 全部停止后如何再启动

1.正式环境下基本上不会出现此类情况

2.测试环境的时候可能会出现,如自己电脑上搞的几个虚拟机上测试,后来全部关机了,再想启动集群,报错了

【解决方式】

1.正常第一次启动集群,使用命令:galera_new_cluster ,其他版本请另行参考

2.整个集群关闭后,再重新启动,则打开任一主机,输入命令:

   vim /var/lib/mysql/grastate.dat

 

#GALERA savedd state
version:2.1
uuid: 自己的cluster id
seqno: -1
safe_to_bootstrap:0

修改safe_to_bootstrap=1

3.重新启动集群命令:galera_new_cluster

4.其他节点:systemctl start mariadb

 

MariaDB Galera Cluster常见问题

问题1:执行galera_new_cluster命令的时候报错:

[ERROR] WSREP: It may not be safe to bootstrap the cluster from this node. It was not the last one to leave the cluster and may not contain all the updates. To force cluster bootstrap with this node, edit the grastate.dat file manually and set safe_to_bootstrap to 1 .

解决方式:这个问题产生的原因就是,你启动集群的这个节点不是最后一个离开集群的节点。解决这个问题可以用如下方式:

把/var/lib/mysql/grastate.dat文件设置safe_to_bootstrap=1,当然了,我们不需需要强制从该节点启动,我们逐一排查每个节点下的grastate.dat文件(该文件在data目录下),找到safe_to_bootstrap=1的节点,然后在该节点上启动即可

问题2: 备点加入集群一直失败
解决方式:在生产环境中必须打开防火墙,但是我只开放了 4567 和 针对指定ip开放3306 端口,导致加入集群失败,需要开放 3306, 4444, 4567, 4568 四个端口才可以正常启动

 

如果还有其它不足的地方欢迎大家留言补充!!

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值