一, 大访问量


1,负载均衡


把访问流分到不同的服务器上。




2,冗余技术


一台服务器故障,另一台服务器备份,集群




二, 大数据存储


1,mysql


主从 (负载) , 分库分表(垂直,水平) 分区,集群(冗余备份)



2,oracle


3,db2


4,nosql



三, 网站加速


1,squid反向缓存-动静分离


2,页面静态化

(1)伪静态,(2)模板技术-页面静态化 (3)ajax


第二种方法才算真正意义的静态化


smarty,还有一些框架


3,memcache



4,sphinx全文检索





四,网站服务,流量监控


1,服务监控


(1)apache web服务监控

(2)mysql 数据库监控

(3)磁盘空间监控



2,流量监控

(1)网站流量监控



===================================

snmp


被监控者要开启snmp服务 161,162端口


监控者绘制图表

mrtg流量图

cacti监控(以网站的方式显示)



postfix邮件报警


linux邮件系统:

1,postfix发邮件系统 25

2,sendmail发邮件系统

3,dovecot 收邮件系统 110






管理员(流量监控+邮件报警)


(1) mrtg +shell


(2) cacti + php





五,压力测试


apache压力测试-ab


mysql压力测试-mysqlslap










============实际应用=====================================




一,网络负载均衡



1,基于网络设备 (硬件设备的负载均衡 )


cisco的以太网通道

F5的负载均衡 (net技术:网络目的地址转换)



2,基于操作系统

windows -> nlv

linux -> lvs


lvs常用的三种负载均衡模式:

(1)lvs nat模式 (网络地址转换)轮循访问

(2)lvs ip-tun模式 (ip隧道模式)

(3)lvs dr模式 (直接路由模式)




四种常用的轮叫算法


1,轮叫

2,加权轮叫 (哪来服务器处理能力强,请求多分一点)

3,最少链接 (哪个服务器链接数最少,请求发到哪来服务器)

4,加权最少链接








二,squid反向代理



普通代理:内部通过设置代理IP和端口访问外部

透明代理:内部不需要每次都设置代理ip和端口就能访问外部

反向代理:外部通过代理服务器访问内部的web服务器。




squid安装与配置


1,安装squid软件包

yum -y install squid*

2,squid配置文件

/etc/squid/squid.conf

3,启动squid服务

service squid start/restart

4,停止squid服务

service squid stop

5.重新加载配置文件

squid -k reconfig




配置文件squid.conf 常用配置项


http_port 192.168.10.1:3128 //代理ip及端口

cache_mem 64MB

maximum_object_size 4096KB

reply_body_max_size 10480000 allow all //下载最大文件限制

access_log /var/log/squid/access.log // 日志

visible_hostname 192.168.10.1

cache_dir ufs /var/spool/squid 100 16 256

大小 一级目录 二级目录






1.普通代理配置方法

demo:


虚拟机两个 clone一台

squid 300M内存 clone 100M内存

改下名字 squid-server web-server


squid-server 要两块网卡


启动两台虚拟server


vim1 192.168.10.2


ifconfig eth0 192.168.10.1

ifconfig eth0


ifconfig eth1 200,168.10.1


进入web-server

ifconfig eth0 200.168.10.2


测试

ping 192.168.10.2

ping 200.168.10.2



进入squid-server

rpm -qa squid* //看有没有安装squid

yum -y install squid* //安装

cd /etc/squid

ls

vi squid.conf

rm -rf squid.conf

vi squid.conf

http_port 192.168.10.1:3128

visible_bostname 192.168.10.1

acl innet src 192.168.10.0/24

acl all src 0.0.0.0/0.0.0.0

http_access allow innet //允许innet网段的人访问代理服务器

http_access deny all //拒绝。。。。。


wq:



service squid restart


pstree |grep squid

netstat -tunpl |grep :3128




进入web-server


rpm -qa |grep httpd

service httpd restart

ckhconfig httpd on

cd /var/www/html/

ls

echo "<h1>web-200.168.10.2</h1>" > index.html



设置代理



可以通过代理服务器上网








ACL访问控制: 可以从客户机的IP地址,请求的URL,文件类型,访问时间,并发请求数来进行控制


ACL控制列表:


定义acl列表:

acl 列表名称 列表类型 列表内容


针对acl列表进行限制

http_access allow 或deny



demo:


acl all src 0.0.0.0/0.0.0.0 所有的IP

http_access deny all 拒绝访问


src 源IP

dst 目的IP

port 端口

srcdomain 源域名

dstdomain 目的域名

time 时间

maxconn 最大连接数

url_regex url正则过滤

urlpath_regex urlpath正则过滤



acl列表定义示例 ======demo


acl LAN1 src 192.168.1.0/24

acl PC1 src 192.168.1.66/32

acl Blk_Domain dstdomain .qq.com .kaixin001.com

acl Work_Hours time MTWHF 08:30-17:30

acl Max20_Conn maxconn 20

acl Blk_URL url_regex -i ^rtsp:// ^mms://

acl Blk_Words urlpath_regex -i sex adult

acl RealFile urlpath_regex -i \.rmvb$ \.rm$


根据已定义的acl列表进行访问控制


http_access deny LAN1 Blk_URL //拒绝LAN1去Blk_URL

http_access deny LAN1 Blk_Words

http_access deny PC1 RealFile

http_access deny PC1 Max20_Conn

http_access allow LAN1 Work_Hours




2.配置squid透明代理


不用设置代理IP端口,只要设置网关就行。


前提:客户机的web访问数据要能经过防火墙

代理服务构建在网关主机中



修改squid.conf配置文件

http_port 192.168.10.1:2128 transparent

squid -k reconfig


iptables -t nat -I PREROUTING -i etho -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128





demo:

进入squid-server

vi squid.conf

修改

http_port 192.168.10.1:2128 transparent


重新加载配置项

squid -k reconfig



cd

iptables -t nat -A PREROUTING -i eth0 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128



iptables -L //查看






3,squid 反向代理




修改squid.conf


http_port 200,168,10,1:80 vhost

cache_peer 192.168.10.2 parent 80





修改vm1 为200.168.10.2


进入squid-server


ifconfig eth0 200.168.10.1

ifconfig eth1 192.168.10.1


进入web-server

ifconfig eth0 192.168.10.2



vi squid.conf


http_port 200.168.10.1:80 vhost

visible_hostname 200.168.10.1

acl all src 0.0.0.0/0.0.0.0

http_access allow all

cache_peer 192.168.10.2 parent 80 0 originserver

:wq



squid -k reconfig


OK......





=============================================================


mysql主从,


mysql分区。



针对海量数据的优化主要有2种方法:大表拆小表,sql语句的优化。索引



1.分库分表:


垂直分表,水平分表。

分表分导致要修改sql语句,会造成逻辑的复杂。使用分区技术可以避免。


2.分区技术: mysql5.1后所带的一种分区技术,它是在逻辑层进行水平分表。


(1)range分区:

基于一个给定连续区间的残值,把多行分配给分区


demo:

create table emplyees(


id int notnull,

fname varchar(30),

lname varchar(30),

hired date notnull default '1970-01-01',

separated date notnull default '9999-12-31',

job_code int not null,

store_id int not null


)


partition by range(store_id)(

partition p0 values less than(6) //小于6的在p0分区

partition p0 values less than(11) //小于11的在p1分区

partition p0 values less than(16)

partition p0 values less than(21)

);







(2)list分区:

类似于range分区,区别在于基于列值匹配一个离散值集合中的某个值来进行选择。


demo:

create table emplyees(


id int notnull,

fname varchar(30),

lname varchar(30),

hired date notnull default '1970-01-01',

separated date notnull default '9999-12-31',

job_code int not null,

store_id int not null


)


partition by list(store_id)(


partition pnorth values in(3,5,6,9,17), //在这个里面的,放在pnorth分区里,

partition peast values in(1,2,10,11,19,20),

partition pwest values in(4,12,13,14,18),

partition pCentral values in(7,8,15,16),


)








(3)hash分区:基于用户定义的表达式的返回值来进行选择分区。



用来确保数据在预先确定的数目的分区中平均分布



demo:

create table emplyees(


id int notnull,

fname varchar(30),

lname varchar(30),

hired date notnull default '1970-01-01',

separated date notnull default '9999-12-31',

job_code int not null,

store_id int not null


)

partition by hash (year(hired))

partitions 4;





(4)key分区


可以为字符型等其它非int类型,md5,ash


-------------------


实例操作:


create table t1(id int);


show create table t1;


cd /usr/local/mysql/data


show tables;


create table t2(id int) engine=myisam


partition by hash(id)

partitions 5;


show create table t2;


cd /usr/local/mysql/data


ll





demo:


/usr/local/mysql/bin/mysql -uroot -hlocalhost -p123456


show databases

show create table t2

?index

?create index


create index in_id on t2(id);

show index from t2;

desc t2;

insert into t2 values(1);

insert into t2 values(2);

insert into t2 values(3);

insert into t2 values(4);

insert into t2 values(5);

insert into t2 values(6);

insert into t2 values(7);

insert into t2 values(8);

insert into t2 values(9);

insert into t2 values(10);


insert into t2 select * from t2;

insert into t2 select * from t2;

insert into t2 select * from t2;

insert into t2 select * from t2;

insert into t2 select * from t2;

insert into t2 select * from t2;


select count(*) from t2;



R+F2 切换终端 监控

cd data

ls

ll -h

watch-n1 | ls -lh 时时跟踪命令结果



insert into t2 select * from t2; //不停插入直到1000W行。


select count(*) from t2;




R+f3 切换到第三个终端:


tree


free //内存的使用


free -m //以M来看内存


top //cpu使用情况





select count(*) from t2;


select count(*) from t2 where id > 5; //带上where耗时就长






create table t3(id int);



\d // //改变结束符


//创建存储

create procedure p3 ()

begin

set @i=1;

while @i<10000 do

insert into t3 valuses(@i);

set @i=@i+1;

end //


\d ;

show procedure status;


select count(*) from t3;

call p3();

select count(*) from t3;




show create procedure p3; //查看存储

?procedure;

drop procedure p3; //删除存储

show procedure status;

call p3;




==================================================



创建一张分区表

创建一张未分区表


写一个存储,插入几百万的数据









mysql 基础操作




1.mysql表复制


create table t3 like t1;

insert into t3 select * from t1;



2,mysql索引



create index 创建索引


demo:

desc ti;

create index in_name on t1(name);

show index from t1;

create unique index in_name on t1(name);


drop index 删除索引


demo:

drop index in_name on t1;

drop index in_name on t1;


alter table 创建删除索引,还可以操作主键索引,最好的方法


alter table t1 add index in_index(name);

alter table t1 drop index in_index;

alter table t1 drop primary key;

alter table t1 modify id int unsigned not null;

desc t1;

alter table t1 drop primary key;


alter table t1 add index in_index(name);

alter table t1 drop index in_name;

show index from t1;


alter table t1 add unique(name);

show index from t1;

alter table t1 drop name;



3,mysql 视图


通过一个条件从一张表拿出来的数据形成一张中间表,就是视图,而且会跟着主表时时更新。


? view

create view v_t1 as select * from t1 where id>4 and id<8;

show tables;

select *from v_t1;


drop view v_t1;


show create view v_t1;



4,mysql内置函数


字符串函数;

concate 连接字符串

lcase 转小写

ucase 转大写

length 长度

ltrim 去左空格

rtime 去右空格

repeat 重复count次

replace 替换

substring/substr 截取字符串

space(count) 生成count个空格



数学函数:

bin 十进制转二进制

ceiling 向上取整

floor 向下取整

max 取最大值

min 取最小值

sqrt 开平方

rand 取随机数



日期函数:


curdate

curtime

now

timestamp

week

year

datediff

unix_timestamp

from_unixtime




5,mysql预处理语句


prepare stmt1 from 'select * from t1 where id>?';

set @i=1;

execute stmt1 using @i;

drop prepare stmt1;


相当于 把@i 作为 ? 那部分执行

即 select * from t1 where id>1



6,mysql 事务处理



set autocommit=0; 自动提交改为0

delect from t1; 删除表数据

select * from t1;

rollback; 恢复

select * from t1;

commit; 提交


savepoint p1; 设置还原点、

rollback to p1; 还原到p1还原点、




7,mysql存储


存储就是要执行的代码段、


?procedure;


\d //

create procedure p1()

begin

set $i=3;

while @i<100 do

insert into t2(name) values(concat("user",@i));

set @i=@i+1;

end while

end //


all p1; //执行存储p1;

show procedure status\G

show create procedure p1;





8,mysql触发器


向一个表执行数据时,对另一个表也同时执行


例:向t1表插入数据,同时向t2表插入数据


truncate t1; 删除t1表的数据,从1开始


create trigger t1 before insert on t1 for each row

begin

insert into t2(name) values(new.name);

end//




9, 重排auto_increment值

delete from tablename 不能重排只有用


truncate table tablename;或

alter table tablename auto_increment=1;



10,常用的sql技巧