当前位置:Gxlcms > 数据库问题 > 基于redis缓存数据库实现lnmp架构高速访问

基于redis缓存数据库实现lnmp架构高速访问

时间:2021-07-01 10:21:17 帮助过:86人阅读


使用nosql数据库:

    如redis,mongodb,memcache


what-redis

redis 是一个高性能的 key-value 数据库。

1) redis 的出现,很大程度弥补了memcached 这类 key-value 存储的不足(只能存入内存).

2)它支持的数据类型比memcache多,包括了 Python,Ruby,Erlang,PHP 客户端...

3)Redis 的所有数据都是保存在内存中,两种同步模式

        A>半持久化模式:RDB(全量同步)

                i>RDB是Redis默认同步方式

                ii>不定期的通过异步方式保存到磁盘上,快照最终结果( 快照二进制文件为dump.rdb)

                iii>在恢复大数据集时的速度比 AOF 的恢复速度要快。

        B>全持久化模式:(增量同步)

                i>把每一次数据变化都写入到一个 append only file(aof)里面.

                ii>没有RDB同步的快,但采用了高并发机制,对系统内存要求高.

                iii>使用方式

                    #appendfsync always实时同步

                    appendfsync everysec每秒同步(推荐使用)

                    # appendfsync no

        一般两种模式结合使用.

4)只保证最终数据一致性,适用于微博粉丝,秒杀,抢红包等场景,微博粉丝数量,一会儿加一减一的,要是这么微量但高频率的变化,每次都要写到数据库里,那数据库的负担就太重啦~





基于redis实现lnmp架构高速访问

sever11:

 yum install mysql-server -y

mysql>grant select on test.* to redis@‘172.25.88.%‘ identified by ‘miao‘;

mysql <test.sql 导入测试数据,可用select查看

技术分享


server13:


tar zxf redis-3.2.5.tar.gz 

yum install gcc -y make && make install

cd utils/ ./install_server.sh

vim /etc/redis/6379.conf


62 bind 127.0.0.1 172.25.4.13  主redis的标准配置

/etc/init.d/redis_6379 restart


redis 端口打开,进入redis.

[root@server13 redis]# netstat -antlp 
Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 172.25.4.13:6379 0.0.0.0:*

[root@server13 redis]# redis-cli
127.0.0.1:6379> del 1 (integer) 1

server12:

快速搭建lnmp架构

rpm -ivh php-5.3.3-38.el6.x86_64.rpm php-cli-5.3.3-38.el6.x86_64.rpm php-fpm-5.3.3-38.el6.x86_64.rpm php-cli-5.3.3-38.el6.x86_64.rpm php-pdo-5.3.3-38.el6.x86_64.rpm php-common-5.3.3-38.el6.x86_64.rpm php-mysql-5.3.3-38.el6.x86_64.rpm

/etc/init.d/php-fpm restart

技术分享


cd /etc/php-fpm.d/

 vim www.conf

39 user = nginx 41 group = nginx

vim /etc/nginx/conf.d/default.conf

 10         index  index.php index.html index.htm;

30     location ~ \.php$ {
31         root           html;
32         fastcgi_pass   127.0.0.1:9000;
33         fastcgi_index  index.php;
34         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;
35         include        fastcgi_params;
36     }

nginx -s reload

cd /usr/share/nginx/html/

 vim index.php 

php<?

phpinfo()

?>

检测:lnp是否搭建成功

出现php测试页~~~



php装载redis模块

server12:


yum install -y unzip 
cd redis/
unzip phpredis-master.zip
cd phpredis-master
yum install gcc -y yum install php-devel-5.3.3-38.el6.x86_64.rpm
phpize     生成预编译环境
./configure make && make install


在 /usr/lib64/php/modules/ 出现redis模块


cd /etc/php.d cp mysql.ini redis.ini            仿照mysql模块定义redis插件


vim redis.ini

2 extension=redis.so

/etc/init.d/php-fpm reload

php -m |grep re

技术分享

cp /mnt/redis/test.php index.php

vim index.php

3 $redis->connect(‘172.25.4.13‘,6379) or die ("could net connect redis server");

10$connect = mysql_connect(‘172.25.4.11‘,‘redis‘,‘westos‘);


检验


在宿主机浏览器访问,http://172.25.88.12/

第一次读取数据是从mysql库中读取,后来读取数据就从redis中读取了.


技术分享                           技术分享        


这样读取数据存在隐患,当mysql中的数据发生变化时,用户读取还是redis中旧数据,所以需要考虑触发更新机制,如果想了解可以移步.......



server11: 

mysql> update test.test set name=‘lalala‘ where id=1;


serve13:

除非在更新数据库的同时,手动删除redis的数据,让redis再去mysql取一次数据(不推荐这种方式....)


redis-cli 

127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> del 1             删除1,让redis再去数据库拿一次
(integer) 1
127.0.0.1:6379> get 1
(nil)


查看客户端的变化



技术分享



本文出自 “12049878” 博客,谢绝转载!

基于redis缓存数据库实现lnmp架构高速访问

标签:高速缓存   redis   lnmp   

人气教程排行