目录
机器准备
安装后服务
redis
安装redis
mariadb
安装mariadb
启动和配置
互为主从同步配置
keepalived
keepalived安装
修改主从 keepalived的配置
主从配置-mariadb监控
主从配置-redis监控
查看和使用
Keepalived
Mariadb
redis
机器准备
两台机器:172.171.16.108-109
操作系统 :centos7.6
两个虚拟ip :172.171.16.110-111
关闭防火墙
关闭防火墙
//临时关闭
systemctl stop firewalld
//禁止开机启动
systemctl disable firewalld
安装后服务
172.171.16.108
主数据库:Mariadb 10.5 3306 root 123456
主redis:Redis5.05 6379 123456
Keepalived
172.171.16.109
主数据库:Mariadb 10.5 3306 root 123456
从redis:Redis5.05 6379 123456
Keepalived
Mariadb 虚拟ip 172.171.16.110
Redis虚拟ip 172.171.16.111
redis
安装redis
1.下载redis,并解压到/home/redis/目录下
mkdir /home/redis/
cd /home/redis/
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar -xzvf redis-5.0.5.tar.gz
2.安装编译环境
yum -y install make automake cmake gcc g++
cd /home/redis/redis-5.0.5/
3.编译安装redis
make && make install
4、配置开机自启
vi /etc/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart= /home/redis/redis-5.0.5/src/redis-server /home/redis/redis-5.0.5/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
ExecStart=/opt/software/redis-6.2.6/src/redis-server :启动服务的命令的位置
/etc/redis.conf 配置文件命令的位置
systemctl daemon-reload
systemctl enable redis
5.修改两台服务器上的redis配置文件,绑定所有ip,后台方式运行进程,设置密码
vim redis.conf
主库 172.171.16.108
bind 0.0.0.0
protected-mode no
pidfile /home/redis/redis_6379.pid
daemonize yes
dir /home/redis
logfile "/home/redis/redis_6379.log"
requirepass 123456
从库 172.171.16.109
bind 0.0.0.0
protected-mode no
pidfile /home/redis/redis_6379.pid
daemonize yes
dir /home/redis
logfile "/home/redis/redis_6379.log"
requirepass 123456
replicaof 172.171.16.108 6379
masterauth 123456
命令
# 启动
systemctl start redis
# 停止
systemctl stop redis
# 重启
systemctl restart redis
# 查看状态
systemctl status redis
mariadb
安装mariadb
查看已安装的包
命令:rpm -qa | grep mysql
如果存在已安装,逐个卸载掉这些包
命令:rpm -e --nodeps
配置yum源,并yum安装
vi /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.5.9 CentOS repository list
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
yum update
yum install MariaDB-server MariaDB-client
配置两台服务器上的数据库配置文件
vim /etc/my.cnf.d/server.cnf
主库 172.171.16.108
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
skip-name-resolve
#event_scheduler=ON
datadir=/home/mariadb
socket=/home/mariadb/mysql.sock
slow_query_log=on
slow_query_log_file=/home/mariadb/slow_query_log.log
long_query_time=10
max_allowed_packet=512M
max_connections=1000
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
lower_case_table_names=0
#skip_name_resolve=ON
innodb_file_per_table=ON
server-id=108
log-bin=mysql-bin
log-bin-index=master-bin.index
# ignored databases
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
# ignored tables
# #replicate-ignore-table=test
auto-increment-increment=2
auto-increment-offset=1
slave-skip-errors=all
relay_log=slave_relay_bin
wait_timeout=31536000
interactive_timeout=31536000
thread_pool_size=128
thread_pool_stall_limit=10
# Specifies the number of days before automatic removal of binary log files
expire_logs_days=30
#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
innodb_flush_log_at_trx_commit=0
innodb_autoextend_increment=128M
innodb_log_buffer_size=16M
innodb_log_file_size=128M
# this is only for embedded server
[embedded]
# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
# This group is only read by MariaDB-10.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.5]
主库 172.171.16.109
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
skip-name-resolve
#event_scheduler=ON
datadir=/home/mariadb
socket=/home/mariadb/mysql.sock
slow_query_log=on
slow_query_log_file=/home/mariadb/slow_query_log.log
long_query_time=10
max_allowed_packet=512M
max_connections=1000
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
lower_case_table_names=0
#skip_name_resolve=ON
innodb_file_per_table=ON
server-id=109
log-bin=mysql-bin
log-bin-index=master-bin.index
# ignored databases
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
# ignored tables
# #replicate-ignore-table=test
auto-increment-increment=2
auto-increment-offset=2
slave-skip-errors=all
relay_log=slave_relay_bin
wait_timeout=31536000
interactive_timeout=31536000
thread_pool_size=128
thread_pool_stall_limit=10
# Specifies the number of days before automatic removal of binary log files
expire_logs_days=30
#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
innodb_flush_log_at_trx_commit=0
innodb_autoextend_increment=128M
innodb_log_buffer_size=16M
innodb_log_file_size=128M
# this is only for embedded server
[embedded]
# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]
# This group is only read by MariaDB-10.5 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.5]
创建数据库目录,并将原有的数据库目录下文件复制到新的目录下,给sock文件设置软连接
mkdir /home/mariadb
chown -R mysql:mysql /home/mariadb
cp -a /var/lib/mysql/* /home/mariadb
ln -s /home/mariadb/mysql.sock /var/lib/mysql/mysql.sock
启动和配置
命令
systemctl start mariadb #启动MariaDB
systemctl stop mariadb #停止MariaDB
systemctl restart mariadb #重启MariaDB
systemctl enable mariadb #设置开机启动
对两台服务器上的数据库做初始化配置
mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n] n
... skipping.
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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] y
... 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] n
... skipping.
By default, MariaDB 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] y
- 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] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
设置远程登录 ,并更新生效
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;
互为主从同步配置
保持 108 和 109 上的数据库、表完全一致
通过执行如下sql 设置两台数据库互相同步
108认为是master1 需要同步master2
change master 'master2' to master_host='172.171.16.109',master_port=3306,master_user='root',master_password='123456';
start SLAVE 'master2';
show all slaves status;
109认为是master2 需要同步master1
change master 'master1' to master_host='172.171.16.108',master_port=3306,master_user='root',master_password='123456';
start SLAVE 'master1';
show all slaves status;
keepalived
keepalived安装
卸载
查找安装目录
[root@CentOS-003 keepalived]# find / -name keepalived
删除安装目录
rm -rf /etc/sysconfig/keepalived
rm -rf /etc/keepalived
rm -rf /var/lib/selinux/targeted/active/modules/100/keepalived
rm -rf /usr/sbin/keepalived
rm -rf /usr/share/doc/keepalived
rm -rf /usr/share/selinux/targeted/default/active/modules/100/keepalived
rm -rf /usr/libexec/keepalived
rm -rf /etc/selinux/targeted/active/modules/100/keepalived
查看进程
[root@CentOS-001 /]# ps aux|grep keepalived
创建目录
mkdir /home/keepalived
cd /home/keepalived
下载压缩包 keepalived-2.0.20.tar.gz 并放在该目录下
解压
tar xvf keepalived-2.0.20.tar.gz
安装
[root@CentOS-003 opt]# cd keepalived-2.0.20/
[root@CentOS-003 keepalived-2.0.20]# ./configure --prefix=/usr/local/keepalived
如果报错
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
Yum安装解决
yum -y install openssl openssl-devel
解决后继续执行命令
[root@CentOS-003 keepalived-2.0.20]# ./configure --prefix=/usr/local/keepalived
[root@CentOS-003 keepalived-2.0.20]# make & make install
# keepalived 启动脚本变量引用文件,默认文件路径是 /etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
[root@CentOS-001 keepalived-2.0.20]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
# 将keepalived 主程序加入到环境变量(安装目录下)
[root@CentOS-001 keepalived-2.0.20]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
# keepalived 启动脚本(源码目录下),放到 /etc/init.d/ 目录下就可以使用 service 命令便捷调用
[root@CentOS-001 keepalived-2.0.20]# cp /home/keepalived/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
# 将配置文件放到默认路径下
[root@CentOS-001 keepalived-2.0.20]# mkdir /etc/keepalived
[root@CentOS-001 keepalived-2.0.20]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chmod 644 /etc/keepalived/keepalived.conf
启动
[root@CentOS-001 keepalived-2.0.20]# service keepalived start
日志位置
[root@CentOS-001 log]# tail -f /var/log/messages
加为系统服务:chkconfig --add keepalived
开机启动:chkconfig keepalived on
查看开机启动的服务:chkconfig --list
启动、关闭、重启 service keepalived start|stop|restart
ip a show |grep -w inet
修改主从 keepalived的配置
vim /etc/keepalived/keepalived.conf
主库 172.171.16.108
global_defs {
router_id redis-mysql-master
script_user root
enable_script_security
}
vrrp_script chk_mysql
{
#监控脚本
script "/home/keepalived-scripts/mysql/mysql_check.sh"
#监控时间,脚本的执行时间间隔
interval 2
#超时时间
timeout 2
weight -15
#3次失败才算失败,会用weight减少优先级(1-255之间)
fall 3
#1次成功就算成功,但不修改优先级
rise 1
}
vrrp_script chk_redis
{
#监控脚本
script "/home/keepalived-scripts/redis/redis_check.sh"
#监控时间,脚本的执行时间间隔
interval 2
#超时时间
timeout 2
weight -15
#3次失败才算失败,会用weight减少优先级(1-255之间)
fall 3
#1次成功就算成功,但不修改优先级
rise 1
}
vrrp_instance VI_1 {
#设置为MASTER
state MASTER
#本机监控网卡
interface ens192
# garp_master_delay 10
#主从需要在同一路由编号
virtual_router_id 55
#权重值
priority 100
#默认抢占
#nopreempt
#vrrp通告的时间间隔,默认1s
advert_int 1
authentication {
#加密
auth_type PASS
#密码
auth_pass 123456
}
virtual_ipaddress {
#Keppalived虚拟出来的VIP
172.171.16.110
}
track_script {
chk_mysql
}
}
vrrp_instance VI_2 {
#设置为MASTER
state MASTER
#本机监控网卡
interface ens192
#主从需要在同一路由编号
virtual_router_id 60
#权重值
priority 100
#vrrp通告的时间间隔,默认1s
advert_int 5
authentication {
#加密
auth_type PASS
#密码
auth_pass 123456
}
virtual_ipaddress {
#Keppalived虚拟出来的VIP
172.171.16.111
}
track_script {
chk_redis
}
#keepalived成为master
notify_master /home/keepalived-scripts/redis/redis_master.sh
#keepalived成为backup
notify_backup /home/keepalived-scripts/redis/redis_slave.sh
#keepalived fault时
notify_fault /home/keepalived-scripts/redis/redis_fault.sh
#keepalived服务中止时
notify_stop /home/keepalived-scripts/redis/redis_stop.sh
}
主库 172.171.16.109
global_defs {
router_id redis-mysql-slave
script_user root
enable_script_security
}
vrrp_script chk_mysql
{
#监控脚本
script "/home/keepalived-scripts/mysql/mysql_check.sh"
#监控时间,脚本的执行时间间隔
interval 2
#超时时间
timeout 2
weight -15
#3次失败才算失败,会用weight减少优先级(1-255之间)
fall 3
#1次成功就算成功,但不修改优先级
rise 1
}
vrrp_script chk_redis
{
#监控脚本
script "/home/keepalived-scripts/redis/redis_check.sh"
#监控时间,脚本的执行时间间隔
interval 2
#超时时间
timeout 2
weight -15
#3次失败才算失败,会用weight减少优先级(1-255之间)
fall 3
#1次成功就算成功,但不修改优先级
rise 1
}
vrrp_instance VI_1 {
#设置为BACKUP
state BACKUP
#本机监控网卡
interface em1
# garp_master_delay 10
#主从需要在同一路由编号
virtual_router_id 55
#权重值
priority 90
#默认抢占
#nopreempt
#vrrp通告的时间间隔,默认1s
advert_int 1
authentication {
#加密
auth_type PASS
#密码
auth_pass 123456
}
virtual_ipaddress {
#Keppalived虚拟出来的VIP
172.171.16.110
}
track_script {
chk_mysql
}
}
vrrp_instance VI_2 {
#设置为BACKUP
state BACKUP
#本机监控网卡
interface em1
#主从需要在同一路由编号
virtual_router_id 60
#权重值
priority 90
#vrrp通告的时间间隔,默认1s
advert_int 5
authentication {
#加密
auth_type PASS
#密码
auth_pass 123456
}
virtual_ipaddress {
#Keppalived虚拟出来的VIP
172.171.16.111
}
track_script {
chk_redis
}
#keepalived成为master
notify_master /home/keepalived-scripts/redis/redis_master.sh
#keepalived成为backup
notify_backup /home/keepalived-scripts/redis/redis_slave.sh
#keepalived fault时
notify_fault /home/keepalived-scripts/redis/redis_fault.sh
#keepalived服务中止时
notify_stop /home/keepalived-scripts/redis/redis_stop.sh
}
主从配置-mariadb监控
创建监控脚本目录,用来放监控脚本文件
mkdir /home/keepalived-scripts
mkdir /home/keepalived-scripts/mysql
cd /home/keepalived-scripts/mysql
vim mysql_check.sh(当前目录的 /script目录下)
编辑mysql_check.sh脚本,108、109服务器一致
#!/bin/bash
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
#mysql is working MYSQL_OK is 1, mysql down MYSQL_OK is 0
MYSQL_OK=1
#select 1;
function check_mysql_helth() {
mysqladmin -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} ping &>/dev/null
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
echo $MYSQL_OK
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ]; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
exit 1
fi
sleep 2
done
主从配置-redis监控
mkdir /home/keepalived-scripts
mkdir /home/keepalived-scripts/redis
mkdir /home/keepalived-scripts/log
cd /home/keepalived-scripts/ redis
vim 编辑如下脚本
/home/keepalived-scripts/redis/redis_master.sh
/home/keepalived-scripts/redis/redis_slave.sh
/home/keepalived-scripts/redis/redis_fault.sh
/home/keepalived-scripts/redis/redis_stop.sh
/home/keepalived-scripts/redis/redis_stop.sh
具体查看下面链接中资源
keepalived 监测mysql&&redis 高可用配置和脚本https://download.csdn.net/download/mengo1234/87954486
chmod +x /home/keepalived-scripts/redis/*
redis脚本日志所在位置:/home/keepalived-scripts/log
chmod +x /home/keepalived-scripts/mysql/*
查看和使用
Keepalived
启动、关闭、重启 service keepalived start|stop|restart
查看keepalived集群主节点在哪台服务器,执行命令:ip a
查看日志:tail -f /var/log/messages
Mariadb
systemctl start mariadb #启动MariaDB systemctl stop mariadb #停止MariaDB systemctl restart mariadb #重启MariaDB systemctl enable mariadb #设置开机启动
redis
启动
systemctl start redis
# 停止
systemctl stop redis
# 重启
systemctl restart redis
# 查看状态
systemctl status redis
登录:redis-cli
查看信息:Info
查看主从信息:info Replicatio
切换主从:replicaof NO ONE
replicaof 172.171.16.109 6379 108上redis从109
replicaof 172.171.16.108 6379 109上redis从108