php特级学习笔记
一, 大訪問量
1,負(fù)載均衡
把訪問流分到不同的服務(wù)器上。
2,冗余技術(shù)
一臺(tái)服務(wù)器故障,另一臺(tái)服務(wù)器備份,集群
二, 大數(shù)據(jù)存儲(chǔ)
1,mysql
主從 (負(fù)載) , 分庫(kù)分表(垂直,水平) 分區(qū),集群(冗余備份)
2,oracle
3,db2
4,nosql
三, 網(wǎng)站加速
1,squid反向緩存-動(dòng)靜分離
2,頁(yè)面靜態(tài)化
(1)偽靜態(tài),(2)模板技術(shù)-頁(yè)面靜態(tài)化 (3)ajax
第二種方法才算真正意義的靜態(tài)化
smarty,還有一些框架
3,memcache
4,sphinx全文檢索
四,網(wǎng)站服務(wù),流量監(jiān)控
1,服務(wù)監(jiān)控
(1)apache web服務(wù)監(jiān)控
(2)mysql 數(shù)據(jù)庫(kù)監(jiān)控
(3)磁盤空間監(jiān)控
2,流量監(jiān)控
(1)網(wǎng)站流量監(jiān)控
===================================
snmp
被監(jiān)控者要開啟snmp服務(wù) 161,162端口
監(jiān)控者繪制圖表
mrtg流量圖
cacti監(jiān)控(以網(wǎng)站的方式顯示)
postfix郵件報(bào)警
linux郵件系統(tǒng):
1,postfix發(fā)郵件系統(tǒng) 25
2,sendmail發(fā)郵件系統(tǒng)
3,dovecot 收郵件系統(tǒng) 110
管理員(流量監(jiān)控+郵件報(bào)警)
(1) mrtg +shell
(2) cacti + php
五,壓力測(cè)試
apache壓力測(cè)試-ab
mysql壓力測(cè)試-mysqlslap
============實(shí)際應(yīng)用=====================================
一,網(wǎng)絡(luò)負(fù)載均衡
1,基于網(wǎng)絡(luò)設(shè)備 (硬件設(shè)備的負(fù)載均衡 )
cisco的以太網(wǎng)通道
F5的負(fù)載均衡 (net技術(shù):網(wǎng)絡(luò)目的地址轉(zhuǎn)換)
2,基于操作系統(tǒng)
windows -> nlv
linux -> lvs
lvs常用的三種負(fù)載均衡模式:
(1)lvs nat模式 (網(wǎng)絡(luò)地址轉(zhuǎn)換)輪循訪問
(2)lvs ip-tun模式 (ip隧道模式)
(3)lvs dr模式 (直接路由模式)
四種常用的輪叫算法
1,輪叫
2,加權(quán)輪叫 (哪來服務(wù)器處理能力強(qiáng),請(qǐng)求多分一點(diǎn))
3,最少鏈接 (哪個(gè)服務(wù)器鏈接數(shù)最少,請(qǐng)求發(fā)到哪來服務(wù)器)
4,加權(quán)最少鏈接
二,squid反向代理
普通代理:內(nèi)部通過設(shè)置代理IP和端口訪問外部
透明代理:內(nèi)部不需要每次都設(shè)置代理ip和端口就能訪問外部
反向代理:外部通過代理服務(wù)器訪問內(nèi)部的web服務(wù)器。
squid安裝與配置
1,安裝squid軟件包
yum -y install squid*
2,squid配置文件
/etc/squid/squid.conf
3,啟動(dòng)squid服務(wù)
service squid start/restart
4,停止squid服務(wù)
service squid stop
5.重新加載配置文件
squid -k reconfig
配置文件squid.conf 常用配置項(xiàng)
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
大小 一級(jí)目錄 二級(jí)目錄
1.普通代理配置方法
demo:
虛擬機(jī)兩個(gè) clone一臺(tái)
squid 300M內(nèi)存 clone 100M內(nèi)存
改下名字 squid-server web-server
squid-server 要兩塊網(wǎng)卡
啟動(dòng)兩臺(tái)虛擬server
vim1 192.168.10.2
ifconfig eth0 192.168.10.1
ifconfig eth0
ifconfig eth1 200,168.10.1
進(jìn)入web-server
ifconfig eth0 200.168.10.2
測(cè)試
ping 192.168.10.2
ping 200.168.10.2
進(jìn)入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網(wǎng)段的人訪問代理服務(wù)器
http_access deny all //拒絕。。。。。
wq:
service squid restart
pstree |grep squid
netstat -tunpl |grep :3128
進(jìn)入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
設(shè)置代理
可以通過代理服務(wù)器上網(wǎng)
ACL訪問控制: 可以從客戶機(jī)的IP地址,請(qǐng)求的URL,文件類型,訪問時(shí)間,并發(fā)請(qǐng)求數(shù)來進(jìn)行控制
ACL控制列表:
定義acl列表:
acl 列表名稱 列表類型 列表內(nèi)容
針對(duì)acl列表進(jìn)行限制
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 時(shí)間
maxconn 最大連接數(shù)
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$
根據(jù)已定義的acl列表進(jìn)行訪問控制
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透明代理
不用設(shè)置代理IP端口,只要設(shè)置網(wǎng)關(guān)就行。
前提:客戶機(jī)的web訪問數(shù)據(jù)要能經(jīng)過防火墻
代理服務(wù)構(gòu)建在網(wǎng)關(guān)主機(jī)中
修改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:
進(jìn)入squid-server
vi squid.conf
修改
http_port 192.168.10.1:2128 transparent
重新加載配置項(xiàng)
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
進(jìn)入squid-server
ifconfig eth0 200.168.10.1
ifconfig eth1 192.168.10.1
進(jìn)入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分區(qū)。
針對(duì)海量數(shù)據(jù)的優(yōu)化主要有2種方法:大表拆小表,sql語句的優(yōu)化。索引
1.分庫(kù)分表:
垂直分表,水平分表。
分表分導(dǎo)致要修改sql語句,會(huì)造成邏輯的復(fù)雜。使用分區(qū)技術(shù)可以避免。
2.分區(qū)技術(shù): mysql5.1后所帶的一種分區(qū)技術(shù),它是在邏輯層進(jìn)行水平分表。
(1)range分區(qū):
基于一個(gè)給定連續(xù)區(qū)間的殘值,把多行分配給分區(qū)
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分區(qū)
partition p0 values less than(11) //小于11的在p1分區(qū)
partition p0 values less than(16)
partition p0 values less than(21)
);
(2)list分區(qū):
類似于range分區(qū),區(qū)別在于基于列值匹配一個(gè)離散值集合中的某個(gè)值來進(jìn)行選擇。
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), //在這個(gè)里面的,放在pnorth分區(qū)里,
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分區(qū):基于用戶定義的表達(dá)式的返回值來進(jìn)行選擇分區(qū)。
用來確保數(shù)據(jù)在預(yù)先確定的數(shù)目的分區(qū)中平均分布
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分區(qū)
可以為字符型等其它非int類型,md5,ash
-------------------
實(shí)例操作:
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 切換終端 監(jiān)控
cd data
ls
ll -h
watch-n1 | ls -lh 時(shí)時(shí)跟蹤命令結(jié)果
insert into t2 select * from t2; //不停插入直到1000W行。
select count(*) from t2;
R+f3 切換到第三個(gè)終端:
tree
free //內(nèi)存的使用
free -m //以M來看內(nèi)存
top //cpu使用情況
select count(*) from t2;
select count(*) from t2 where id > 5; //帶上where耗時(shí)就長(zhǎng)
create table t3(id int);
\d // //改變結(jié)束符
//創(chuàng)建存儲(chǔ)
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; //查看存儲(chǔ)
?procedure;
drop procedure p3; //刪除存儲(chǔ)
show procedure status;
call p3;
==================================================
創(chuàng)建一張分區(qū)表
創(chuàng)建一張未分區(qū)表
寫一個(gè)存儲(chǔ),插入幾百萬的數(shù)據(jù)
mysql 基礎(chǔ)操作
1.mysql表復(fù)制
create table t3 like t1;
insert into t3 select * from t1;
2,mysql索引
create index 創(chuàng)建索引
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 創(chuàng)建刪除索引,還可以操作主鍵索引,最好的方法
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 視圖
通過一個(gè)條件從一張表拿出來的數(shù)據(jù)形成一張中間表,就是視圖,而且會(huì)跟著主表時(shí)時(shí)更新。
? 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內(nèi)置函數(shù)
字符串函數(shù);
concate 連接字符串
lcase 轉(zhuǎn)小寫
ucase 轉(zhuǎn)大寫
length 長(zhǎng)度
ltrim 去左空格
rtime 去右空格
repeat 重復(fù)count次
replace 替換
substring/substr 截取字符串
space(count) 生成count個(gè)空格
數(shù)學(xué)函數(shù):
bin 十進(jìn)制轉(zhuǎn)二進(jìn)制
ceiling 向上取整
floor 向下取整
max 取最大值
min 取最小值
sqrt 開平方
rand 取隨機(jī)數(shù)
日期函數(shù):
curdate
curtime
now
timestamp
week
year
datediff
unix_timestamp
from_unixtime
5,mysql預(yù)處理語句
prepare stmt1 from 'select * from t1 where id>?';
set @i=1;
execute stmt1 using @i;
drop prepare stmt1;
相當(dāng)于 把@i 作為 ? 那部分執(zhí)行
即 select * from t1 where id>1
6,mysql 事務(wù)處理
set autocommit=0; 自動(dòng)提交改為0
delect from t1; 刪除表數(shù)據(jù)
select * from t1;
rollback; 恢復(fù)
select * from t1;
commit; 提交
savepoint p1; 設(shè)置還原點(diǎn)、
rollback to p1; 還原到p1還原點(diǎn)、
7,mysql存儲(chǔ)
存儲(chǔ)就是要執(zhí)行的代碼段、
?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; //執(zhí)行存儲(chǔ)p1;
show procedure status\G
show create procedure p1;
8,mysql觸發(fā)器
向一個(gè)表執(zhí)行數(shù)據(jù)時(shí),對(duì)另一個(gè)表也同時(shí)執(zhí)行
例:向t1表插入數(shù)據(jù),同時(shí)向t2表插入數(shù)據(jù)
truncate t1; 刪除t1表的數(shù)據(jù),從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技巧
轉(zhuǎn)載于:https://blog.51cto.com/zaikuip/1300124
總結(jié)
- 上一篇: python的三个函数(eval、exe
- 下一篇: php 命令行方式运行时 几种传入参数的