什么是Nosql
NoSQL,泛指非关系型的数据库。随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,暴露了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展。NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用难题。
一、memcache
memcache是NoSQL的一种。
第一次访问数据库,后面使用memcache(key-value)。memcache工作于内存中
1.配置memcache
tar zxf memcache-2.2.5.tgz
cd memcache-2.2.5
vim ~/.bash_profile ##更改path
source ~/.bash_profile ##重载
phpize ##拓展php的扩展模块
./configure ##编译
make && make install ##安装
vim /usr/local/lnmp/php/etc/php.ini ##编辑php配置文件
/etc/init.d/php-fpm start
php -m |grep memcache ##memcache模块是否开启
yum install memcached -y ##安装memcache守护程序
cat /etc/sysconfig/memcached
MAXCONN=“1024” ##最大链接数1024;mysql之前设置过
CACHESIZE=“64”
OPTIONS="" ##查看memcache端口为11211
/etc/init.d/memcached start
netstat -antlpe | grep memcache
2.用telnet测试
yum install telnet -y
telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
set name 0 0 6 ##key:name;编号:0;缓存时间:0(永久)valuce值:6位
westos
STORED
get name ##查看name
VALUE name 0 6
westos
END
delete name ##删除name
DELETED
set name 0 5 6 ##建立存活时间为5s的name
westos
STORED
get name ##5s过后,查看name,已失效,查看不到
END
quit
3.memcache加速http
[root@server1 memcache-2.2.5]# cp memcache.php example.php /usr/local/lnmp/nginx/html/
##memcache.php是监控页面 example.php用来测试memcache命中率
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/
[root@server1 html]# vim memcache.php
改密码;取消负载均衡,改本地ip
[root@server1 html]# nginx -s stop ##原nginx不支持memcache,把它关闭
安装特殊版nginx(支持memcache)
[root@server1 html]# cd /root/lnmp/
[root@server1 lnmp]# tar zxf openresty-1.13.6.1.tar.gz
[root@server1 lnmp]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ./configure
[root@server1 openresty-1.13.6.1]# gmake && gmake install
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/memcache.php .
[root@server1 html]# ls
50x.html example.php index.html index.php memcache.php
[root@server1 html]# cd ..
[root@server1 nginx]# cd conf/
[root@server1 conf]# id nginx ##察看是否有nginx用户
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@server1 conf]# vim nginx.conf
配置nginx主配置文件:
1.nginx用户及组,自动开启进程;
2.upstream模块:nginx现在是反向代理;保持连接512;
3.导入/memc模块;
4.设置请求php时,先找memcache,若mecache没有再找mysql;访问结束后缓存数据到memcache;更改fastcgi.conf
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx ##开启服务
压测
10个并发,5000个请求
[kiosk@foundation64 ~]$ ab -c 10 -n 5000 http://172.25.64.1/example.php ##速度快,无失败连接
[kiosk@foundation64 ~]$ ab -c 10 -n 5000 http://172.25.64.1/index.php ##速度慢
二、nginx+tomcat+memcached实现负载均衡
(1)Nginx负责负载均衡。
(2)Tomcat负责实际服务。
(3)Memcached负责同步Tomcat的Session,达到Session共享的目的。
二、tomcat
1.安装java环境
[root@server2 ~]# cd lnmp/
[root@server2 lnmp]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/ ##安装jdk
[root@server2 lnmp]# cd /usr/local/
[root@server2 local]# ln -s jdk1.7.0_79/ java ##建立软连接便于访问
[root@server2 local]# vim /etc/profile ##配置全局环境变量
[root@server2 local]# source /etc/profile ##使文件生效
[root@server2 local]# echo $PATH ##查看是否添加成功
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin
测试
[root@server2 local]# vim test.java
[root@server2 local]# javac test.java ##编译,编译完会生成.class文件,是一个java库数据文件
[root@server2 local]# java test
server3相同操作
2.安装tomcat
tomcat不能反复开启,需要关闭后再开启。否则会增加很多进程,占用端口。如果开启不了tomcat,先查看文件是否错误,再查看端口是否被占用,或结束进程
[root@server2 lnmp]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server2 lnmp]# cd /usr/local/
[root@server2 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@server2 local]# cd tomcat/webapps/ROOT/
[root@server2 ROOT]# vim test.jsp
[root@server2 bin]# pwd
/usr/local/tomcat/bin
[root@server2 bin]# ./startup.sh
[root@server2 bin]# netstat -antlp|grep java
server3安装:
[root@server2 local]# scp -r tomcat/ root@172.25.64.3:/usr/local/
[root@server3 bin]# ./startup.sh
[root@server3 bin]# netstat -antlp|grep java