基础介绍:
- 负载均衡的原理:
|----->server1 (Nginx+php+mysql) ---> | memcached | client -----> proxy ----> ----->server2 (Nginx+php+mysql) ---> | memcached | mysql集群 |----->server3 (Nginx+php+mysql) ---> | memcached |
- 数据库优势:
有强大的查询能力,文字字符(metedata-数据的数据)。
- 存在的缺点:
能不存储在数据库中的一定不放在数据库中。 处理数据的速度:CPU --> 内存 --> 硬盘
- 存在的优点:
传统的数据库在不优化情况下读读写速度大概几千 放在内存中的Nosql来说读写速度高达10W
- nosql数据库: memcached 、 redis 、mongodb
- 缓存数据库:(memcached存在的最大问题就是重启数据丢失)
官方网站:
-> www.memcached.org
memcached --> 不能存储表
> key-value(KV数据库)
配置文件:
-> vim /usr/lib/systemd/system/memcached.service
-> vim /etc/sysconfig/memcached
memcached的弊端官方不提供读写工具:
-> 为了方便实验使用telnet进行连接
实验准备:
1.一台Centos7虚拟机
2.安装memcached、telnet
准备工作:
yum -y install memcached telnet
systemctl start/enabled memcached
setenforce 0
systemctl stop/disable firewalld
开始测试:
telnet 192.168.4.100 11211 连接该端口 #进入memcache的操作界面
———————————(1)——————————
set fengjie 0 180 3
jpg
STORED # seccuss
+============================+
解释:设置key名称fengjie,0表示不会压缩,
180表示3分钟后数据自动删除,存在的内容是3个字符
+============================+
———————————(2)——————————
get fengjie #获取变量的数值
VALUE fengjie 0 3 #结果:(变量名)(是否压缩)(字符字数)
sbw #变量的值
END #结束
#######################################
———————————(3)——————————
add sbma? 0 180 10 #存在则不建立,不存在则新建
niqueding?
STORED
#######################################
———————————(4)——————————
set sbma? 0 180 10 #添加或替换变量
shidewoshi
STORED
get sbma?
VALUE sbma? 0 10
shidewoshi
END
append sbma? 0 180 3 #向变量中追加数据
yes
STORED
get sbma?
VALUE sbma? 0 13
shidewoshiyes
END
#######################################
———————————(5)——————————
replace sbma? 0 180 10 #替换,如果myname不存在则报错
delete sbma? #删除变量
stats #查看状态
flush_all #清空所有
quit #退出登录
#######################################
用PHP操作Memcache方案:
准备工作:
lnmp架构
修改配置文件
步骤一:
yum -y mariadb-server mariadb php-pecl-memcache nginx php-fpm memcache
源码编译不再说
启动php-fpm、mariadb、nginx
步骤二:
vim /usr/local/nginx/conf/nginx.conf
======================================================
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
======================================================
步骤三:
vim /usr/local/nginx/html/test.php
=================================================
<?php
memcache=new Memcache; #创建memcache对象 memcache->connect('localhost',11211) or die ('could not connect!!');
memcache->set('key','test'); #定义变量 get_values=memcache->get('key'); #获取变量 echo get_values;
?>
=================================================
测试:
telnet 192.168.4.100 11211
====================
get key
VALUE key 0 4
test #看见数值就成功
END
quit
====================
如果出现报错:
白屏幕,报错文件为为找到解决方案
1.是否有动静分离(下载下来就是没有作动静分离)
2.是否安装php-pecl-memcache(空白)
3.是否重启php-fpm(空白)
简单的解释Session原理:
人 -------> (1)连锁超市
ID号唯一 办卡id[用户信息] ————|
| ————》 在连锁超市不会反复的办卡
人 -------> (2)连锁超市 | ————》 只需要读取的你信息就可以
ID号唯一 获取id[用户信息] ————|
目的:
为了实现的多台机器同时认证一个用户名
实战演练过程:
准备工作:
proxy 一台主机
web1、web2 二台作作
项目需求:
1.实现访问web1时候Session记录,访问web2时候无记录(upstream)
2.实现访问web1时候同时web2也有Session也有记录(memcache)
开始部署:
1.基础部署
proxy、web1、web2
yum -y install mariadb mariadb-server php-fpm nginx
systemctl restart/enabled mariadb/php-fpm
2.修改配置文件
对proxy的进行修改
vim /usr/local/nginx/conf/nginx.conf
+===================================+
http {
...
upstream sb {
server 192.168.2.100;
server 192.168.2.200;
}
server {
location / {
proxy_pass http://sb; #开启反向代理
root html;
index index.html index.htm;
}
+===================================+
nginx -s reload
对web1、web2进行修改
vim /usr/local/nginx/conf/nginx.conf
+===================================+
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
+===================================+
cd /root/lnmp_soft/php_scripts/php-memcached-demo
cp -a * /usr/local/nginx/html
nginx -s reload
firefox 192.168.4.5 访问测试
3.测试(实现要求1的内容)
cd /var/lib/php/session
访问web1的页面,输入信息:123,123
返回web1服务器,查询
目录下会有唯一的session数值
4.测试(实现有求2的内容)
原来的配置项目无法实现session保持会话所以开始修改
+======================================+
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
+======================================+
5.实现保持会话的方式
定义Session信息存储在公共的memcached服务器上,主机参数中为memcache(没有d)
通过path参数定义公共的memcached服务器在哪(服务器的IP和端口)
+======================================+
php_value[session.save_handler] = memcahe
php_value[session.save_path] = "tcp://192.168.4.5:11211"
+======================================+
systemctl restart php-fpm
Hello there, You’ve done an incredible job. I will definitely digg it and personally suggest to my friends. I’m sure they will be benefited from this site.