mysql集群
mysql双主+keepalived+haproxy
一、集群作用
实现高可用及负载均衡。
二、示例
1.实验环境
101 mysql01102 mysql01103 haproxy01+keepalived01104 haproxy02+keepalived02105 client2.各主机改名并关闭防火墙
101 mysql01102 mysql02103 haproxy01104 haproxy02105 clientsystemctl stop firewalldsetenforce 03.101,102安装mysql8.0(二进制编码包安装,解压即用)
tar zxf mysql-8.0.33-linux-glibc2.28-x86_64.tar -C /usr/local/ # 解压mysql到/usr/local
cd /usr/local # 切换到/usr/local下
mv mysql-8.0.33-linux-glibc2.28-x86_64 mysql # 重命名文件为mysql
useradd mysql # 创建运行用户mysql
cd /usr/local/mysql/bin # 切换到mysql的bin下
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ # 初始化,指定软件目录和数据目录,会生成随机密码。vim /etc/my.cnf # 编写mysql主配置文件
[mysqld]
basedir=/usr/local/mysql/ # 软件目录
datadir=/usr/local/mysql/data/ # 数据目录ln -s /usr/local/mysql/bin/* /usr/local/bin/ # 优化系统路径,做软连接(也可写变量)
cd /usr/local/mysql/support-files/ # 切换到mysql的支持文件
cp mysql.server /etc/init.d/mysqld # 复制启动脚本到系统服务,命名为 mysqldvim /etc/init.d/mysqld # 编写启动脚本
basedir=/usr/local/mysql/ # 增加内容
datadir=/usr/local/mysql/data/ # 增加内容chmod +x /etc/init.d/mysqld # 授予执行权限
chkconfig --add mysqld # 将mysqld添加到系统服务
service mysqld start # 启动mysqlmysql -uroot -p临时密码 # 输入临时密码登录# 修改root密码
mysql>alter user 'root'@'localhost' identified by 'pwd123'; # 创建用户张三,%代表所有允许连接的主机,密码为pwd123
mysql>create user 'zhangsan'@'%' identified by 'pwd123';# 将用户zhangsan的身份验证方式强制设置为mysql_native_password并更新密码为pwd123
mysql> alter user 'zhangsan'@'%' identified with mysql_native_password by 'pwd123';# 授予zhangsan用户在所有数据库中所有表上的全部权限。
mysql> grant all privileges on *.* to 'zhangsan'@'%';# 刷新权限。
mysql> flush privileges;101 vim /etc/my.cnf
[mysqld] # 配置文件主部分,表示mysql数据库的服务配置
basedir=/usr/local/mysql/ # 软件目录
datadir=/usr/local/mysql/data/ # 数据目录
log-bin=mysql-bin # 启用日志记录功能
server-id=1 # mysql实例的唯一id
log-slave-updates=true # 开启后,将从服务器的更新日志记录到主服务器的日志文件中。102 vim /etc/my.cnf
[mysqld] # 配置文件主部分,表示mysql数据库的服务配置
basedir=/usr/local/mysql/ # 软件目录
datadir=/usr/local/mysql/data/ # 数据目录
log-bin=mysql-bin # 启用日志记录功能
server-id=2 # mysql实例的唯一id
log-slave-updates=true # 开启后,将从服务器的更新日志记录到主服务器的日志文件中。mysql>show master status; # 查看binray log文件名和位置change master to master_host='192.168.10.102',master_user='zhangsan',master_password='pwd123',master_log_file='mysql-bin.000001',master_log_pos=155;
# 连接主,指定主的IP地址,主上已授权的用户及密码,主上的日志文件名及位置。
101和102互联,使用的用户为前面创建的用户zhangsan
mysql>show slave status\G # 查看主从同步状态,io线程和sql线程都为yes即成功。
#####################至此,mysql双主完成#######################################4.103、104安装haproxy,代理mysql服务,保护真实服务器地址。
yum -y install pcre-devel bzip2-devel gcc* # 安装haproxy的依赖包
tar zxf haproxy-1.5.19.tar.gz # 解压haproxy
cd haproxy-1.5.19/ # 进入haproxy
make TARGET=linux26 # 编译,使用uname -r可查看内核版本 使用more readme 可查看到内核版本对应的TARGET=
make install # 编译安装
mkidr /etc/haproxy # 创建haproxy配置文件目录
cp examples/haproxy.cfg /etc/haproxy/ # 复制配置文件
mkdir /usr/share/haproxy
vim /etc/haproxy/haproxy.cfg # 修改配置文件(无用的配置文件必须删除,否则haproxy会一个一个进行校对,这样就会导致启动失败)globallog 127.0.0.1 local0log 127.0.0.1 local1 notice # 日志级别为notice#log loghost local0 infomaxconn 4096
# chroot /usr/share/haproxy # haproxy的根目录,不注释启动会失败uid 99 # 运行haproxy的用户gid 99 # 运行haproxy的组daemon # 启动后以后台方式运行#debug#quietdefaultslog globalmode tcp # 模式为tcpoption httplogoption dontlognullretries 3redispatchmaxconn 2000 # 最大连接contimeout 5000 # 成功连接到一台服务器的最长等待时间 默认单位:毫秒clitimeout 50000 # 连接客户端发送数据时最长等待时间 默认单位:毫秒srvtimeout 50000 # 服务器端回应客户端数据发送的最长等待时间 默认单位毫秒frontend main *:3306 # 这里为实验方便,用3306端口default_backend mysql # 后端服务器组名 backend mysqlbalance leastconn # 使用最少连接方式调度server m1 192.168.10.101:3306 check port 3306 maxconn 300 server m2 192.168.10.102:3306 check port 3306 maxconn 300 # 指定mysql服务器ip及端口,最大连接数为300cp examples/haproxy.init /etc/init.d/haproxy # 复制进程文件到/etc/init.d
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy # 软连接方式做环境变量
chmod +x /etc/init.d/haproxy # 给haproxy.init执行权限
/etc/init.d/haproxy start # 启动haproxy
Reloading systemd: [ 确定 ]
Starting haproxy (via systemctl): [ 确定 ]
# 成功启动显示的内容
service haproxy start # 启动haproxy
netstat -tnlp | grep haproxy # 查看状态
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 60805/haproxy
tcp 0 0 0.0.0.0:6677 0.0.0.0:* LISTEN 60805/haproxy # 状态信息101上创建测试用的账户并授权,因为101和102互为主从,所以在一台上敲即可。# 创建用户李四,%代表所有允许连接的主机,密码为pwd123
mysql>create user 'lisi'@'%' identified by 'pwd123';# 将用户lisi的身份验证方式强制设置为mysql_native_password并更新密码为pwd123
mysql> alter user 'zhangsan'@'%' identified with mysql_native_password by 'pwd123';# 授予lisi用户在所有数据库中所有表上的全部权限。
mysql> grant all privileges on *.* to 'zhangsan'@'%';
客户端访问
mysql -ulisi -p123456 -h192.168.10.103 # 访问测试
mysql -ulisi -p123456 -h192.168.10.104 # 访问测试
mysql> # 访问成功会登入mysql
#####################proxy完成!!!#######################################5.103、104安装keepalived,实现高可用。
systemctl stop NetworkManager # 关闭网络管理功能,防止干扰实验
yum -y install keepalived ipvsadm # 安装keepalived和ipvsadm
systemctl enable keepalived # 设置开机自启动vim /etc/keepalived/keepalived.conf # 配置keepalived,Vrrp_strict 此项必须注释掉,否则严格遵守vrrp协议,会导致无法访问vip。
103上配置文件内容
global_defs {
notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_01
}vrrp_script chk_haproxy {script "/etc/keepalived/chk.sh" ##检查haproxy的脚本interval 2 ##每两秒检查一次
}vrrp_instance VI_1 {state BACKUP ##定义为BACKUP节点nopreempt ##开启不抢占interface ens160virtual_router_id 51priority 100 ##开启了不抢占,所以此处优先级必须高于另一台advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.10.100 ##配置VIP}track_script {chk_haproxy ##调用检查脚本}notify_backup "/etc/init.d/haproxy restart"notify_fault "/etc/init.d/haproxy stop"
}104上配置文件内容
global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_02
}vrrp_script chk_haproxy {script "/etc/keepalived/chk.sh"interval 2
}vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 99advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.10.100}track_script {chk_haproxy}notify_backup "/etc/init.d/haproxy restart"notify_fault "/etc/init.d/haproxy stop"
}vim /etc/keepalived/chk.sh # 创建脚本
#!/bin/bash
#
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; thensystemctl restart keepalived
fichmod +x /etc/keepalived/chk.sh # 给予权限
service keepalived start # 开启keepalivedip add # 查看vipinet 192.168.10.103/24 brd 192.168.10.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 192.168.10.100/32 scope global ens160
##因为两台主机均配置为BACKUP,因此哪台先运行keepalived,VIP就在哪台上,此时在103上。
mysql -ulisi -p123456 -h192.168.10.100 # 连接测试,会登入mysql。
CREATE DATABASE ooos; # 101、102都可以看到
| Database |
+--------------------+
| information_schema |
| mysql |
| ooos |
| performance_schema |
| test | # 101、102显示结果关闭103的haproxy,keepalived会随脚本自动关闭,此时104拿着vip,检测mysql的连接,发现没有中断。