首页 > 作文

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

更新时间:2023-04-08 20:47:15 阅读: 评论:0

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

1. 在rver1中配置nginx2. 在rver1中配置php3. 在rver2中配置redis4. 在rver3中配置数据库5. 测试6. 存在的问题

1. 在rver1中配置nginx

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

yum install psmisc-22.20-11.el7.x86_64 -ykillall redis-rver

step2 编译安装nginx:

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

step3 配置并启动nginx:

c巨蚁d /usr/local/nginx/ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/#制作软连接cd conf/vim nginx.conf修改以下内容:  2 ur  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         }uradd -u 900 nginxnginx#启动netstat -antlp

2. 在rver1中配置php

安装后启动:

systemctl start php-fpmnetstat -antlp

3. 在rver2中配置redis

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

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

测试redis:可以正常使用

redis-cli127.0.0.1:6379> get name"nigar"127.0.0.1:6379&g生日祝福信息t; DEL name(integer) 1127.0.0.1:6379> get name(nil)

4. 在rver3中配置数据库

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

授权用户:

mysql -uroot -p123#登陆数据库MariaDB [(none)]> create databa test;Query OK, 1 row affected (0.00 c)MariaDB [(none)]> grant all on test.* to redis@'%' identified by '123';Query OK, 0 rows affected (0.00 c)MariaDB [(none)]> flush privileges;Query OK, 0 rows affected (0.00 c)

配置测试页面:

在rver1中:

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 rver");#此处ip为安装redis的虚拟机ip(rver2)  # $query = "lect * from test limit 9";        $query = "lect * from test";        for ($key = 1; $key < 10; $key++)        {                if (!$redis->get($key))                {                        $connect = mysql_connect('172.25.254.3','redis','123');#此处ip为安装mariadb的虚拟机ip(rver3);密码为数据库授权的用户密码                        mysql_lect_db(test);                        $result = mysql_query激情的歌有哪些($query);                        //如果没有找到$key,就将该查询sql的结果缓存到redis                        while ($row = mysql_fet卧室风水ch_assoc($result))                        {                                $redis->t($row['id'],$row['name']);                        }                        $myrver = 'mysql';                        break;                }                el                {                        $myrver = "redis";                        $data[$key] = $redis->get($key);                }        }         echo $myrver;        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>";        }?>

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

vim test.sql u 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 < teit运维方案st.sql#导入数据

5. 测试

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

6. 存在的问题

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

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

MariaDB [(none)]> u test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADataba changedMariaDB [test]> update test t name='haha' where id=1;Query OK, 1 row affected (0.00 c)Rows matched: 1  Changed: 1  Warnings: 0MariaDB [test]> lect * from test;

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

redis-cli127.0.0.1:6379> get 1#并没有修改为haha"test1"127.0.0.1:6379> t 1 haha#手动修改为hahaOK127.0.0.1:6379> get 1"haha"

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

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

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

本文发布于:2023-04-08 20:47:13,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/0451ef8e861fd697290cd41ff38c70c2.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Redis结合lnmp架构做mysql的缓存服务器.doc

本文 PDF 下载地址:Redis结合lnmp架构做mysql的缓存服务器.pdf

标签:数据   测试   数据库   如果没有
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图