haproxy负载均衡
[root@haproxy01 ~]# yum -y install ntpdate
[root@haproxy01 ~]# yum -y install ntp
[root@haproxy01 ~]# systemctl start ntpd
安装haproxy
[root@haproxy01 ~]# yum -y install ntpdate
配置文件的地址
[root@haproxy01 ~]# rpm -ql haproxy
[root@haproxy01 ~]# vim /etc/haproxy/haproxy.cfg
frontend main *:80 //修改63行为80
# use_backend static if url_static //注释掉这一行
default_backend web //修改68行为web
backend web //在最后添加这几行
balance roundrobin
server weba 192.168.2.200:80 check
server webb 192.168.2.201:80 check
[root@haproxy01 ~]# systemctl start haproxy
[root@haproxy01 ~]# systemctl enable haproxy.service
访问haproxy自己,对web01,web02实现了轮询负载均衡
[root@haproxy01 ~]# curl 192.168.2.11
web---------01
[root@haproxy01 ~]# curl 192.168.2.11
web----------02
[root@haproxy01 ~]# vim /etc/haproxy/haproxy.cfg
# 定义web管理界面
listen statistics
bind *:9090 #定义监听端口
mode http #默认使用协议
stats enable #启用stats
stats uri /hadmin?stats #自定义统计页面的url
stats auth admin:admin #统计页面的账号密码
stats hide-version #隐藏在统计页面上的
stats refresh 30s #统计页面自动刷新时间
stats admin if TRUE #如果认证通过就做管理
stats realm hapadmin #统计页面密码框上提示
[root@haproxy01 ~]# systemctl restart haproxy
在浏览器访问192.168.2.11:9090/hsdmin?stats
加权轮询
[root@haproxy01 ~]# vim /etc/haproxy/haproxy.cfg
backend web
balance static-rr
server weba 192.168.2.200:80 weight 8 check
server webb 192.168.2.201:80 weight 2 check
[root@haproxy01 ~]# systemctl restart haproxy
Curl 192.168.2.11 //结果是web01出现的次数多于web02
[root@haproxy01 ~]# curl 192.168.2.11
web---------01
[root@haproxy01 ~]# curl 192.168.2.11
web---------01
[root@haproxy01 ~]# curl 192.168.2.11
web----------02
[root@haproxy01 ~]# curl 192.168.2.11
web---------01
[root@haproxy01 ~]# curl 192.168.2.11
web---------01
[root@haproxy01 ~]# curl 192.168.2.11
web---------01
mysql读写分离
- 在生产中,查询和修改的比例大概为7:3,查询压力大,可以分出多的主机做查询,slave也可以被查询,所有,可以将mysql做高可用主从复制
- 用户发送请求服务器响应压力(nginx,lvs,haproxy),但是web服务器需要提供服务,需要从数据库读数据,随着业务量并发量提高,单点mysql已经无法满足需求,所以要配置1主1从,一主多从
- 对数据库的从服务器是不允许修改,否则M-S失效
- 读写分离
- 代码级读写分离,中间件读写分离
mysql的主从复制
1. 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. 配置文件[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/db01-
master.err
log-bin=/usr/local/mysql/data/binlog
server-id=10
character_set_server=utf8mb4
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';
2. slave
1. rm -rf /etc/my.cnf
2. glibc,下载解压
3. 将解压后的文件移动的指定的/usr/local/mysql4. 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. 配置文件
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3310
log
error=/usr/local/mysql/data/mysql.log
relay
log=/usr/local/mysql/data/relaylog
server-id=11
character_set_server=utf8mb4
9.cp /usr/local/mysql/suport-files/msyql.server
/etc/init.d/mysql8
3. 同步数据
1. yum -y install rsync
2. service mysql8 stop
3. master=> rm -rf /usrlocal/mysql/data/auto.cnf4. rsync -av /usr/local/mysql/data
root@slaveip:/usr/loca/mysql
5. salve=>service mysql8 start
6. master=>service msyql8 start
4. 设置主数据库
1. 创建远程slave账号
create user 'slave'@'%' identified by
'slave';
grant replication slave on *.* to
'slave'%'%';
flush privileges;
2. flush tables with read lock;
3. show master status\G;
1. 文件名称
2. 文件位置
5. 设置从数据库 help change master to
1. change master to change master to
MASTER_HOST = '10.1.1.11' ,
MASTER_USER = 'slave' ,
MASTER_PASSWORD = 'slave' ,
MASTER_PORT = 3306,
MASTER_LOG_FILE = 'binlog000006' ,
MASTER_LOG_POS = 873,
GET_MASTER_PUBLIC_KEY = 1;
2. 启动slave并且查看状态
start slave;
show slave status\G
3. master => unlock tables;
python代码的读写分离
1.安装pymysql 是python管理mysql的驱动,或者成为
连接器
pip3 install pymysql
2.在python3的命令行界面引入pymysql
import pymysql3.创建两个connenction对象,一个指向master
mysql,一个指向slave msyql
master_conn=pymysql.connect(host="10.1.1.11"
,user="zhangmin",password="zhangmin",port=33
06,database="test");
slave_conn=pymysql.connect(host="10.1.1.12",
user="zhangmin",password="zhangmin",port=331
0,database="test");
4. 获取数据游标 master
master_cursor=master_conn.cursor()
5. 执行查询 master
select_sql="select * from user";
master_cursor.execute(select_sql);
rs=cursor.fetchall()
6. 执行修改 master
update_sql="update user set
password='000' where username='aaaa'"
master_cursor.execute(update_sql)
master_conn.commit()
7. 执行删除 masterdelete_sql="delete from user where
username='aaaa'"
master_cursor.execute(delete_sql)
master_conn.commit()
8. 执行新增 master
insert_sql="insert into user values
(1004, 'dddddd' , 'ddddddd')"
master_cursor.execute(insert_sql);
master_conn.commit()
9. 执行查询 slave
>>> # 执行查询 获得获得slave 游标
...
>>> slave_cursor=slave_conn.cursor()
>>> sql
'select * from user'
>>> slave_cursor.execute(sql)
3
>>> slave_cursor.fetchall()
((2, 'bbb' , 'bbbb'), (3, 'ccc' , 'cccc'),
(1004, 'ddddd' , 'ddddddd'))