redis做缓存手动更新

验主机3台:
server11:redis缓存服务器
server12:mysql服务器:client
serever13:ngnx+php-fpm,gearmand server,worker
server11:

首先安装redis服务器

server13:

rpm包安装nginx+php-fpm

vim /etc/php.ini
946 date.timezone = Asia/Shanghai

vim  /etc/php-fpm.d/www.conf
#修改用户和组为nginx,原因是查看/etc/nginx/nginx.conf文件中记录了nginx默认以nginx用户身份启动
39 user = nginx
40 ; RPM: Keep a group allowed to write in log dir.
41 group = nginx

vim /etc/nginx/conf.d/default.conf 
8     location / {
9         root   /usr/share/nginx/html; 10         index  index.php index.html index.htm;        #添加php首页
 11     }


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     }

 yum install -y unzip  
 unzip phpredis-master.zip
 cd phpredis-master         
 yum install -y gcc
 phpize
./configure 
 make & make install 
 #此时可以生成redis.so,即redis动态连接库
 cd /usr/lib64/php/modules/    #默认在此目录下



cd /usr/share/nginx/html/
vim test.php
<?php
        $redis = new Redis();
        $redis->connect('172.25.92.11',6379) or die ("could net connect redis server");       #指定redis服务器
  #      $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.92.12','redis','westos');  #设定登陆后端mysql服务器的用户和密码
                        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);
                }
        }


cd /etc/php.d/
cp mysql.ini redis.ini
vim redis.ini
extension=redis.so                 #添加redis缓存库,php默认会读取/etc/php.d/下的所有.ini结尾的文件 

 /etc/init.d/nginx start
/etc/init.d/php-fpm start

 server12:

yum install -y mysql-sesrver
/etc/init.d/mysqld start
mysql_secure_installation 
mysql -phello
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on test.* to redis@'%' identified by 'westos';   #根据test.php文件内容授权用户
Query OK, 0 rows affected (0.00 sec)

mysql < test.sql -phello

#test.sql文件内容  

[root@server12 ~]# 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 ;


mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from test;
+----+-------+
| id | name  |
+----+-------+
|  1 | test1 |
|  2 | test2 |
|  3 | test3 |
|  4 | test4 |
|  5 | test5 |
|  6 | test6 |
|  7 | test7 |
|  8 | test8 |
|  9 | test9 |
+----+-------+

测试:手动更新redis

浏览器:
172.25.92.13/test.php
会显示test库中的内容。
在server12上手动做数据库更新:

mysql> update test set name='westos' where id=1;
Query OK, 1 row affected (0.10 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | westos |
|  2 | test2  |
|  3 | test3  |
|  4 | test4  |
|  5 | test5  |
|  6 | test6  |
|  7 | test7  |
|  8 | test8  |
|  9 | test9  |
+----+--------+

在浏览器再次访问时发现没有变化,原因是因为redis缓存的存在。
server11上:删除id1的缓存:

root@server11 ~]# 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
(integer) 1
127.0.0.1:6379> get 1
(nil)

再次在浏览器上访问: 

 这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42167918/article/details/81603277