LVS负载均衡MySQL
环境说明
类型 | IP地址 |
---|---|
RS1 | 192.168.236.129 |
dl | 192.168.236.130 |
RS2 | 192.168.236.133 |
Client | 192.168.236.135 |
//配置client端的IP地址
[root@localhost ~]# hostname -I
192.168.236.135
//配置dl端的DIP和VIP地址
[root@ld ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
BOOTPROTO=static
DEFROUTE=yes
NAME=ens160
UUID=105f2d68-7458-46e0-a24a-f7ab016bdb6d
DEVICE=ens160
IPADDR0=192.168.236.130
PREFIX0=24IPADDR1=192.168.236.200
PREFIX1=24
GATEWAY0=192.168.236.2
DNS1=114.114.114.114
//修改RS1和RS2的内核参数
[root@rs1 ~]# vim /etc/sysctl.conf #添加以下两行
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@rs1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@rs2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@rs2 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
//配置RS1上的RIP和VIP
[root@rs1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
BOOTPROTO=static
DEFROUTE=yes
NAME=ens160
UUID=244a1c69-a4f1-4575-a659-1e7921137b05
DEVICE=ens160
ONBOOT=yesIPADDR0=192.168.236.129
PREFIX0=24IPADDR1=192.168.236.200
PREFIX1=24
GATEWAY0=192.168.236.2
DNS1=114.114.114.114
//配置RS2上的RIP和VIP
[root@rs2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
TYPE=Ethernet
BOOTPROTO=ststic
DEFROUTE=yes
NAME=ens160
UUID=92415411-0007-4219-9e6c-4670d9352434
DEVICE=ens160
ONBOOT=yes
IPADDR0=192.168.236.133
PREFIX0=24
IPADDR1=192.168.236.200
PREFIX1=24
GATEWAY0=192.168.236.2
DNS1=114.114.114.114
//配置路由信息
[root@dl ~]# route add -host 192.168.236.200 dev lo
[root@rs1 ~]# route add -host 192.168.236.200 dev lo
[root@rs2 ~]# route add -host 192.168.236.200 dev lo
//在Director端添加并保存规则
[root@dl ~]# ipvsadm -A -t 192.168.236.200:3306 -s rr
[root@dl ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.236.200:3306 rr
[root@dl ~]# ipvsadm -a -t 192.168.236.200:3306 -r 192.168.236.133:3306 -g
[root@dl ~]# ipvsadm -a -t 192.168.236.200:3306 -r 192.168.236.129:3306 -g
[root@dl ~]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
//在RS1和RS2上安装MySQL并授权
[root@localhost ~]# yum -y install mariadb mariadb-common mariadb-devel mariadb-server
//启动 并设置开机自动启动
[root@RS1 ~]# systemctl enable --now mariadb
[root@RS1 ~]# systemctl status mariadb
//在RS2上安装MySQL,然后创建数据库并授权
[root@RS2 ~]# yum -y install mariadb mariadb-common mariadb-devel mariadb-server
[root@RS2 ~]# systemctl enable --now mariadb
//在RS1上创建数据库并授权
MariaDB [(none)]> create database RS1;
Query OK, 1 row affected (0.271 sec)
MariaDB [(none)]> grant all on *.* to ‘test’@'192.168.236.%' identified by '123456';
Query OK, 0 rows affected (0.317 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
//在RS2上创建并数据库并授权
[root@rs2 ~]# mysql -uroot //创建数据库并授权
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database rs2;
Query OK, 1 row affected (0.282 sec)
MariaDB [(none)]> grant all on *.* to 'test'@'192.168.236.%' identified by '123456';
Query OK, 0 rows affected (0.319 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.284 sec)
MariaDB [(none)]>
[root@localhost ~]# mysql -utest -p123456 -h192.168.236.200 -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| RS1 |
| information_schema |
| mysql |
| performance_schema |
+--------------------+