场景描述
#安装数据库mariadb 主主模式 keepalived 调度
#mysql1 192.168.30.99
#mysql2 192.168.30.100
#vip 192.168.30.101
yum install mariadb-server keepalived -y
配置keepalived
global_defs {
router_id mysql-ha
} vrrp_instance VI_1 {
state BACKUP # 两个节点都为BACKUP状态,根据优先级大小判断谁为MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
nopreempt # 非抢占模式
authentication {
auth_type PASS
auth_pass 1111
} # 虚拟IP池
virtual_ipaddress {
192.168.30.101
}
} virtual_server 192.168.30.101 3306 {
delay_loop 2
lb_algo wrr #如果调度失败 可以注释这两行
lb_kind DR #
persistence_timeout 60
protocol TCP
real_server 192.168.1.99 3306 {
weight 3
notify_down /etc/keepalived/mysql.sh # 当mysql服务down了之后,执行的脚本
TCP_CHECK {
connect_timeout 10 # mysql连接超时时长(秒)
nb_get_retry 3 # mysql服务连接失败,重试次数
delay_before_retry 3 #每隔3秒检测一次mysql服务是否可用
connect_port 3306
}
}
}
#节点二配置
global_defs {
router_id mysql-ha
} vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
} virtual_ipaddress {
192.168.30.101
}
} virtual_server 192.168.30.101 3306 {
delay_loop 2
lb_algo wrr #如果调度不成功,可以将这两行注释
lb_kind DR #
persistence_timeout 60
protocol TCP
real_server 192.168.30.100 3306 {
weight 3
notify_down /etc/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
配置数据库
#配置mysql主主模式
#mysql1 192.168.30.99 配置
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin
server-id=1
symbolic-links=0
character-set-server=utf8
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid !includedir /etc/my.cnf.d service mariadb start
#创建账号用户同步
mysql -uroot -p
grant replication slave on *.* to 'backup'@'192.168.30.100' identified by 'backup'; flush privileges;
show master status;
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 | 40989 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
#在100上配置
vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin
server-id=2
symbolic-links=0
character-set-server=utf8
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid !includedir /etc/my.cnf.d
service mariadb start
mysql -uroot -p
change master to master_host='192.168.30.99',master_user='backup',master_password='backup',master_log_file='mysql-bin.000007',master_log_pos=40989;
show slave status \G;
#查看到Slave_IO_Running :yes 和Slave_SQL_Running yes 则说明同步成功,如果没有可以关闭防火墙和SELinux 然后重启服务器
setenforce 0
iptables -F
service mariadb restart
#设置99和100互为主从关系
#100上操作
mysql -uroot -p
grant replication slave on *.* to 'backup'@'192.168.30.99' identified by 'backup'; flush privileges;
show master status;
MariaDB [(none)]> show master status ;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000009 | 245 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
change master to master_host='192.168.30.100',master_user='backup',master_password='backup',master_log_file='mysql-bin.000009',master_log_pos=245;
show slave status \G;
如果同步不成功 可以重启mariadb试试
然后在99 上面创建数据表 测试
重启服务
service mariadb restart
service keepalived restart