化学网站定制,vps网站建设,制作一个网站的费用是多少钱,logo设计在线MySQL双主一从高可用 文章目录 MySQL双主一从高可用环境说明1.配置前的准备工作2.配置yum源 1.在部署NFS服务2.安装主数据库的数据库服务#xff0c;并挂载nfs3.初始化数据库4.配置两台master主机数据库5.配置m1和m2成为主数据库6.安装、配置keepalived7.安装部署从数据库8.测…MySQL双主一从高可用 文章目录 MySQL双主一从高可用环境说明1.配置前的准备工作2.配置yum源 1.在部署NFS服务2.安装主数据库的数据库服务并挂载nfs3.初始化数据库4.配置两台master主机数据库5.配置m1和m2成为主数据库6.安装、配置keepalived7.安装部署从数据库8.测试1.在高可用集群中当master1主机为主时master2主机为备2.模拟master1主机发生故障导致服务宕机master1主机上的keepalived服务则会自动关闭释放资源vip则会跳转到master2主机上从而使得master2主机接替主数据库位置开启mysql服务 环境说明
nfs服务器用于同步两台主服务器的数据确保一致。
主机名称IP地址充当角色所需软件操作系统nfs192.168.195.133NFS服务器nfscentos 8master1192.168.195.134mysql主服务器主节点mysql、keepalivedcentos 8master2192.168.195.135mysql主服务器备节点mysql、keepalivedcentos 8slave192.168.195.136mysql从服务器mysqlcentos 8
1.配置前的准备工作
永久关闭所有主机的防火墙和selinux
//nfs主机
[rootnfs ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[rootnfs ~]# sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config
[rootnfs ~]# reboot//master1主机
[rootmaster1 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[rootmaster1 ~]# sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config
[rootmaster1 ~]# reboot//master2主机
[rootmaster2 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[rootmaster2 ~]# sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config
[rootmaster2 ~]# reboot//slave主机
[rootslave ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[rootslave ~]# sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config
[rootslave ~]# reboot2.配置yum源
master主机加上epel源
推荐使用阿里云源
//nfs主机
[rootnfs ~]# rm -rf /etc/yum.repos.d/*
[rootnfs ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[rootnfs ~]# yum clean all
[rootnfs ~]# yum makecache //master1主机
[rootmaster1 ~]# rm -rf /etc/yum.repos.d/*
[rootmaster1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[rootmaster1 ~]# yum -y install epel-release wget vim
[rootmaster1 ~]# yum clean all
[rootmaster1 ~]# yum makecache//master2主机
[rootmaster2 ~]# rm -rf /etc/yum.repos.d/*
[rootmaster2 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[rootmaster2 ~]# yum -y install epel-release wget vim
[rootmaster2 ~]# yum clean all
[rootmaster2 ~]# yum makecache//slave主机
[rootslave ~]# rm -rf /etc/yum.repos.d/*
[rootslave ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[rootslave ~]# yum clean all
[rootslave ~]# yum makecache1.在部署NFS服务
在nfs主机上部署nfs服务
//安装nfs服务
[rootnfs ~]# yum -y install nfs-utils.x86_64//创建一个共享目录
[rootnfs ~]# mkdir /opt/data
[rootnfs ~]# vim /etc/exports
[rootnfs ~]# cat /etc/exports
/opt/data 192.168.195.133(rw,sync,no_root_squash)
/opt/data 192.168.195.134(rw,sync,no_root_squash)//启动服务
[rootnfs ~]# systemctl restart rpcbind.service
[rootnfs ~]# systemctl restart nfs-server.service
[rootnfs ~]# systemctl enable --now nfs-server.service
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.2.安装主数据库的数据库服务并挂载nfs
在master1和master2上安装mysql数据库
在master1主机上
//创建mysql用户
[rootmaster1 ~]# groupadd -r -g 306 mysql
[rootmaster1 ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql//解压mysql二进制包
[rootmaster1 ~]# ls
anaconda-ks.cfg mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[rootmaster1 ~]# tar xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[rootmaster1 ~]# ln -sv /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/ /usr/local/mysql
/usr/local/mysql - /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64///修改mysql目录属主属组并添加环境变量
[rootmaster1 ~]# chown mysql:mysql /usr/local/mysql
[rootmaster1 ~]# echo export PATH/usr/local/mysql/bin:$PATH /etc/profile.d/mysql.sh
[rootmaster1 ~]# source /etc/profile.d/mysql.sh
[rootmaster1 ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin//建立数据存放目录
[rootmaster1 ~]# mkdir /opt/data
[rootmaster1 ~]# chown -R mysql:mysql /opt/data///挂载nfs上的/opt/data目录以便同步
[rootmaster1 ~]# yum -y install nfs-utils.x86_64
[rootmaster1 ~]# systemctl enable --now nfs-server.service
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
[rootmaster1 ~]# mount -t nfs 192.168.195.133:/opt/data /opt/data//配置头文件和lib库文件路径
[rootmaster1 ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
/usr/local/include/mysql - /usr/local/mysql/include/
[rootmaster1 ~]# echo /usr/local/mysql/lib /etc/ld.so.conf.d/mysql.conf
[rootmaster1 ~]# ldconfig//生成mysql配置文件/etc/my.cnf
[rootmaster1 ~]# vim /etc/my.cnf
[rootmaster1 ~]# cat /etc/my.cnf
[mysqld]
basedir /usr/local/mysql
datadir /opt/data
socket /tmp/mysql.sock
port 3306
pid-file /opt/data/mysql.pid
user mysql
skip-name-resolve
[rootmaster1 ~]#//配置服务启动脚本
[rootmaster1 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[rootmaster1 ~]# sed -ri s#^(basedir).*#\1/usr/local/mysql#g /etc/init.d/mysqld
[rootmaster1 ~]# sed -ri s#^(datadir).*#\1/opt/data#g /etc/init.d/mysqld在master2主机上
//创建mysql用户
[rootmaster2 ~]# groupadd -r -g 306 mysql
[rootmaster2 ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql//解压mysql二进制包将master1主机上的mysql二进制包用scp命令传送过来
[rootmaster1 ~]# scp mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz root192.168.195.135:/root/
The authenticity of host 192.168.195.135 (192.168.195.135) cant be established.
ECDSA key fingerprint is SHA256:rX8qo9h9Jq89dUg33ZnWDL7KT30i/It603EM37Mic.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 192.168.195.135 (ECDSA) to the list of known hosts.
root192.168.195.135s password:
mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz 100% 645MB 127.1MB/s 00:05
[rootmaster2 ~]# ls
anaconda-ks.cfg mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[rootmaster2 ~]# tar xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[rootmaster2 ~]# ln -sv /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/ /usr/local/mysql
/usr/local/mysql - /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/
[rootmaster2 ~]# ll /usr/local/
total 0
drwxr-xr-x. 2 root root 6 Aug 12 2018 bin
drwxr-xr-x. 2 root root 6 Aug 12 2018 etc
drwxr-xr-x. 2 root root 6 Aug 12 2018 games
drwxr-xr-x. 2 root root 6 Aug 12 2018 include
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64
drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec
lrwxrwxrwx 1 root root 47 Oct 15 18:50 mysql - /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/
drwxr-xr-x 9 root root 129 Oct 15 18:50 mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin
drwxr-xr-x. 5 root root 49 Jul 20 11:33 share
drwxr-xr-x. 2 root root 6 Aug 12 2018 src//修改mysql目录的属主属组,并添加环境变量
[rootmaster2 ~]# chown -R mysql:mysql /usr/local/mysql
[rootmaster2 ~]# echo export PATH/usr/local/mysql/bin:$PATH /etc/profile.d/mysql.sh
[rootmaster2 ~]# source /etc/profile.d/mysql.sh
[rootmaster2 ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin//建立数据存放目录
[rootmaster2 ~]# mkdir /opt/data
[rootmaster2 ~]# chown -R mysql:mysql /opt/data///挂载nfs上的/opt/data目录以便同步
[rootmaster2 ~]# yum -y install nfs-utils.x86_64
[rootmaster2 ~]# systemctl enable --now nfs-server.service
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
[rootmaster2 ~]# mount -t nfs 192.168.195.133:/opt/data /opt/data//配置头文件和lib库文件路径
[rootmaster2 ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
/usr/local/include/mysql - /usr/local/mysql/include/
[rootmaster2 ~]# echo /usr/local/mysql/lib /etc/ld.so.conf.d/mysql.conf
[rootmaster2 ~]# ldconfig//生成配置文件
[rootmaster2 ~]# vim /etc/my.cnf
[rootmaster2 ~]# cat /etc/my.cnf
[mysqld]
basedir /usr/local/mysql
datadir /opt/data
socket /tmp/mysql.sock
port 3306
pid-file /opt/data/mysql.pid
user mysql
skip-name-resolve//配置脚本启动服务
[rootmaster2 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[rootmaster2 ~]# sed -ri s#^(basedir).*#\1/usr/local/mysql#g /etc/init.d/mysqld
[rootmaster2 ~]# sed -ri s#^(datadir).*#\1/opt/data#g /etc/init.d/mysqld3.初始化数据库
由于我们配置了nfs同步两台主机的/opt/data目录所以只需在其中一台上初始化数据库即可另一台会 自动同步/opt/data目录下的文件
在master1主机上初始化数据库
[rootmaster1 ~]# /usr/local/mysql/bin/mysqld --initialize --usermysql --datadir/opt/data/
2023-10-15T13:09:26.800119Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-10-15T13:09:27.203177Z 0 [Warning] InnoDB: New log files created, LSN45790
2023-10-15T13:09:27.264266Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-10-15T13:09:27.322831Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 11b3dfb4-6b5c-11ee-9bf0-000c298b9baf.
2023-10-15T13:09:27.325547Z 0 [Warning] Gtid table is not ready to be used. Table mysql.gtid_executed cannot be opened.
2023-10-15T13:09:27.589254Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-10-15T13:09:27.589289Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-10-15T13:09:27.593109Z 0 [Warning] CA certificate ca.pem is self signed.
2023-10-15T13:09:27.644471Z 1 [Note] A temporary password is generated for rootlocalhost: gyOij-dGi0K# //最后一行随机生成一个密码“gyOij-dGi0K#”//查看nfs主机和两台master主机的/opt/data目录的内容
nfs主机
[rootnfs ~]# ls /opt/data/
auto.cnf client-cert.pem ibdata1 mysql public_key.pem sys
ca-key.pem client-key.pem ib_logfile0 performance_schema server-cert.pem
ca.pem ib_buffer_pool ib_logfile1 private_key.pem server-key.pemmaster1主机
[rootmaster1 ~]# ls /opt/data/
auto.cnf client-cert.pem ibdata1 mysql public_key.pem sys
ca-key.pem client-key.pem ib_logfile0 performance_schema server-cert.pem
ca.pem ib_buffer_pool ib_logfile1 private_key.pem server-key.pemmaster2主机
[rootmaster2 ~]# ls /opt/data/
auto.cnf client-cert.pem ibdata1 mysql public_key.pem sys
ca-key.pem client-key.pem ib_logfile0 performance_schema server-cert.pem
ca.pem ib_buffer_pool ib_logfile1 private_key.pem server-key.pem4.配置两台master主机数据库
//先在两台主机上安装一个库文件包
[rootmaster1 ~]# yum -y install ncurses-compat-libs[rootmaster2 ~]# yum -y install ncurses-compat-libs//首先启动master1主机的数据库服务由于两个数据库完全一模一样所以只可以启动一台不可以同时启动
[rootmaster1 ~]# service mysqld start
Starting MySQL.Logging to /opt/data/master1.err.
SUCCESS!
[rootmaster1 ~]#//使用初始密码登录设置密码
[rootmaster1 ~]# mysql -uroot -pgyOij-dGi0K#
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type help; or \h for help. Type \c to clear the current input statement.mysql set password password(12345678);
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql quit
Bye
[rootmaster1 ~]# 在master2主机上通过在master1给mysql设置的密码登录数据库
//首先需要关闭master1主机上的mysql服务
[rootmaster1 ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[rootmaster1 ~]#//启动m2的数据库服务
[rootmaster2 ~]# service mysqld start
Starting MySQL.Logging to /opt/data/master2.err.SUCCESS!
[rootmaster2 ~]#//尝试使用修改过后的密码登录m2的数据库
[rootmaster2 ~]# mysql -uroot -p12345678
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type help; or \h for help. Type \c to clear the current input statement.mysql quit
Bye //可以登录
[rootmaster2 ~]#//测试完后关闭m2的数据库服务
[rootmaster2 ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[rootmaster2 ~]#5.配置m1和m2成为主数据库
master1主机
[rootmaster1 ~]# mysql -uroot -p12345678
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type help; or \h for help. Type \c to clear the current input statement.mysql grant replication slave on *.* to ftx192.168.195.136 identified by 12345678; //创建一个同步账号授权给从数据库使用
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql flush privileges; //重读授权表
Query OK, 0 rows affected (0.00 sec)mysql quit
Bye
[rootmaster1 ~]#//修改配置文件
[rootmaster1 ~]# vim /etc/my.cnf
[rootmaster1 ~]# cat /etc/my.cnf
[mysqld]
basedir /usr/local/mysql
datadir /opt/data
socket /tmp/mysql.sock
port 3306
pid-file /opt/data/mysql.pid
user mysql
skip-name-resolve
log-binmysql_bin //添加此行
server-id10 //添加此行id全局唯一不可重复//重启服务
[rootmaster1 ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[rootmaster1 ~]# service mysqld start
Starting MySQL. SUCCESS!
[rootmaster1 ~]#//然后关闭服务确保两台master主机只启动一台
[rootmaster1 ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[rootmaster1 ~]#//配置master2主机由于/opt/data的是同步的所以不需要再创建用户授权只用改配置文件
[rootmaster2 ~]# vim /etc/my.cnf
[rootmaster2 ~]# cat /etc/my.cnf
[mysqld]
basedir /usr/local/mysql
datadir /opt/data
socket /tmp/mysql.sock
port 3306
pid-file /opt/data/mysql.pid
user mysql
skip-name-resolve
log-binmysql_bin //添加此行
server-id11 //添加此行id全局唯一不可重复//重启服务然后关闭服务
[rootmaster2 ~]# service mysqld start
Starting MySQL. SUCCESS!
[rootmaster2 ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[rootmaster2 ~]#6.安装、配置keepalived
master1主机
//在master1主机上安装keepalived服务
[rootmaster1 ~]# yum -y install keepalived.x86_64//在master1主机上配置keepalived所需脚本
[rootmaster1 ~]# mkdir /scripts cd /scripts
[rootmaster1 scripts]# vim check_mysql.sh
[rootmaster1 scripts]# chmod x check_mysql.sh
[rootmaster1 scripts]# cat check_mysql.sh //该脚本得出是否存在mysql服务进程若没有则进入判断执行关闭keepalived服务的命令
#!/bin/bashmysql_status$(ps -ef | grep -Ev grep|$0 | grep \bmysql\b|wc -l)if [ $mysql_status -lt 1 ];then
systemctl stop keepalived
fi[rootmaster1 scripts]# ll
total 4
-rwxr-xr-x 1 root root 145 Oct 15 22:25 check_mysql.sh//先将原配置文件备份一下然后修改生成一个新的配置文件
[rootmaster1 ~]# cp /etc/keepalived/keepalived.conf /opt/
[rootmaster1 ~]# vim /etc/keepalived/keepalived.conf
[rootmaster1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id mysql1 //路由器id两台主机要不一样
}vrrp_script check_mysql {script /scripts/check_mysql.sh //脚本的路径interval 1fall 3weight -40
}vrrp_instance VI_1 {state MASTER //初始状态MASTER或BACKUPinterface ens160 //vrrp示例绑定的网卡接口和真实网卡一致virtual_router_id 80 //虚拟路由器id两台主机要一样priority 100 //优先级优先级越大就是主服务器advert_int 1authentication {auth_type PASSauth_pass 12345678 //密码可以自定义}virtual_ipaddress {192.168.195.100 //vip}track_script {check_mysql //追踪的脚本}
}virtual_server 192.168.195.100 80 { //配置虚拟服务器delay_loop 6 //健康检查时间间隔lb_algo rr //lvs调度算法lb_kind NAT //lvs模式persistence_timeout 50 //持久化超时时间单位为秒protocol TCPreal_server 192.168.195.134 80 { //指向第1台主数据库的ipmaster1主机weight 1TCP_CHECK {connect_port 3306connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.195.135 80 { //指向第2台主数据库的ipmaster2主机weight 1TCP_CHECK {connect_port 3306connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
[rootmaster1 ~]#//重启keepalived服务以重读keepalived配置文件
[rootmaster1 ~]# systemctl restart keepalived.service
master2主机
//在master2主机上安装keepalived服务
[rootmaster2 ~]# yum -y install keepalived.x86_64//在master2主机上配置keepalived所需脚本
[rootmaster2 ~]# mkdir /scripts cd /scripts
[rootmaster2 scripts]# vim notify.sh
[rootmaster2 scripts]# chmod x notify.sh
[rootmaster2 scripts]# cat notify.sh
#!/bin/bashcase $1 inmaster)mysql_status$(ps -ef|grep -Ev grep|$0|grep \bmysql\b|wc -l)if [ $mysql_status -lt 1 ];thenservice mysqld startfi;;backup)mysql_status$(ps -ef|grep -Ev grep|$0|grep \bmysql\b|wc -l)if [ $mysql_status -gt 0 ];thenservice mysqld stopfi;;*)echo Usage:$0 master|backup;;
esac[rootmaster2 scripts]# ll
total 4
-rwxr-xr-x 1 root root 419 Oct 15 22:48 notify.sh//先将原配置文件备份一下然后修改生成一个新的配置文件
[rootmaster2 ~]# cp /etc/keepalived/keepalived.conf /opt/
[rootmaster2 ~]# vim /etc/keepalived/keepalived.conf
[rootmaster2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id mysql2
}vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 80priority 80advert_int 1authentication {auth_type PASSauth_pass 12345678}virtual_ipaddress {192.168.195.100}notify_master /scripts/notify.sh masternotify_backup /scripts/notify.sh backup
}virtual_server 192.168.195.100 80 {delay_loop 6lb_algo rrlb_kind NATpersistence_timeout 50protocol TCPreal_server 192.168.195.134 80 {weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.195.135 80 {weight 1TCP_CHECK {connect_port 80 connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
[rootmaster2 ~]#//重启服务以重读配置文件
[rootmaster2 ~]# systemctl restart keepalived.service查看vip
我们先手动开启master1主机的mysql服务和keepalived服务开启master1主机上的mysql服务之前需关闭master2主机上的mysql服务
//当前在我们的mysql服务和keepalived服务都开启的状态下存在vip
[rootmaster1 ~]# systemctl is-active keepalived.service
active
[rootmaster1 ~]# ss -antl | grep 3306
LISTEN 0 80 *:3306 *:*
[rootmaster1 ~]# ip a show ens160 | grep 192.168.195.100inet 192.168.195.100/32 scope global ens160
[rootmaster1 ~]# ip a show ens160 | grep 192.168.195.100 | wc -l
1//查看master2主机上的服务状态应该是keepalived启动mysql未启动没有vip
[rootmaster2 ~]# systemctl is-active keepalived.service
active
[rootmaster2 ~]# ss -antl | grep 3306
[rootmaster2 ~]# ss -antl | grep 3306 | wc -l
0
[rootmaster2 ~]# ip a show ens160 | grep 192.168.195.100
[rootmaster2 ~]# ip a show ens160 | grep 192.168.195.100 | wc -l
07.安装部署从数据库
在slave主机上
//安装一个库文件包
[rootslave ~]# yum -y install ncurses-compat-libs//创建mysql用户和组
[rootslave ~]# groupadd -r -g 306 mysql
[rootslave ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql//解压二进制包到/usr/local/
[rootmaster1 ~]# scp mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz root192.168.195.136:/root/
root192.168.195.136s password:
mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz 100% 645MB 122.6MB/s 00:05
[rootslave ~]# ls
anaconda-ks.cfg mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[rootslave ~]# tar xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local///创建目录链接并修改目录/usr/local/mysql的属主属组
[rootslave ~]# ln -sv /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/ /usr/local/mysql
/usr/local/mysql - /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64/
[rootslave ~]# chown -R mysql:mysql /usr/local/mysql
[rootslave ~]# ll -d /usr/local/mysql
lrwxrwxrwx 1 mysql mysql 47 Oct 15 23:16 /usr/local/mysql - /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64///添加环境变量
[rootslave ~]# echo export PATH/usr/local/mysql/bin:$PATH /etc/profile.d/mysql.sh
[rootslave ~]# source /etc/profile.d/mysql.sh
[rootslave ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin//建立数据存放目录
[rootslave ~]# mkdir /opt/data
[rootslave ~]# chown -R mysql:mysql /opt/data///初始化数据库
[rootslave ~]# /usr/local/mysql/bin/mysqld --initialize --usermysql --datadir/opt/data/
2023-10-15T15:19:37.491589Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-10-15T15:19:37.612968Z 0 [Warning] InnoDB: New log files created, LSN45790
2023-10-15T15:19:37.634355Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-10-15T15:19:37.688134Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 410b0a73-6b6e-11ee-8f2f-000c29d9f7e8.
2023-10-15T15:19:37.688743Z 0 [Warning] Gtid table is not ready to be used. Table mysql.gtid_executed cannot be opened.
2023-10-15T15:19:38.020453Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-10-15T15:19:38.020499Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-10-15T15:19:38.020906Z 0 [Warning] CA certificate ca.pem is self signed.
2023-10-15T15:19:38.127308Z 1 [Note] A temporary password is generated for rootlocalhost: ZKyqhRghv0/Y //最后一行随机生成一个登录mysql数据库密码“ZKyqhRghv0/Y”//配置头文件和lib库文件路径
[rootslave ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
/usr/local/include/mysql - /usr/local/mysql/include/
[rootslave ~]# echo /usr/local/mysql/lib /etc/ld.so.conf.d/mysql.conf
[rootslave ~]# ldconfig//生成mysql配置文件
[rootslave ~]# vim /etc/my.cnf
[rootslave ~]# cat /etc/my.cnf
[mysqld]
basedir /usr/local/mysql
datadir /opt/data
socket /tmp/mysql.sock
port 3306
pid-file /opt/data/mysql.pid
user mysql
skip-name-resolve
server-id20
relay-logmysql_relay_bin
[rootslave ~]#//配置服务启动脚本
[rootslave ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[rootslave ~]# sed -ri s#^(basedir).*#\1/usr/local/mysql#g /etc/init.d/mysqld
[rootslave ~]# sed -ri s#^(datadir).*#\1/opt/data#g /etc/init.d/mysqld//重新设置mysql数据库密码
[rootslave ~]# mysql -uroot -pZKyqhRghv0/Y
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type help; or \h for help. Type \c to clear the current input statement.mysql set password password(12345678);
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql quit
Bye
[rootslave ~]#配置从库slave模式
查看master1主机上的master状态
//认vip为主192.168.195.100
[rootslave ~]# mysql -uroot -p12345678
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.39 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type help; or \h for help. Type \c to clear the current input statement.mysql change master to master_host192.168.195.100,- master_userftx,- master_password12345678,- master_log_filemysql_bin.000004,- master_log_pos154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)mysql start slave; //启动主从复制
Query OK, 0 rows affected (0.00 sec)//查看从数据库的状态
mysql show slave status\G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.195.100Master_User: ftxMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql_bin.000004Read_Master_Log_Pos: 154Relay_Log_File: mysql_relay_bin.000002Relay_Log_Pos: 320Relay_Master_Log_File: mysql_bin.000004Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 527Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 10Master_UUID: 11b3dfb4-6b5c-11ee-9bf0-000c298b9bafMaster_Info_File: /opt/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)mysql8.测试
1.在高可用集群中当master1主机为主时master2主机为备
//master1主机上的keepalived服务和mysql是启动的有vip
[rootmaster1 ~]# systemctl is-active keepalived.service
active
[rootmaster1 ~]# ss -antl | grep 3306
LISTEN 0 80 *:3306 *:*
[rootmaster1 ~]# ip a show ens160 | grep 192.168.195.100inet 192.168.195.100/32 scope global ens160
[rootmaster1 ~]# ip a show ens160 | grep 192.168.195.100 | wc -l
1//而此时master2主机上的则是keepalived服务开启mysql服务关闭没有vip
[rootmaster2 ~]# systemctl is-active keepalived.service
active
[rootmaster2 ~]# ss -antl | grep 3306
[rootmaster2 ~]# ss -antl | grep 3306 | wc -l
0
[rootmaster2 ~]# ip a show ens160 | grep 192.168.195.100
[rootmaster2 ~]# ip a show ens160 | grep 192.168.195.100 | wc -l
0//在master1主机上的mysql数据库中创建一个数据库然后查看从服务器是否同步
[rootmaster1 ~]# mysql -uroot -p12345678 -e create database ftx;
mysql: [Warning] Using a password on the command line interface can be insecure.
[rootmaster1 ~]# mysql -uroot -p12345678 -e show databases;
mysql: [Warning] Using a password on the command line interface can be insecure.
--------------------
| Database |
--------------------
| information_schema |
| ftx |
| mysql |
| performance_schema |
| sys |
--------------------
上述告警是告诉我们在命令行界面输入密码不安全此此实验我们不用考虑这个问题//前往slave主机上查看
[rootslave ~]# mysql -uroot -p12345678 -e show databases;
mysql: [Warning] Using a password on the command line interface can be insecure.
--------------------
| Database |
--------------------
| information_schema |
| ftx | //成功创建
| mysql |
| performance_schema |
| sys |
--------------------2.模拟master1主机发生故障导致服务宕机master1主机上的keepalived服务则会自动关闭释放资源vip则会跳转到master2主机上从而使得master2主机接替主数据库位置开启mysql服务
//手动关闭master1主机上的mysql服务从而使得keepalived服务自动关闭释放资源
[rootmaster1 ~]# service mysqld stop
Shutting down MySQL............ SUCCESS!
[rootmaster1 ~]# systemctl is-active keepalived.service
inactive
[rootmaster1 ~]# ss -antl | grep 3306
[rootmaster1 ~]# ss -antl | grep 3306 | wc -l
0
[rootmaster1 ~]# ip a show ens160 | grep 192.168.195.100
[rootmaster1 ~]# ip a show ens160 | grep 192.168.195.100 | wc -l
0//再次查看master2主机上mysql和keepalived服务状态以及vip位置
[rootmaster2 ~]# systemctl is-active keepalived.service
active
[rootmaster2 ~]# ss -antl | grep 3306
LISTEN 0 80 *:3306 *:*
[rootmaster2 ~]# ss -antl | grep 3306 | wc -l
1
[rootmaster2 ~]# ip a show ens160 | grep 192.168.195.100inet 192.168.195.100/32 scope global ens160
[rootmaster2 ~]# ip a show ens160 | grep 192.168.195.100 | wc -l
1//在master2主机上登录mysql数据库创建一个数据库查看从服务器是否同步
[rootmaster2 ~]# mysql -uroot -p12345678 -e create database yyr;
mysql: [Warning] Using a password on the command line interface can be insecure.
[rootmaster2 ~]# mysql -uroot -p12345678 -e show databases;
mysql: [Warning] Using a password on the command line interface can be insecure.
--------------------
| Database |
--------------------
| information_schema |
| ftx |
| mysql |
| performance_schema |
| sys |
| yyr |
--------------------//前往slave主机上查看
[rootslave ~]# mysql -uroot -p12345678 -e show databases;
mysql: [Warning] Using a password on the command line interface can be insecure.
--------------------
| Database |
--------------------
| information_schema |
| ftx |
| mysql |
| performance_schema |
| sys |
| yyr | //成功创建
--------------------部署完成