mariadb主主复制+keepalived高可用
主机1:192.168.1.22
主机2: 192.168.1.23
VIP1:192.168.1.233
VIP2: 192.168.1.234
主机1设置:
安装需要用到的rpm包
yum -y install ipvsadm keepalived mariadb mariadb-server
启动mariadb并设置为开机启动
systemctl start mariadb
systemctl enable mariadb
设置密码
mysql_secure_installation
回车以后提示:
enter current password for root (enter for none): ## 如果是初次运行直接回车,因为没有密码
set root password?[Y/n] ##是否设置root用户密码,输入y回车
new password: ##设置root用户密码
re-enter new password: ##国际惯例再输入一次密码
remove anonymous users?[Y/n] ##是否删除匿名用户
disallow root login remotely?[Y/n] ##是否禁止root用户远程登录
remove test database and access to it?[Y/n] ## 是否删除test数据库
reload privilege tables now?[Y/n] ## 是否重新加载权限表
测试登录
mysql -uroot -p 密码
修改配置文件
vim /etc/my.cnf
[mysqld]
init_connect=‘SET collation_connection = utf8_unicode_ci‘
init_connect=‘SET NAMES utf8‘
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-bin=mysql-bin #手动添加该项配置 二进制文件的前缀
log-slave-updates=true #手动添加该项配置 开启从日志
relay-log=relay-log-bin #手动添加该项配置 中继日志文件的前缀
relay-log-index=slave-relay-bin.index #手动添加该项配置 定义中继日志的名称
server-id=11 #手动添加该项配置 ID号唯一,不能重复
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
保存退出,重启mariadb
systemctl restart mariadb
写监测脚本
vim /opt/chk_mysql.sh
#!/bin/bash
#check mysqld server status
mysqlpid=$(ps -C mysqld --no-header | wc -l)
if [ $mysqlpid -eq 0 ];then
systemctl start mariadb
sleep 3
mysqlpid=$(ps -C mysqld --no-header | wc -l)
if [ $mysqlpid -eq 0 ];then
systemctl stop keepalived
echo "Keepalived服务关闭,请检查你的mariadb服务!" | tee -a /var/log/messages
fi
fi
保存退出
修改keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_mariadb
}
vrrp_script chk_mysql {
script /opt/chk_mysql.sh #指定脚本位置
interval 2 #2秒检查一次
weight -10 # 权重 -10
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51 # VI_1里的 ID 号和从里的VI_1的ID是一致的
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql #重新指向vrrp_script
}
virtual_ipaddress {
192.168.1.233 #VIP
}
}
vrrp_instance VI_2 {
state MASTER
interface ens32
virtual_router_id 52 #区别VI_1的ID
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.1.234
}
}
主机2配置:
安装需要用到的rpm包
yum -y install ipvsadm keepalived mariadb mariadb-server
启动mariadb并设置为开机启动
systemctl start mariadb
systemctl enable mariadb
设置密码
mysql_secure_installation
回车以后提示:
enter current password for root (enter for none): ## 如果是初次运行直接回车,因为没有密码
set root password?[Y/n] ##是否设置root用户密码,输入y回车
new password: ##设置root用户密码
re-enter new password: ##国际惯例再输入一次密码
remove anonymous users?[Y/n] ##是否删除匿名用户
disallow root login remotely?[Y/n] ##是否禁止root用户远程登录
remove test database and access to it?[Y/n] ## 是否删除test数据库
reload privilege tables now?[Y/n] ## 是否重新加载权限表
测试登录
mysql -uroot -p 密码
修改配置文件
vim /etc/my.cnf
[mysqld]
init_connect=‘SET collation_connection = utf8_unicode_ci‘
init_connect=‘SET NAMES utf8‘
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-bin=mysql-bin #手动添加该项配置 二进制文件的前缀
log-slave-updates=true #手动添加该项配置 开启从日志
relay-log=relay-log-bin #手动添加该项配置 中继日志文件的前缀
relay-log-index=slave-relay-bin.index #手动添加该项配置 定义中继日志的名称
server-id=12 #手动添加该项配置 ID号唯一,不能重复
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
保存退出,重启mariadb
systemctl restart mariadb
写监测脚本
vim /opt/chk_mysql.sh
#!/bin/bash
#check mysqld server status
mysqlpid=$(ps -C mysqld --no-header | wc -l)
if [ $mysqlpid -eq 0 ];then
systemctl start mariadb
sleep 3
mysqlpid=$(ps -C mysqld --no-header | wc -l)
if [ $mysqlpid -eq 0 ];then
systemctl stop keepalived
echo "Keepalived服务关闭,请检查你的mariadb服务!" | tee -a /var/log/messages
fi
fi
保存退出
修改keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_mariadb
}
vrrp_script chk_mysql {
script /opt/chk_mysql.sh #指定脚本位置
interval 2 #2秒检查一次
weight -10 # 权重 -10
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51 # VI_1里的 ID 号和从里的VI_1的ID是一致的
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql #重新指向vrrp_script
}
virtual_ipaddress {
192.168.1.233 #VIP
}
}
vrrp_instance VI_2 {
state MASTER
interface ens32
virtual_router_id 52 #区别VI_1的ID
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_mysql
}
virtual_ipaddress {
192.168.1.234
}
}
配置互为主从复制:
首先是主机1:
给从服务器授权:
mysql -uroot -p123123
grant replication slave on *.* to ‘myslave’@’192.168.1.%’ identified by ‘123123’;
flush privileges;
show master status; #记住file的文件名 和position的id号
进入主机2
mysql -uroot -p123123
change master to master_host=’192.168.1.22’,master_user=’myslave’,master_password=’123123’,master_log_file=’mysql-bin.000004’,master_log_pos=’475’; #这里写上主机1上显示的file的文件名和id号
start slave;
show slave status \G;
主机2:
给从服务器授权:
mysql -uroot -p123123
grant replication slave on *.* to ‘myslave’@’192.168.1.%’ identified by ‘123123’;
flush privileges;
show master status; #记住file的文件名 和position的id号
进入主机1
mysql -uroot -p123123
change master to master_host=’192.168.1.23’,master_user=’myslave’,master_password=’123123’,master_log_file=’mysql-bin.000004’,master_log_pos=’475’; #这里写上主机1上显示的file的文件名和id号
start slave;
show slave status \G;
配置完成
原文地址:https://www.cnblogs.com/liuYGoo/p/9451305.html