一、haproxy----高可用、负载均衡
1.安装
安装ntpdate
[root@haproxy ~]# yum -y install ntpdate.x86_64
安装ntp
[root@haproxy ~]# yum -y install ntp
同步时间
[root@haproxy ~]# ntpdate cn.ntp.org.cn
启动ntp服务
[root@haproxy ~]# systemctl start ntpd
设置开机自启
[root@haproxy ~]# systemctl enable ntpd
安装haproxy
[root@haproxy ~]# yum -y install haproxy18.x86_64
2.配置
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
3.重启
设置开机启动
[root@haproxy ~]# systemctl restart haproxy
[root@haproxy ~]# systemctl enable haproxy
注:web01 ,web02开启nginx服务,然后测试
4.测试
[root@haproxy ~]# curl 192.168.1.245
web========01
[root@haproxy ~]# curl 192.168.1.245
web========02
[root@haproxy ~]# curl 192.168.1.245
web========01
[root@haproxy ~]# curl 192.168.1.245
web========02
5.添加统计页面
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
重启服务
[root@haproxy ~]#systemctl restart haproxy18.service
浏览器访问:
192.168.1.245:9090/hadmin?stats)
6.添加权重
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
重启服务
[root@haproxy ~]#systemctl restart haproxy18.service
浏览器访问:
刷新发现:web01出现次数比web02多
二、基于代码层级的读写分离
1.mysql的主从复制
master
1. rm -rf /etc/my.cnf
2. glibc,下载解压
3. 将解压后的文件移动的指定的/usr/local/mysql
4. mkdir /usr/local/mysql/mysql-files
5. useradd -r -s /sbin/nologin mysql
6. chown mysql:mysql /usr/local/mysql/mysql-files
7. chmod 750 /usr/local/mysql/mysql-files
8. /usr/local/mysql/bin/mysqld --initialize -- user=mysql --basedir=/usr/local/mysql/
9. 查看data目录和初始密码
10. /usr/local/mysql/bin/mysql_ssl_rsa_setup -- datadir=/usr/local/mysql/data
11. 配置文件
12.cp /usr/local/mysql/suport-files/msyql.server /etc/init.d/mysql8
13.service mysql8 start
14.sed -i '$aexport PATH=$PATH:/usr/local/mysql/bin' /etc/profile
15.source /etc/profile
16.mysql -h10.1.1.11 -P3306 -uzhangmin - pzhangmin
17.create user 'aaaa'%'aaaa' identified by 'sn'
18.grant all on . to 'aaaa';
slave
1. rm -rf /etc/my.cnf
2. glibc,下载解压
3. 将解压后的文件移动的指定的/usr/local/mysql
4. mkdir /usr/local/mysql/mysql-files
5. useradd -r -s /sbin/nologin mysql
6. chown mysql:mysql /usr/local/mysql/mysql-files
7. chmod 750 /usr/local/mysql/mysql-files
8. 配置文件
9.cp /usr/local/mysql/suport-files/msyql.server /etc/init.d/mysql8
2.同步数据
1. yum -y install rsync
2. service mysql8 stop
3. master=> rm -rf /usrlocal/mysql/data/auto.cnf
4. rsync -av /usr/local/mysql/data root@slaveip:/usr/loca/mysql
5. salve=>service mysql8 start
6. master=>service msyql8 start
3.设置主数据库
1. 创建远程slave账号
2. flush tables with read lock;
3. show master status\G;
文件名称
文件位置
4.设置从数据库
help change master to
1. change master to
2. 启动slave并且查看状态
3. master => unlock tables;
5.python代码的读写分离
1.安装pymysql 是python管理mysql的驱动,或者成为 连接器
pip3 install pymysql
2.在python3的命令行界面引入pymysql
import pymysql
3.创建两个connenction对象,一个指向master mysql,一个指向slave msyql
master_conn=pymysql.connect(host="10.1.1.11",user="dd",password="1",port=33 06,database="test");
slave_conn=pymysql.connect(host="10.1.1.12",user="dd",password="1",port=331 0,database="test");
4. 获取数据游标 master
master_cursor=master_conn.cursor()
5. 执行查询 master
6. 执行修改 master
7. 执行删除 master
8. 执行新增 master
9. 执行查询 slave
标签:haproxy,python,local,28,master,usr,mysql,root From: https://blog.csdn.net/m0_73907608/article/details/141198506