实例项目搭建-redis服务器环境下mysql实现la/nmp架构缓存

目录

1.流程图

2.场景实例搭建

2.1 场景实例搭建步骤

2.1.1 PHP安装:(web服务器安装)

2.1.2 安装php的redis扩展(web服务器安装)

2.1.3 vim修改/etc/php.ini,添加redis的扩展

2.1.4 重启web(http)服务器

 2.1.5 在httpd的发布目录下编辑如下php页面,然后访问

2.2.6 安装redis(在redis服务器上操作)

2.2.7 配置文件并启动redis

2.2.8 编写php的测试代码(在web服务器)

2.2.9 配置mysql(在mysql服务器)

2.2.10 测试mysql是否连接(在web服务器)

 2.2.11 在mysql创建测试数据

3. 测试验证


1.流程图

2.场景实例搭建

环境规划

ip地址环境
192.168.150.19redis
192.168.150.20web
192.168.150.21mysql

​​关闭防火墙

systemctl stop firewalld

systemctl disable firewalld

sed -i 's/enforcing/disabled/' /etc/selinux/config

setenforce 0

同步时间

yum -y install ntp ntpdate

ntpdate cn.pool.ntp.org

hwclock --systohc

2.1 场景实例搭建步骤

2.1.1 PHP安装:(web服务器安装)

yum install php php-fpm php-cli php-common php-gd php-mbstring php-mysql php-pdo php-devel php -xmlrpc php-xml php-bcmath php-dba php-enchant -y

2.1.2 安装phpredis扩展(web服务器安装)

wget http://pecl.php.net/get/redis-2.2.7.tgz

tar -zxvf redis-2.2.7.tgz  
cd redis-2.2.7/
phpize
./configure
make install
phpize php 进行添加扩展。并且 phpize 编译的扩展库可以随时启用或停用,比较灵活。
编译完成后可以看到安装的目录如下,进入后可以看到编译的模块 redis.so

2.1.3 vim修改/etc/php.ini,添加redis的扩展

2.1.4 重启web(http)服务器

systemctl restart httpd

 2.1.5 httpd的发布目录下编辑如下php页面,然后访问

[root@192 html]# cat index.php
<?php
phpinfo();
?>

注意:找到redis才算ok

2.2.6 安装redis(在redis服务器上操作)

wget -c -t 0 http://download.redis.io/releases/redis-2.8.19.tar.gz

 tar -xvf redis-2.8.19.tar.gz

 make

2.2.7 配置文件并启动redis

修改redis.confdaemonize no改为yes  

 启动redis

src/redis-server redis.conf

2.2.8 编写php的测试代码(在web服务器)

vim redis.php

<?php
        $redis = new Redis();
        $redis->connect('192.168.150.19',6379) or die ("could net connect redis server");   ## 这里面ip 是redis服务器ip
#       $query = "select * from testab limit 5";
        $query = "select * from testab"; ##这里改成自己mysql存在的表
for ($key = 1; $key < 10; $key++)
{
        if (!$redis->get($key))
        {
                $connect = mysql_connect('192.168.150.21','redis','Zcf@201104');  ##mysql的ip
                mysql_select_db(testab);  
                $result = mysql_query($query);
                $arr = [];
                while ($row = mysql_fetch_assoc($result))
                {
                        $redis->setex($row['id'],10,$row['name']);
                        $arr[] = $row;
                }
                $myserver = 'mysql';
                $data = $arr;
                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>";
        if ($myserver == "mysql") {
                $arr2 = array_map('end',$data);
                echo "name is <b><font color=#FF0000>$arr2[$key]</font></b>";
                echo "<br>";
        }
        else {
        echo "name is <b><font color=#FF0000>$data[$key]</font></b>";
        echo "<br>";
        }
}

2.2.9 配置mysql(在mysql服务器)

在mysql服务器创建用户redis

create user 'redis'@'%' indentified by 'Zcf@201104'

alter user 'redis'@'%' indentified with mysql_native_password by 'Zcf@201104' 

flush privileges

2.2.10 测试mysql是否连接(在web服务器)

vim mysql.php

<?php
$link=mysql_connect('192.168.150.21','redis','Zcf@201104');
if($link) echo "Success!!";
else echo "Fail!!";
mysql_close();
?>

 2.2.11 在mysql创建测试数据


mysql> select * from testab;
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | tom1 |
|    3 | tom2 |
|    4 | tom3 |
|    5 | tom4 |
|    6 | tom5 |
|    7 | tom6 |
|    8 | tom7 |
|    9 | tom8 |
|   10 | tom9 |
+------+------+
10 rows in set (0.00 sec)

3. 测试验证

 第一次访问,redis中没有对应的KEY值

 刷新,再次访问,此时redis中已有相关数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值