目录
前言
据我了解到的现在的大部分企业数据库架构,目前最常用的有这几种:一主多从、双主、还有Galera Cluster集群的方式。Galera Cluster是Codership公司开发的一套免费开源的高可用方案,有想了解的伙伴,这里是它的官网 ~~ 点我 。今天只说高可用双主。
环境
Mysql节点1:172.16.10.70 节点2:172.16.10.71
Keepalived主:172.16.10.70 备:172.16.10.71 VIP:172.16.10.100
MYSQL部分安装配置详解
下载安装mysql
1)安装(生产环境一般都是源码安装,这里我直接yum安装)
[root@bogon ~]# yum -y install mariadb mariadb-server
配置数据库的两个节点
1)开启数据库二进制日志和中继日志(这里顺便把第二台节点也配置方便下面操作)
[root@bogon ~]# vim /etc/my.cnf 第一个节点
server-id=1
log-bin=mysql-bin
relay-log=relay-logs
[root@bogon ~]# vim /etc/my.cnf 第二个节点
server-id=2
log-bin=mysql-bin
relay-log=relay-logs
2)启动数据库并且在第二个节点授权并获取二进制二进制文件信息
[root@bogon ~]# mysql #注意生产必须设置密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
MariaDB [(none)]> grant all on *.* to hxb@'172.16.10.70' identified by 'hxb';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; #刷新权限
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master status; #获取到这些数据
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 453 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
3)进入数据库配置与第二个节点建立连接
[root@bogon ~]# mysql #默认yum安装的没有密码,生产中必须设置密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
MariaDB [(none)]> change master to master_host='172.16.10.70',master_user='hxb',master_password='hxb',master_log_file='mysql-bin.000005',master_log_pos=245;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes #双yes则表示成功
4)另一台操作同上
Keepalived部分安装配置详解
安装
1)keepalived可以直接yum安装
[root@bogon ~]# yum -y install keepalived
配置文件的修改
1)主节点的配置如下
[root@bogon ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id Keep1
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.10.100
}
}
virtual_server 172.16.10.70 3306{
delay_loop 6
lb_algo rr
lb_kind NAT
! persistence_timeout 50
protocol TCP
real_server 172.16.10.70 3306 {
weight 1
notify_down /etc/keepalived/killkep1.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
2)killkep1的脚本内容(与备上一样)
[root@bogon ~]# vim /etc/keepalived/killkep1.sh
pkill keepalived
chmod +x killkep1.sh
3)备上的配置文件如下
[root@bogon ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id Keep2
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.10.100
}
}
virtual_server 172.16.10.71 3306{
delay_loop 6
lb_algo rr
lb_kind NAT
! persistence_timeout 50
protocol TCP
real_server 172.16.10.71 3306 {
weight 1
notify_down /etc/keepalived/killkep1.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
启动服务并测试
1)启动所有的keealived和mysql
[root@bogon ~]# systemctl start mariadb keepalived
2)测试通过VIP访问数据库
[root@bogon keepalived]# mysql -u root -P 3306 -h 172.16.10.100
Welcome to the MariaDB monitor. Commands end with ; or \g.
##可以登录数据库
总结
时间有点挤,写的有点仓促,这里我数据库连通性,完整性我已经测试过了没有问题,个人认为中小型服务这个架构完全能够满足需求,关于从备可以在主主的基础上纵向扩展。
本文介绍了一种基于MySQL和Keepalived实现的高可用双主架构部署方法,包括环境搭建、配置步骤及测试验证。
4215

被折叠的 条评论
为什么被折叠?



