Redis——结合lnmp架构做mysql的缓存服务器

  • 1. 在server1中配置nginx
  • 2. 在server1中配置php
  • 3. 在server2中配置redis
  • 4. 在server3中配置数据库
  • 5. 测试
  • 6. 存在的问题

1. 在server1中配置nginx

step1 关闭之前做的redis(如果没有做过可忽略):

yum install psmisc-22.20-11.el7.x86_64 -y
killall redis-server

step2 编译安装nginx:

tar zxf nginx-1.16.1.tar.gz 
cd nginx-1.16.1
yum install gcc pcre-devel zlib-devel -y
./configure --prefix=/usr/local/nginx
make && make install

step3 配置并启动nginx:

cd /usr/local/nginx/
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/	#制作软连接
cd conf/
vim nginx.conf
修改以下内容:
  2 user  nginx nginx;

 43         location / {
 44             root   html;
 45             index  index.php index.html index.htm;
 46         }

 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
 70             include        fastcgi.conf;
 71         }


useradd -u 900 nginx
nginx	#启动
netstat -antlp

2. 在server1中配置php

安装后启动:

systemctl start php-fpm
netstat -antlp

3. 在server2中配置redis

注意:因为在之前的博客中server2做过redis的主从复制,所以此处还原实验环境

/etc/init.d/redis_6379 stop
vim /etc/redis/6379.conf	#删除最后一行(1379 slaveof 172.25.254.1 6379)
/etc/init.d/redis_6379 start

测试redis:可以正常使用

redis-cli
127.0.0.1:6379> get name
"nigar"
127.0.0.1:6379> DEL name
(integer) 1
127.0.0.1:6379> get name
(nil)

4. 在server3中配置数据库

rpm -qa | grep mysql	#查看之前是否安装过mysql,如果安装过要卸载
yum install -y mariadb-server	#实验不需要用mysql,安装mariadb即可
systemctl start mariadb
mysql_secure_installation	#安全初始化

授权用户:

mysql -uroot -p123	#登陆数据库
MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on test.* to redis@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

配置测试页面:

在server1中:

cd /usr/local/nginx/html/
vim index.php

<?php
        $redis = new Redis();
        $redis->connect('172.25.254.2',6379) or die ("could net connect redis server");	#此处ip为安装redis的虚拟机ip(server2)
  # $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','123');	#此处ip为安装mariadb的虚拟机ip(server3);密码为数据库授权的用户密码
                        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 "<br>";
        for ($key = 1; $key < 10; $key++)
        {
                echo "number is <b><font color=#FF0000>$key</font></b>";
 
                echo "<br>";
 
                echo "name is <b><font color=#FF0000>$data[$key]</font></b>";
 
                echo "<br>";
        }
?>

为server3上的mysql的test库加入一些数据:

vim 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');

mysql -p123 < test.sql	#导入数据

5. 测试

浏览器访问172.25.254.1
进入时查看到数据是从mysql取到的;刷新后数据从redis中取,再刷新也是redis

6. 存在的问题

如果此时 mysql 数据发生变更,redis会同步吗?

测试:
修改数据库的数据信息:

MariaDB [(none)]> 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
MariaDB [test]> update test set name='haha' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [test]> select * from test;

在刚才的测试页面查看,并没有更新数据信息:

redis-cli
127.0.0.1:6379> get 1	#并没有修改为haha
"test1"
127.0.0.1:6379> set 1 haha	#手动修改为haha
OK
127.0.0.1:6379> get 1
"haha"

此时在刚才的测试页面中查看,数据才更新了。

这是一个很大的弊端,我们必须时刻查看数据库的数据是否有更新,再手动在redis上更新,这显然不符合企业要求。那么这个问题该如何解决呢?需要用到gearmand,具体用法在下一篇博文中介绍

本文地址:https://blog.csdn.net/weixin_45775963/article/details/104845931