一、用keepalived实现nginx与lvs的高可用集群
keepalived+nginx高可用
环境:
node1: 192.168.31.233 keepalived+nginx master
node2: 192.168.31.72 keepalived+nginx slave
node3: 192.168.31.233 web1
node4: 192.168.31.26 web2
VIP: 192.168.31.100
1、node1,node2安装 keepalived nginx软件包
yum -y install nginx keepalived
2、node3,node4安装httpd服务,并配置网页
node3
yum -y install httpd
echo server3 >/var/www/html/index.html
systemctl start httpd.service
访问测试
node4
yum -y install httpd
echo server4 >/var/www/html/index.html
systemctl start httpd.service
访问测试
3、配置nginx负载均衡 <node1>
在http段内加入
upstream webserver {
server 192.168.31.26;
server 192.168.31.23;
}
在server段location内加入
proxy_pass http://webserver;
proxy_set_header X-Real-IP $remote_addr;
4、配置keepalived <node1>
global_defs {
notification_email {
root@localhost.com
}
notification_email_from admin@localhost.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #主服务器
interface ens33
virtual_router_id 51
priority 100 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.31.100 #虚拟VIP,客户段访问的IP
}
}
5、拷贝nginx和keepalived的配置文件到node2服务器上,并修改keepalived的配置
scp /etc/nginx/nginx.conf 192.168.31.72:/etc/nginx/
scp /etc/keepalived/keepalived.conf 192.168.31.72:/etc/keepalived/
vim /etc/keepalived/keepalived.conf
state BACKUP #改为从服务器
6、启动nginx、keepalived服务,并查看80端口是否起来
nginx
systemctl start keepalived.service
7、测试
http://192.168.31.100 通过访问VIP能浏览到后端的网页
可以看到VIP绑定到node1的网卡上
模拟node1节点故障,可以看到VIP转移到node2节点上,访问后端网页正常(省略…)
keepalived+lvs高可用
环境:
node1: 192.168.31.233 keepalived+lvs-DR master
node2: 192.168.31.72 keepalived+lvs-DR slave
node3: 192.168.31.233 web1
node4: 192.168.31.26 web2
VIP: 192.168.31.100
client: 192.168.31.172
1、node1,node2安装keepalived,ipvsadm软件包
yum -y install keepalived ipvsadm
2、修改node1上的keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost.com
}
notification_email_from admin@localhost.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #主服务器
interface ens33
virtual_router_id 51
priority 100 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.31.100
}
}
virtual_server 192.168.31.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
sorry_server 127.0.0.1 80
persistence_timeout 50
protocol TCP
real_server 192.168.31.26 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.31.23 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
3、修改node2上的keepalived配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost.com
}
notification_email_from admin@localhost.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP #从服务器
interface ens33
virtual_router_id 51
priority 90 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.31.100
}
}
virtual_server 192.168.31.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
sorry_server 127.0.0.1 80
persistence_timeout 50
protocol TCP
real_server 192.168.31.26 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.31.23 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
4、启动node1,node2上的keepalived
systemctl start keepalived
VIP绑定到node1主服务器上
5、node3,node4安装httpd,并创建启动LVS-DR模式脚本
yum -y install httpd
vim realserver.sh
#!/bin/bash
VIP=192.168.31.100
. /etc/rc.d/init.d/functions
case “$1” in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo “1” >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “2” >/proc/sys/net/ipv4/conf/lo/arp_announce
echo “1” >/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2” >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo “RealServer Start OK”
;;
stop)
ifconfig lo:0 down
route del $VIP >/dev/null 2>&1
echo “0” >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “0” >/proc/sys/net/ipv4/conf/lo/arp_announce
echo “0” >/proc/sys/net/ipv4/conf/all/arp_ignore
echo “0” >/proc/sys/net/ipv4/conf/all/arp_announce
echo “RealServer Stoped”
;;
*)
echo “Usage: $0 {start|stop}”
exit 1
esac
exit 0
bash realserver.sh start
6、客户端访问测试
for i in `seq 100`;do curl 192.168.31.100;sleep 1;done
<h1>RS3</h1>
<h1>RS4</h1>
<h1>RS3</h1>
<h1>RS4</h1>
<h1>RS3</h1>
<h1>RS4</h1>
###主服务器宕机时,备机可正常提供服务,访问正常###
for i in `seq 100`;do curl 192.168.31.100;sleep 1;done
<h1>RS3</h1>
<h1>RS4</h1>
<h1>RS3</h1>
<h1>RS4</h1>
<h1>RS3</h1>
<h1>RS4</h1>
二、授权test用户通过任意主机连接当前mysql,但每秒中最大查询次数不得超过5次;此账户的同时连接次数不得超过3次
GRANT USAGE ON *.* TO test@’%’ WITH MAX_QUERIES_PER_HOUR 5;
GRANT USAGE ON *.* TO test@’%’ WITH MAX_USER_CONNECTIONS 3;
三、导入hellodb.sql生成数据库,完成以下练习:
(1)在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄;
+————–+—–+
| Name | Age |
+————–+—–+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
+————–+—–+
(2)以ClassID为分组依据,显示每组的平均年龄;
> SELECT ClassID,avg(age) FROM students GROUP BY ClassID;
+———+———-+
| ClassID | avg(age) |
+———+———-+
| NULL | 63.5000 |
| 1 | 20.5000 |
| 2 | 36.0000 |
| 3 | 20.2500 |
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+———+———-+
(3) 显示第2题中平均年龄大于30的分组及平均年龄;
> SELECT ClassID,avg(age) FROM students GROUP BY ClassID HAVING avg(age)>30;
+———+———-+
| ClassID | avg(age) |
+———+———-+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+———+———-+
(4) 显示以L开头的名字的同学的信息;
> SELECT * FROM students WHERE Name LIKE ‘L%’;
+——-+————-+—–+——–+———+———–+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+——-+————-+—–+——–+———+———–+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
+——-+————-+—–+——–+———+———–+
(5) 显示TeacherID非空的同学的相关信息;
> SELECT * FROM students WHERE TeacherID IS NOT NULL;
+——-+————-+—–+——–+———+———–+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+——-+————-+—–+——–+———+———–+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
+——-+————-+—–+——–+———+———–+
(6) 以年龄排序后,显示年龄最大的前10位同学的信息;
> SELECT * FROM students ORDER BY Age DESC LIMIT 10;
+——-+————–+—–+——–+———+———–+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+——-+————–+—–+——–+———+———–+
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
+——-+————–+—–+——–+———+———–+
(7) 查询年龄大于等于20岁,小于等于25岁的同学的信息;用三种方法;
方法一:
> SELECT * FROM students WHERE Age>=20 AND Age<=25;
+——-+—————+—–+——–+———+———–+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+——-+—————+—–+——–+———+———–+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
+——-+—————+—–+——–+———+———–+
方法二:
> SELECT * FROM students WHERE Age BETWEEN 20 AND 25;
+——-+—————+—–+——–+———+———–+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+——-+—————+—–+——–+———+———–+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
+——-+—————+—–+——–+———+———–+
方法三:
> SELECT * FROM students WHERE Age IN (20,21,22,23,24,25);
+——-+—————+—–+——–+———+———–+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+——-+—————+—–+——–+———+———–+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
+——-+—————+—–+——–+———+———–+
(8) 显示前5位同学的姓名、课程及成绩;
> SELECT st.Name,co.Course,sc.Score FROM students st,courses co,scores sc WHERE st.StuID=sc.StuID and sc.CourseID=co.CourseID LIMIT 5;
+————-+—————-+——-+
| Name | Course | Score |
+————-+—————-+——-+
| Shi Zhongyu | Kuihua Baodian | 77 |
| Shi Zhongyu | Weituo Zhang | 93 |
| Shi Potian | Kuihua Baodian | 47 |
| Shi Potian | Daiyu Zanghua | 97 |
| Xie Yanke | Kuihua Baodian | 88 |
+————-+—————-+——-+
(9) 显示其成绩高于80的同学的名称及课程;
> SELECT st.Name,co.Course FROM students st,courses co,scores sc WHERE st.StuID=sc.StuID and sc.CourseID=co.CourseID AND sc.Score>80;
+————-+—————-+
| Name | Course |
+————-+—————-+
| Shi Zhongyu | Weituo Zhang |
| Shi Potian | Daiyu Zanghua |
| Xie Yanke | Kuihua Baodian |
| Ding Dian | Kuihua Baodian |
| Shi Qing | Hamo Gong |
| Xi Ren | Hamo Gong |
| Xi Ren | Dagou Bangfa |
| Lin Daiyu | Jinshe Jianfa |
+————-+—————-+
(10) 求前8位同学每位同学自己两门课的平均成绩,并按降序排列;
> SELECT st.Name,sc2.avg_score FROM (SELECT StuID,avg(Score) AS avg_score FROM scores GROUP BY StuID) AS sc2,students st where st.StuID=sc2.StuID ORDER BY avg_score DESC LIMIT 8;
+————-+———–+
| Name | avg_score |
+————-+———–+
| Shi Qing | 96.0000 |
| Shi Zhongyu | 85.0000 |
| Xi Ren | 84.5000 |
| Xie Yanke | 81.5000 |
| Ding Dian | 80.0000 |
| Lin Daiyu | 75.0000 |
| Shi Potian | 72.0000 |
| Yu Yutong | 51.0000 |
+————-+———–+
(11) 显示每门课程课程名称及学习了这门课的同学的个数;
> SELECT co2.Course,count(co2.StuID) FROM (SELECT co.Course,sc.StuID FROM courses co,scores sc WHERE co.CourseID=sc.CourseID) AS co2 GROUP BY co2.Course;
+—————-+——————+
| Course | count(co2.StuID) |
+—————-+——————+
| Dagou Bangfa | 2 |
| Daiyu Zanghua | 2 |
| Hamo Gong | 3 |
| Jinshe Jianfa | 1 |
| Kuihua Baodian | 4 |
| Taiji Quan | 1 |
| Weituo Zhang | 2 |
+—————-+—————–
原创文章,作者:N27_Vicent,如若转载,请注明出处:http://www.178linux.com/82057
评论列表(1条)
总体还不错。
做类似的实验,建议给出拓扑图和实验的效果会让阅读者更好的了解博文内容;继续加油~