实训day28

 [root@nat ~]# ipvsadm -d -t 192.168.10.101:3306 -r 10.0.0.22:3306        #删除真实主机
 nat:
 [root@nat ~]# ifconfig
 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 10.0.0.10  netmask 255.255.255.0  broadcast 10.0.0.255
 ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 192.168.10.101  netmask 255.255.255.0  broadcast 192.168.10.255
 [root@nat ~]# yum -y install ipvsadm.x86_64
 [root@nat ~]# ipvsadm -A -t 192.168.10.101:3306 -s rr
 [root@nat ~]# ipvsadm -a -t 192.168.10.101:3306 -r 10.0.0.21:3306 -m
 [root@nat ~]# ipvsadm -a -t 192.168.10.101:3306 -r 10.0.0.22:3306 -m
 [root@nat ~]# 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.10.101:3306 rr
   -> 10.0.0.21:3306               Masq    1      0          0         
   -> 10.0.0.22:3306               Masq    1      0          0  
 [root@nat ~]# vim /etc/sysctl.conf
 [root@nat ~]# net.ipv4.ip_forward=1
 sysctl -p
 mysql:
 [root@master-mysql ~]# route del default
 [root@master-mysql ~]# route add default gw 10.0.0.10
 [root@mysql-slave ~]# route del default
 [root@mysql-slave ~]# route add default gw 10.0.0.10
2、同步时间
 [root@haproxy ~]# yum -y install ntpdate.x86_64
 [root@haproxy ~]# ntpdate cn.ntp.org.cn
 14 Aug 15:48:21 ntpdate[1858]: adjust time server 203.107.6.88 offset -0.000690 sec
 [root@haproxy ~]# date
 2024年 08月 14日 星期三 15:48:30 CST
 [root@haproxy ~]# yum -y install ntp.x86_64 
 [root@haproxy ~]# vim /etc/ntp.conf
 15行加入   restrict 10.0.0.0 mask 255.255.255.0   #允许10.0.0.0网段的主机访问
 [root@haproxy ~]# systemctl start ntpd
 [root@web01 ~]# yum -y install ntpdate.x86_64
 [root@web01 ~]# ntpdate 10.0.0.35
 14 Aug 15:58:19 ntpdate[1654]: adjust time server 10.0.0.35 offset -0.003274 sec
 [root@web02 ~]# yum -y install ntpdate.x86_64
 [root@web02 ~]# ntpdate 10.0.0.35
 14 Aug 15:58:19 ntpdate[1654]: adjust time server 10.0.0.35 offset -0.003274 sec
3、web haproxy高可用代理
(1)打开web01和web02

 [root@web01 ~]#nginx
 [root@web02 ~]#nginx
(2)下载安装haproxy

 [root@haproxy ~]# yum -y install haproxy
 [root@haproxy ~]# rpm -ql haproxy
(3)编辑haproxy配置文件,实现代理负载均衡

 [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
 43行 http   #表示代理http服务
 63行 5000改80  #将端口号5000改为80
 #67行 use_backend   #没有配置静态服务器,直接注释
 68行 app换web  #将默认app模块改为web
 87行 backend web   #编写web模块
      balance   roundrobin
      server weba 10.1.1.200:80 check
      server webb 10.1.1.201:80 check
 [root@haproxy ~]# systemctl status haproxy
 [root@haproxy ~]# systemctl start haproxy
 [root@haproxy ~]# systemctl enable haproxy
 [root@haproxy ~]# curl 10.1.1.30
 web01
 [root@haproxy ~]# curl 10.1.1.30
 web02
(4)编辑haproxy配置文件,添加统计页面

 [root@haproxy ~]# vim   /etc/haproxy/haproxy.cfg
 # 配置文件最后追加
 listen statistics
         bind *:9090                     #定义监听端口
         mode http                       #默认使用协议
         stats enable                    #启用stats
         stats uri /hadmin?stats         #自定义统计页面的url
         stats auth admin:admin           #统计页面的账号密码
         stats hide-version              #隐藏在统计页面上的haproxy版本信息
         stats refresh 30s               #统计页面自动刷新时间
         stats admin if TRUE             #如果认证通过就做管理功能,可以管理后端服务器
         stats realm hapadmin            #统计页面密码框上提示文件,默认为haproxy\statistic
 [root@haproxy ~]# systemctl restart haproxy.service 
 [root@haproxy ~]# netstat -lntup | grep haproxy
 tcp        0      0 0.0.0.0:9090            0.0.0.0:*               LISTEN      1665/haproxy        
 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1665/haproxy        
 udp        0      0 0.0.0.0:37365           0.0.0.0:*                           1664/haproxy 


(5)编辑haproxy配置文件,添加权重功能

 [root@haproxy ~]# vim   /etc/haproxy/haproxy.cfg
 backend web
     balance     static-rr
     server  web01 10.0.0.11:80 weight 3 check
     server  web02 10.0.0.12:80 weight 1 check
 [root@haproxy ~]# systemctl restart haproxy
4、mysql haproxy高可用代理
(1)打开数据库

 [root@master-mysql ~]# service mysql8 start
 [root@mysql-slave ~]#  service mysql8 start
(2)编辑haproxy配置文件,实现代理负载均衡

 [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
 43行    mode                    tcp
 67行#   use_backend static          if url_static
 68行    default_backend             mysql
 86行 backend mysql
          balance     static-rr
          server  mysql01 10.0.0.21:3306 weight 3 check
          server  mysql02 10.0.0.22:3306 weight 1 check
 [root@haproxy ~]# systemctl restart haproxy
 [root@haproxy ~]# netstat -lntup | grep haproxy
 tcp        0      0 0.0.0.0:9090            0.0.0.0:*               LISTEN      2045/haproxy        
 tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2045/haproxy        
 udp        0      0 0.0.0.0:54557           0.0.0.0:*                           2044/haproxy 
 [root@mysql-slave ~]# mysql -u10.0.0.35 -p'123'
 mysql> show variables like 'server_id';
 +---------------+-------+
 | Variable_name | Value |
 +---------------+-------+
 | server_id     | 10    |
 +---------------+-------+
 1 row in set (0.09 sec)
 [root@mysql-slave ~]# mysql -u10.0.0.35 -p'123'
 mysql> show variables like 'server_id';
 +---------------+-------+
 | Variable_name | Value |
 +---------------+-------+
 | server_id     | 11    |
 +---------------+-------+
 1 row in set (0.09 sec)
下午
 mysql> help change master to   #查看连接master的帮助信息
1、python代码实现mysql的读写分离
(1)安装pymysql是python管理mysql的驱动,或者成为连接器
 [root@python ~]# pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
 [root@python ~]# pip3 install pymysql   #安装pymysql
(2)在python3的命令行界面引入pymysql
 [root@python ~]# mysql3
 >>> import pymysql
(3)创建两个connenction对象,一个指向master mysql,一个指向slave mysql
 master_conn=pymysql.connect(host="10.0.0.21",user="li",port=3306,database="test",password="li")
 slave_conn=pymysql.connect(host="10.0.0.22",user="li",port=3306,database="test",password="li")
(4)获取数据游标
 >>> master_cursor=master_conn.cursor()
(5)执行查询
 >>> select_sql="select * from student"
 >>> master_cursor.execute(select_sql)
 3
 >>> master_cursor.fetchall()
 ((1001, '孙颖莎', '云计算2班', '1002'), (1002, '大头', '云计算2班', '1003'), (1003, '小胖', '云计算2班', '1004'))


(6)执行修改
 >>> update_sql="update student set name='马龙' where id=1002"
 >>> master_cursor.execute(update_sql)
 1
 >>> master_conn.commit()


(7)执行删除
 >>> delete_sql="delete from student where name='小胖'"
 >>> master_cursor.execute(delete_sql);
 1
 >>> master_conn.commit()


(8)执行新增
 >>> insert_sql="insert into student values(1004,'张翔','云计算2班','1004')"
 >>> master_cursor.execute(insert_sql)
 1
 >>> master_conn.commit()


(9)执行查询(slave)
 >>> slave_cursor=slave_conn.cursor()
 >>> sql="select * from student"
 >>> slave_cursor.execute(sql)
 3
 >>> slave_cursor.fetchall()
 ((1001, '孙颖莎', '云计算2班', '1002'), (1002, '马龙', '云计算2班', '1003'), (1004, '张翔', '云计算2班', '1004'))


2、python脚本实现mysql的读写分离
 [root@python ~]# vim rwpython.py
 class rwsplit(object):
         def __init__ (self):
                 print("initialize")
         def master_statment(self,sql):
                 pass
         def slave_statment(self,sql):
                 pass
 ​
 ​
 if __name__ == "__main__":
         demo=rwsplit()
         sql=input("sign sql:")
         if sql[:6]=="select":
                 demo.slave_statment(sql)
         else:
                 demo.master_statment(sql)
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C语言万年历的实训报告,供您参考: 一、实训目的 本次实训的主要目的是通过编写一个简单的C语言万年历程序,巩固C语言基本语法和编程思想,提高编程能力和解决问题的能力。 二、实训要求 1、能够使用C语言基本语法和控制语句编写程序。 2、能够使用数组来存储和操作数据。 3、能够使用函数来实现模块化编程。 4、能够使用指针来操作内存中的数据。 5、能够使用文件来读取和写入数据。 三、实训内容 1、程序功能 本程序实现了一个简单的万年历功能,可以通过输入年份和月份来显示该月的日历。 2、程序实现 以下是本程序的主要代码实现: ```c #include <stdio.h> // 判断是否为闰年 int is_leap_year(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return 1; } else { return 0; } } // 获取某个月份的总天数 int get_days(int year, int month) { int days[] = {31, 28 + is_leap_year(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; return days[month - 1]; } // 获取某个日期是星期几 int get_weekday(int year, int month, int day) { if (month == 1 || month == 2) { year--; month += 12; } int c = year / 100; int y = year % 100; int w = y + y / 4 + c / 4 - 2 * c + 26 * (month + 1) / 10 + day - 1; w = (w % 7 + 7) % 7; return w; } // 显示日历 void show_calendar(int year, int month) { int days = get_days(year, month); int weekday = get_weekday(year, month, 1); printf(" 日 一 二 三 四 五 六\n"); int i; for (i = 0; i < weekday; i++) { printf(" "); } for (i = 1; i <= days; i++) { printf("%2d ", i); if ((weekday + i) % 7 == 0) { printf("\n"); } } if ((weekday + days) % 7 != 0) { printf("\n"); } } int main() { int year, month; printf("请输入年份:"); scanf("%d", &year); printf("请输入月份:"); scanf("%d", &month); if (month < 1 || month > 12) { printf("月份输入错误!\n"); return 1; } printf(" %d年%d月\n", year, month); show_calendar(year, month); return 0; } ``` 四、实训总结 通过本次实训,我学会了如何使用C语言来编写一个简单的万年历程序,巩固了C语言基本语法和编程思想,加强了对函数、数组、指针、文件等概念和用法的理解,提高了编程能力和解决问题的能力。同时,我也意识到在编程过程中需要注重代码的规范、可读性和可维护性,这对于日后的开发工作非常重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值