php特级学习笔记
一, 大訪問量
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技巧
轉載于:https://blog.51cto.com/zaikuip/1300124
總結
- 上一篇: python的三个函数(eval、exe
- 下一篇: php 命令行方式运行时 几种传入参数的