LNMP +memcached(开源跨平台)
proxe代理服务器:安装memcached服务 (memcache,内存存储——要先装事件触发器的包libevent,libevent和memache要版本兼容,这里libevent-2.0.21,memcached-1.4.24)
先安装libevent
#yum-y install gcc
#tar -xf libevent-2.0.21-stable.tar.gz (memcached服务库文件)
#cd libevent-2.0.21-stable
#./configure
#make && make install
可以直接给库文件做链接也可在库配置文件中写进去,做下面其一就行。
1)#ln -s /usr/local/lib/libevent/* /usr/lib/
#ldconfig (//刷新)
2)#vim /etc/ld.so.conf (库文件的配置文件)
/usr/local/lib/
#ldconfig(//刷新)
3)#vim /etc/ld.so.conf/libevent.conf
/usr/local/lib/
#ldconfig
#tar -zxf memcached-1.4.24.tar.gz
#cd memcached-1.4.24
#./configure
#make && make install
#ln -s /usr/local/bin/memached /bin
#memcached -h (查看帮助)
(memcached -u
# memcached -u root -m 64 -vv -d 启动memcached
查看服务是否启动:
# netstat -anptu |grep :11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6813/memcached
tcp 0 0 :::11211 :::* LISTEN 6813/memcached
udp 0 0 0.0.0.0:11211 0.0.0.0:* 6813/memcached
udp 0 0 :::11211 :::* 6813/memcached
【memcached 没有关闭服务脚本,杀进程pkill -9 memcached停止服务】
测试:
# yum -y install telnet
# telnet 192.168.4.511211
Trying 192.168.4.5...
Connected to 192.168.4.5.
Escape character is '^]'.
set name 0100 3
tom
STORED
get name
VALUE name 0 3
tom
END
replace name 0200 3
asc
STORED
get name
VALUE name 0 3
asc
END
addsetreplacedeleteget
#cd /usr/lcoal/bin
#./php -m(查看php所支持的功能)
#./php -m |grep memcached (查php是否支持memcached)
让php支持memcached
#cd
# tar -zxvf memcahce-2.2.5
# cd memcahce-2.2.5
#/usr/local/php5/bin/phpize
#./configure--with-php-config=/usr/local/php5/bin/php-config--anable-memcache
#make&& make install
客户---网站---登录[代码]---数据库
memcached临时数据,永久数据
永久数据一定要存储在SQL数据库中
memcached目的,缓存这些数据
memcached的数据什么时候会丢失
1.服务重启
2.计算机重启
3.数据过期
4.delete或者fluash_all
5.LRU
——————————————————————————————————————————————————————————————————————————————————————————
——————————————————————————————————————————————————————————————————————————————————————————
LNMP +memcache 在安装Php过程时,要按装memcache模块,才能用Php解析memcached的指令。(memcached是服务 memcache是php调用该服务的模块)
(注意:这是PHP使用数据库memcache时需要安装该模块功能,因为上课实验,脚本中都已经默认添加安装列)
# ./configure \
> --with-php-config=/usr/local/php5/bin/php-config
> --enable-memcache )
LNMP +memcached 实验:(linux nginx mysql php)+memcached
# vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_indexindex.php;
# fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
include fastcgi.conf;
}
# vim /usr/local/nginx/html/test.php
# nginx -s reload
# netstat -anptu |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6875/nginx
# service php-fpm restart (安装了memcache模块,默认脚本里是安装了的,没有的话就要加模块)
# netstat -anptu |grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 9931/php-fpm
# netstat -anptu |grep :11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6813/memcached
测试:
浏览器测试:
# firefox http://192.168.4.5/test.php
test
memcache数据库查看测试效果:
# telnet 192.168.4.5 11211
Trying 192.168.4.5...
Connected to 192.168.4.5.
Escape character is '^]'.
get key
VALUE key 0 4
test
END
——————————————————————————————————————————————————————————————————————————————————————————————
++++++++++++++++++++++++++++++++
PHP--->memcached
++++++++++++++++++++++++++++
注意:memcache安装要安装在有PHP的主机
memcache是一个PHP的扩展库文件
#tar -xf memcache-2.2.5.tgz
#cd memcache-2.2.5
#/usr/local/php5/bin/phpize .
#./configure
#make && make install
#sed -i '728i extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20100525/"' /usr/local/php5/etc/php.ini
#sed -i '856i extension=memcache.so' /usr/local/php5/etc/php.ini
给PHP安装memcache扩展以后,PHP就可用支持连接memcached服务器
#vim /usr/local/nginx/html/test.php
#servicephp-fpmrestart
排错:
1.nginx是否启动
2.nginx是否通过fastcgi_pass连接PHP
3.PHP是否启动service php-fpm start
4.PHP是否安装了memcac he扩展,并调用了该扩展,并重启PHP
6.test.php是否有语法错误
7.memcached启动服务
用户->nginx->PHP->memcache->memcached
日志:nginx/logs/error.log
++++++++++++++++++++++++++++++++
client:192.168.4.100eth0
nginx: 192.168.4.5 eth0nginx
192.168.2.5 eth1
tomcat1:192.168.2.100 eth1tomcat
tomcat2:192.168.2.200 eth1tomcat
负载均衡,后台从httpd换成tomcat
1.nginx负载均衡
#vim nginx/conf/nginx.conf
upstream tom {
server 192.168.2.100:8080;
server 192.168.2.200:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tom;
...
}
}
#nginx -s reload
2.安装启动tomcat
#/usr/local/tomcat/bin/catalina.sh start
++++++++++++++++++++++++++++++
实验:使用memcached保存会话信息
以下操作,需要在两台tomcat都执行,现在两台web机上安装tomcat.
# cd lnmp_soft
# rpm -ivh jdk-8u77-linux-x64.rpm
Preparing... ###########################################
1:jdk1.8.0_77 ###########################################
Unpacking JAR files...
tools.jar...
........
# java -version
java version "1.8.0_77"
# tar -zxf apache-tomcat-8.0.30.tar.gz
# mv apache-tomcat-8.0.30 /usr/local/tomcat
# ln -s /usr/local/tomcat/bin/catalina.sh /bin
# catalina.sh start
1.创建java页面,显示sessionID
(192.4.2.100)
#cat /usr/local/tomcat/webapps/ROOT/test.jsp
tomcatA
(192.42.200)
#cat /usr/local/tomcat/webapps/ROOT/test.jsp
tomcatA
测试一:
# firefox http://192.168.4.5/test.jsp
再按F5就会在100和200之间切换,有明显区别。
2.让tomcat连接memcached(web1和web2)
需要给tomcat安装扩展库文件
#cd lnmp_soft/session/
#cp *.jar/usr/local/tomcat/lib/
3.编写tomcat的context.xml文件,指定memcached的IP和端口
#cd lnmp_soft/session
#cp context.xml/usr/local/tomcat/conf/
#vim /usr/local/tomcat/conf/context.xml
... ...
memcachedNodes="mem1:192.168.2.5:11211"
将IP修改为启动了memcached服务那台主机的IP地址
#/usr/local/tomcat/bin/catalina.sh stop
#/usr/local/tomcat/bin/catalina.sh start
测试:(192.168.4.5)
# firefox http://192.168.4.5/test.jsp
再按F5刷新(在100和200之间切换,除了颜色不同之外,可以在1中,网页文件中分别写入不同数据让它显示。)
redis (实验都是做好的脚本)
# cd lnmp_soft
# tar -zxf redis-3.0.6.tar.gz
# cd redis-3.0.6
# make (没有./configue是因为老师做的脚本都已经做过之后打包的,所以直接make)
# make install
# cd utils/
# ./install_server.sh(该脚本是提前写好列的,运行后,会复制配置文件,会创建日志文件,开启端口服务,等)
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
+++++++++++++++++++++++++++++++++++
redis数据库数据保存的位置:(工作中需要备份这里的数据来保存)
cat /var/lib/redis/6379/dump.rdb
+++++++++++++++++++++++++++++++++++
--------------------------------------------------------------------------------------------------------------——————————
redis(数据库):key=valurehashlist 安全默认永久保存 | memcached(数据库):key=valure不安全临时存储
string:key=valure
hash=表 【列 值 列 值】
listA=a b c d
list先进后出
聊天记录
购物记录
# redis-cli(支持tab建 上下 还有help帮助)
127.0.0.1:6379> set name tom (默认永久有效)
OK
127.0.0.1:6379> get name
"tom"
127.0.0.1:6379> help set
SET key value
summary: Set the string value of a key
since: 1.0.0
group: string
127.0.0.1:6379> set name carry ex 10
OK
127.0.0.1:6379> get name
"carry"
127.0.0.1:6379> get name
"carry"
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set name jerry nx(nx新加)
(nil)
127.0.0.1:6379> set test 123nx
OK
127.0.0.1:6379> set test 111 nx
(nil)
127.0.0.1:6379> set test 111 xx(xx覆盖)
OK
127.0.0.1:6379> get test
"111"
+++++++++++++++++++++++++++++++++++
redis数据库数据保存的位置:(工作中需要备份这里的数据来保存)
cat /var/lib/redis/6379/dump.rdb
+++++++++++++++++++++++++++++++++++
127.0.0.1:6379> set test"hellow the world"
OK
127.0.0.1:6379> get test
"hellow the world"
127.0.0.1:6379> SETRANGE test 12 redinsnh (从零开始算)
(integer) 20
127.0.0.1:6379> get test
"hellow the wredinsnh"
127.0.0.1:6379> SETRANGE test 11 xxx
(integer) 20
127.0.0.1:6379> get test
"hellow the xxxdinsnh"
127.0.0.1:6379> SETRANGE test 11 jjjjjjjjjjjj
(integer) 23
127.0.0.1:6379> get test
"hellow the jjjjjjjjjjjj"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
set p 101 (每个字符1位,都是显示101.这显示的是字符)
setbit v01 (每个八位,这显示的101是数字)
setbit v10
setbit v21
p==24位
v=3位
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
www.redhatdu.com 11.1
11.2 tom
11.20 tom
11.25 tom
统计每个人登录天数
set tom_11.21
set tom_11.20 1
set tom_11.25
统计tom用户11月登录该网站多少次 www.redhatdu.com 11.1
127.0.0.1:6379> SETBIT tom 2 1
(integer) 0
127.0.0.1:6379> SETBIT tom 20 1
(integer) 0
127.0.0.1:6379> SETBIT tom 25 1
(integer) 0
127.0.0.1:6379> BITCOUNT tom(BITCOUNT统计)
(integer) 3
127.0.0.1:6379> set phone 18535335225
OK
127.0.0.1:6379> get phone
"18535335225"
127.0.0.1:6379> GETRANGE phone 3 3
"3"
127.0.0.1:6379> GETRANGE phone 3 5
"353"
127.0.0.1:6379> GETRANGE phone -4 -1 (对)
"5225"
127.0.0.1:6379> GETRANGE phone -1 -4(错)
""
127.0.0.1:6379> GETRANGE phone 0 -1
"18535335225"
127.0.0.1:6379> GETRANGE phone 0 2
"185"
127.0.0.1:6379> GETSET tom tt (将tom覆盖成tt)
" \x00\b@"
127.0.0.1:6379> get tom
"tt"
127.0.0.1:6379> GETSET tom 123
"tt"
127.0.0.1:6379> get tom
"123"
127.0.0.1:6379> help mset
MSET key value
summary: Set multiple keys to multiple values
since: 1.0.1
group: string
127.0.0.1:6379> mset a 123 b 456 c 789
OK
127.0.0.1:6379> mget a b c
1) "123"
2) "456"
3) "789"
127.0.0.1:6379> hset lily sex femal
(integer) 1
127.0.0.1:6379> hset lily phone 11232
(integer) 1
127.0.0.1:6379> hset lily age 14
(integer) 1
127.0.0.1:6379> hget lily age
"14"
127.0.0.1:6379> hget lily phone
"11232"
127.0.0.1:6379> hget lily sex
"femal"
127.0.0.1:6379> hmset zansan age 12 phone 121 (一个表中定义多列)
OK
127.0.0.1:6379> hmget zansan age phone
1) "12"
2) "121"
127.0.0.1:6379> HGETALL zansan(hgetall 查所有)
1) "age"
2) "12"
3) "phone"
4) "121"
127.0.0.1:6379> hkeys zansan
1) "age"
2) "phone"
127.0.0.1:6379> hvals zansan
1) "12"
2) "121"
127.0.0.1:6379> hincrby zansan age 1 (hincrby对表中的一个数据进行相加)
(integer) 13
127.0.0.1:6379> hincrby zansan age 1
(integer) 14
127.0.0.1:6379> hmget zansan age
1) "14"
127.0.0.1:6379> hincrby zansan age 1
(integer) 15
127.0.0.1:6379> hmget zansan age
1) "15"
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
练习:(redis文件)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# vim /etc/redis/6379.conf
pidfile /var/run/redis_6379.pid
port 6379
tcp-backlog 511排队长度,不是并发
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
bind 192.168.4.5 (只允许4.5,没有这行就是允许所有)
logfile /var/log/redis_6379.log
databases 16 (最大16个数据库用0-15表示)
save 900 1
save 300 10 (按时间存盘,如果不到时间,重启或关机也会存盘,只要不是突然断电)
save 60 10000
rdbcompression yes(数据存盘后压缩)
dbfilename dump.rdb (数据存放的文件名和路径)
dir /var/lib/redis/6379
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
redis数据库 做主从 +
+++++++++++++++++++++++++
主服务器(192.168.4.5/192.168.4.2.5):
# vim /etc/redis/6379.conf
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
bind 192.168.4.5 (这三行设置只允许4.5ip访问)
397requirepass 123456 (redisrequire 一定要配密码)
# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
# /etc/init.d/redis_6379 restart (重启不了。要密码)
Stopping ...
(error) NOAUTH Authentication required.
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
Waiting for Redis to shutdown ...
# redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
# redis-cli -h
# redis-cli -a 123456
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set ttt 1235
OK
127.0.0.1:6379> get ttt
"1235"
# redis-cli -a 123456 set abc 123 (非交互式)
OK
# redis-cli -a 123456 get abc
"123"
vim /etc/init.d/redis_6379(在脚本启动文件中 stop这 加上 -a 123456 密码)
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -a 123456-p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
root@proxe ~]# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
————————————————————————
为了主服务器安全:
最好两个都配:
bind 192.168.4.5 (只允许内网访问)
(redisrequire 一定要配密码)
————————————————————————
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
从服务器设置:192.168.2.100
# vim /etc/redis/6379.conf
212slaveof 192.168.2.5 6379 (主服务器端口和IP 和本机要连得通)
220 masterauth 123456 (主服务器密码)
# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
# redis-cli (从服务器获得主服务器数据)
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "page"
2) "bit1
-------------------------------------------
-h 192.168.4.5 -a 123456
-------------------------------------------
页:
[1]