1.两台MySQL互为主从
2.采用keepalived来实现MySQL的故障转移
3.同时只有一台MySQL能进行读写,另一台MySQL只能读
集群环境
Master/Slave Centos7.2 192.168.1.10
Slave/Master Centos7.2 192..168.1.20
vip 192.168.1.100
- 关闭防火墙以及Selinux
systemctl stop firewalld;systemctl disable firewalld
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
- 安装mariadb,并启动服务
yum install mariadb mariadb-server
systemctl restart mariadb;systemctl enable mariadb
- 修改mariadb配置文件
vim /etc/my.conf
[mysqld]
server-id=1 节点标识(主,从不能一样)
log-bin=mysql-bin 日志文件命名格式
relay-log=myslql-relay-bin relay-log日志文件命名格式
replicate_wild_ignore_table=mysql.% 复制过滤(不复制mysql库)
replicate_wild_ignore_table=test.%
replicate_wild_ignore_table=information_schema.%
(relicate_wild_do_table)定义需要复制的表或者库
- 重置数据库密码,复制DB1mysql文件到DB2上覆盖
DB1
mysql_secure_installation
cd /var/lib
tar zcvf mysql.tar.gz mysql
scp mysql.tar.gz 192.168.1.20:/var/lib
DB2
tar zxvf mysql.tar.gz
-
创建复制用户
在DB1创建复制用户
mysql -u root -p
12345(数据库登录密码)
grant replication slave on *.* to ‘test'@'192.168.1.20' identified by 'passwd';
show master status;
在DB2上创建复制用户
mysql -u root -p
12345(数据库登录密码)
grant replication slave on . to ‘test'@'192.168.1.10' identified by 'passwd';
show master status;
- **分别在两台mariadb服务器上将对方设置为自己的主服务器
在DB2上将DB1设为自己的主服务器
mysql -u root -p
12345(数据库登录密码)
change master to
master_host='192.168.1.10',master_user='test',master_pass
ord='passwd' master_log_file='mysql
bin.0000006',master_log_pos=245;
(master_log_file和master_log_pos是在DB1上用 show master
status查询到的信息)
在DB1上将DB2设为自己的主服务
change master to master_host='192.168.1.20',master_user='test',master_password='passwd' master_log_file='mysql-bin.0000005',master_log_pos=245;
(master_log_file和master_log_pos是在DB2上用 show master status查询到的信息)
- 使用show slave status\G查看复制是否搭建成功。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
表示成功
- 在两个节点上启动slave服务
mariadb> start slave
使用Keepalived实现双主高可用
编辑配置文件,用下面类容覆盖
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
212129925@qq.com
}
#notification_email_from 212129925@qq.com
#smtp_server smtp.qq.com
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_mysql_port {
script "</dev/tcp/127.0.0.1/3306"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP 两个节点上同时为BACKUP
interface ens33
virtual_router_id 51
priority 101
advert_int 1
nopreempt 优先级高的上面配置非抢占
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
track_script {
chk_mysql_port
}
track_interface {
ens33
}
}
网友评论