fdhfgh 发表于 2018-11-4 07:57:21

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

how-缓存加速
  使用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.%'>  mysqldel 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         indexindex.php index.html index.htm;  30   location ~ \.php$ {
  31         root         html;
  32         fastcgi_pass   127.0.0.1:9000;
  33         fastcgi_indexindex.php;
  34         fastcgi_paramSCRIPT_FILENAME/usr/share/nginx/html/$fastcgi_script_name;
  35         include      fastcgi_params;
  36   }

  nginx -s>  cd /usr/share/nginx/html/
  vim index.php
php
  检测: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>  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中旧数据,所以需要考虑触发更新机制,如果想了解可以移步.......
  通过 mysql 触发器+gearman+实现redis数据同步
  server11:

  mysql> update test.test set name='lalala' where>  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)
  查看客户端的变化



页: [1]
查看完整版本: 基于redis缓存数据库实现lnmp架构高速访问