侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

redis服务器环境下mysql实现lnmp架构缓存

Redis  /  管理员 发布于 7年前   158

配置环境:redhat6.5

server1:redis(172.25.254.1)

server2:php(172.25.254.2)

server3:mysql(172.25.254.3)

配置步骤:

server2:

1、server2安装php的redis相应模块

2、nginx安装

[root@server2 php-fpm.d]# rpm -ivh nginx-1.8.0-1.el6.ngx.x86_64.rpm warning: nginx-1.8.0-1.el6.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEYPreparing...        ########################################### [100%]  1:nginx         ########################################### [100%]---------------------------------------------------------------------- Thanks for using nginx! Please find the official documentation for nginx here:* https://nginx.org/en/docs/Commercial subscriptions for nginx are available on:* https://nginx.com/products/ ----------------------------------------------------------------------[root@server2 php-fpm.d]# id nginxuid=498(nginx) gid=499(nginx) groups=499(nginx)

3、nginx和php配置

1、php配置

[root@server2 php-fpm.d]# cd /etc/php-fpm.d/[root@server2 php-fpm.d]# id nginxuid=498(nginx) gid=499(nginx) groups=499(nginx)[root@server2 php-fpm.d]# vim www.conf  39 user = nginx 41 group = nginx [root@server2 php-fpm.d]# vim /etc/php.ini 946 date.timezone = Asia/Shanghai[root@server2 php-fpm.d]# /etc/init.d/php-fpm startStarting php-fpm:         [ OK ][root@server2 php-fpm.d]# netstat -antlp | grep phptcp    0   0 127.0.0.1:9000       0.0.0.0:*          LISTEN   1125/php-fpm    [root@server2 php-fpm.d]# vim /etc/php.ini

2、nginx配置

[root@server2 ~]# cd /etc/nginx/conf.d/[root@server2 conf.d]# lsdefault.conf example_ssl.conf[root@server2 conf.d]# vim 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   }[root@server2 conf.d]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@server2 conf.d]# nginx [root@server2 conf.d]# netstat -anplt |grep nginxtcp    0   0 0.0.0.0:80         0.0.0.0:*          LISTEN   1141/nginx

php测试:

[root@server2 conf.d]# cd /usr/share/nginx/html/[root@server2 html]# vim index.php[root@server2 html]# cat index.php <!--phpphpinfo()-->[root@server2 html]# /etc/init.d/php-fpm reloadReloading php-fpm: [14-Jul-2018 01:09:13] NOTICE: configuration file /etc/php-fpm.conf test is successful      [ OK ]

物理机访问:

4、php配置redis+mysql

[root@server2 ~]# cd /usr/share/nginx/html/[root@server2 html]# vim test.php   <!--php      $redis = new Redis();      $redis--->connect('172.25.254.1',6379) or die ("could net connect redi  s server");   #   $query = "select * from test limit 9";      $query = "select * from test";      for ($key = 1; $key < 10; $key++)      {          if (!$redis->get($key))          { $connect = mysql_connect('172.25.254.3','redis','wes  tos'); mysql_select_db(test); $result = mysql_query($query); //如果没有找到$key,就将该查询sql的结果缓存到redis while ($row = mysql_fetch_assoc($result)) {     $redis->set($row['id'],$row['name']); } $myserver = 'mysql'; break;         }         else         { $myserver = "redis"; $data[$key] = $redis->get($key);         }     }     echo $myserver;     echo "";     for ($key = 1; $key < 10; $key++)     {        echo "number is $key";        echo "";        echo "name is $data[$key]"  ;        echo "";   }>

5、添加php支持的redis模块

[root@server2 ~]# unzip phpredis-master.zip [root@server2 ~]# cd phpredis-master[root@server2 phpredis-master]# phpize Configuring for:PHP Api Version:     20090626Zend Module Api No:   20090626Zend Extension Api No:  220090626[root@server2 phpredis-master]# lsacinclude.m4  config.sub   library.c     README.markdownaclocal.m4   configure    library.h     redis.cautom4te.cache configure.in  ltmain.sh     redis_session.cbuild      CREDITS     Makefile.global  redis_session.hcommon.h    debian     missing      run-tests.phpconfig.guess  debian.control mkdeb-apache2.sh serialize.listconfig.h.in   igbinary    mkinstalldirs   testsconfig.m4    install-sh   php_redis.h[root@server2 phpredis-master]# ./configure [root@server2 phpredis-master]# make && make install [root@server2 ~]# cd /etc/php.d/[root@server2 php.d]# lscurl.ini   json.ini   mysql.ini   pdo_sqlite.ini zip.inifileinfo.ini mbstring.ini pdo.ini    phar.inigd.ini    mysqli.ini  pdo_mysql.ini sqlite3.ini[root@server2 php.d]# cp mysql.ini redis.ini[root@server2 php.d]# vim redis.ini  2 extension=redis.so [root@server2 php.d]# /etc/init.d/php-fpm reloadReloading php-fpm: [14-Jul-2018 01:21:56] NOTICE: configuration file /etc/php-fpm.conf test is successful      [ OK ][root@server2 php.d]# php -m |grep redisredisserver3:mysql配置

1、安装mysql-server

[root@server3 ~]# rpm -qa | grep mysqlmysql-community-common-5.7.17-1.el6.x86_64mysql-community-client-5.7.17-1.el6.x86_64mysql-community-libs-compat-5.7.17-1.el6.x86_64mha4mysql-node-0.56-0.el6.noarchmysql-community-libs-5.7.17-1.el6.x86_64mysql-community-server-5.7.17-1.el6.x86_64[root@server3 ~]# rpm -e `rpm -qa|grep mysql` --nodeps  ##不考虑依赖性删除mysqlwarning: /etc/my.cnf saved as /etc/my.cnf.rpmsave[root@server3 ~]# rpm -qa | grep mysql[root@server3 ~]# cd /var/lib/mysql/[root@server3 mysql]# rm -fr *[root@server3 mysql]# ls[root@server3 mysql]# yum install -y mysql-server ##安装

2、开启mysql,并导入测试数据库

[root@server3 ~]# /etc/init.d/mysqld start[root@server3 ~]# mysql < test.sql[root@server3 ~]# mysql < test.sql [root@server3 ~]# cat test.sql use test;CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');#DELIMITER $$#CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN#  SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`)); # END$$#DELIMITER ;

3、数据库授权

[root@server3 ~]# mysqlmysql> grant all on test.* to redis@'%' identified by 'westos';Query OK, 0 rows affected (0.00 sec)mysql> select * from test.test;+----+-------+| id | name |+----+-------+| 1 | test1 || 2 | test2 || 3 | test3 || 4 | test4 || 5 | test5 || 6 | test6 || 7 | test7 || 8 | test8 || 9 | test9 |+----+-------+9 rows in set (0.00 sec)

测试:访问172.25.254.2/test.php

1、php默认从redis 索取数据,第一次redis无缓存,则php从mysql'索取数据

第一次无缓存

第二次索取数据后:

redis节点也可查看

[root@server1 redis-4.0.1]# redis-cli127.0.0.1:6379> get 2"test2"

2、将数据库server3节点内容更新并删除节点,则php从数据库索取数据节点更新内容

mysql> update test.test set name='westos' where id=1;Query OK, 1 row affected (0.05 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from test.test;+----+--------+| id | name  |+----+--------+| 1 | westos || 2 | test2 || 3 | test3 || 4 | test4 || 5 | test5 || 6 | test6 || 7 | test7 || 8 | test8 || 9 | test9 |+----+--------+9 rows in set (0.00 sec)

redis的master主机删除节点内容

[root@server1 redis-4.0.1]# redis-cli127.0.0.1:6379> get 2"test2"127.0.0.1:6379> del 1(integer) 1127.0.0.1:6379> get 1(nil)

刷新页面,再次访问

以上redis 作为 mysql 的缓存服务器,但是如果更新了 mysql,redis中仍然会有对应的 KEY,数据就不会更新,此时就会出现 mysql 和 redis 数据不一致的情况。

总结

以上所述是小编给大家介绍的redis服务器环境下mysql实现lnmp架构缓存,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!


  • 上一条:
    Centos下配置Redis开机启动脚本
    下一条:
    php监听redis key失效触发回调事件
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Redis中能实现的功能、常见应用介绍(0个评论)
    • 2024年Redis面试题之一(0个评论)
    • 在redis缓存常见出错及解决方案(0个评论)
    • 在redis中三种特殊数据类型:地理位置、基数(cardinality)估计、位图(Bitmap)使用场景介绍浅析(2个评论)
    • Redis 删除 key用 del 和 unlink 有啥区别?(1个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-12
    • 2020-03
    • 2020-05
    • 2021-04
    • 2022-03
    • 2022-05
    • 2022-08
    • 2023-02
    • 2023-04
    • 2023-07
    • 2024-01
    • 2024-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客