Linux企业运维---redis1

redis软件下载地址:Redis

redis支持的数据类型:

• 字符串(strings)
• 散列(hashes)
• 列表(lists)
• 集合(sets)
• 有序集合(sorted sets)

redis常用命令:

config get * 查看配置
select 1 选择数据库
flushdb 清空当前数据库
flushall 清空所有数据库
move key 1 移动key
del key 删除
rename oldkey newkey 改名
expire key 10 设置过期时间
persist key 设置持久化
keys user* 查询
exists key 判断是否存在

Redis主从复制的基本情况

1、Redis主从复制的概念

主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器。前者称为主节点(Master),后者称为从节点(Slave);数据的复制是单向的,只能由主节点到从节点。

默认情况下,每台Redis服务器都是主节点;且一个主节点可以有多个从节点(或没有从节点),但一个从节点只能有一个主节点。

2、Redis主从复制的作用

●数据冗余:主从复制实现了数据的热备份,是持久化之外的一种数据冗余方式。

●故障恢复:当主节点出现问题时,可以由从节点提供服务,实现快速的故障恢复;实际上是一种服务的冗余。

●负载均衡:在主从复制的基础上,配合读写分离,可以由主节点提供写服务,由从节点提供读服务(即写Redis数据时应用连接主节点,读Redis数据时应用连接从节点),分担服务器负载;尤其是在写少读多的场景下,通过多个从节点分担读负载,可以大大提高Redis服务器的并发量。

●高可用基石:除了上述作用以外,主从复制还是哨兵和集群能够实施的基础,因此说主从复制是Redis高可用的基础。

3、Redis主从复制的流程

●【1】若启动一个Slave机器进程,则它会向Master机器发送一个“sync command”命令,请求同步连接。

●【2】无论是第一次连接还是重新连接,Master机器都会启动一个后台进程,将数据快照保存到数据文件中(执行rdb操作),同时Master还会记录修改数据的所有命令并缓存在数据文件中。

●【3】后台进程完成缓存操作之后,Maste机器就会向Slave机器发送数据文件,Slave端机器将数据文件保存到硬盘上,然后将其加载到内存中,接着Master机器就会将修改数据的所有操作一并发送给Slave端机器。若Slave出现故障导致宕机,则恢复正常后会自动重新连接

●【4】Master机器收到Slave端机器的连接后,将其完整的数据文件发送给Slave端机器,如果Mater同时收到多个Slave发来的同步请求,则Master会在后台启动一个进程以保存数据文件,然后将其发送给所有的Slave端机器,确保所有的Slave端机器都正常。

二、redis主从复制搭建

实验环境:

主机名称

ip

角色

server1 172.25.10.1 master
server2 172.25.10.2 slave
server3 172.25.10.3 slave

 实验步骤:

在server1中操作

[root@server1 ~]# ls

[root@server1 ~]# tar zxf redis-6.2.4.tar.gz
[root@server1 ~]# ls
dump.sql                   nginx-1.20.1                      redis-6.2.4
mysql-5.7.31               nginx-1.20.1.tar.gz               redis-6.2.4.tar.gz
mysql-boost-5.7.31.tar.gz  php-fpm-5.4.16-46.el7.x86_64.rpm
[root@server1 ~]# cd  redis-6.2.4
[root@server1 redis-6.2.4]# ls
00-RELEASENOTES  COPYING   MANIFESTO   runtest-cluster    src
BUGS             deps      README.md   runtest-moduleapi  tests
CONDUCT          INSTALL   redis.conf  runtest-sentinel   TLS.md
CONTRIBUTING     Makefile  runtest     sentinel.conf      utils
[root@server1 redis-6.2.4]# make && make install

[root@server1 redis-6.2.4]# cd utils/
[root@server1 utils]# ls

[root@server1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server

This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
[root@server1 utils]# vim install_server.sh

78 #if [ "${_pid_1_exe##*/}" = systemd ]
 79 #then
 80 #       echo "This systems seems to use systemd."
 81 #       echo "Please take a look at the provided example service unit files     in this directory, and adapt and install them. Sorry!"
 82 #       exit 1
 83 #fi      注释

[root@server1 utils]# ./install_server.sh

/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!  -----------------> 成功

 [root@server1 utils]# cd /etc/redis/
[root@server1 redis]# ls
6379.conf  sentinel.conf
[root@server1 redis]# vim 6379.conf

 76 bind 0.0.0.0

root@server1 redis]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

[root@server1 redis]# netstat -antlp

tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      7971/redis-server 0

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3315/master    

 

 测试:

 [root@server1 redis]# cd
[root@server1 ~]# cd redis-6.2.4/
[root@server1 redis-6.2.4]# ls

[root@server1 redis-6.2.4]# redis-cli     登陆
127.0.0.1:6379> set name westos
OK                                                              
127.0.0.1:6379> get name
"westos"

127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> get name           server1为master 可以读写
(nil)

127.0.0.1:6379> set name westos
OK
127.0.0.1:6379> EXPIRE name 5               5秒后后过期
(integer) 1
127.0.0.1:6379> get name
"westos"
127.0.0.1:6379> get name
"westos"
127.0.0.1:6379> get name
(nil)


在server2中操作:

[root@server2 ~]# ls
dump.sql  redis-6.2.4.tar.gz
[root@server2 ~]# tar zxf redis-6.2.4.tar.gz
[root@server2 ~]# ls
dump.sql  redis-6.2.4  redis-6.2.4.tar.gz
[root@server2 ~]# cd redis-6.2.4/
[root@server2 redis-6.2.4]# make && make install

[root@server2 ~]# ls
dump.sql  redis-6.2.4.tar.gz
[root@server2 ~]# tar zxf redis-6.2.4.tar.gz
[root@server2 ~]# ls
dump.sql  redis-6.2.4  redis-6.2.4.tar.gz
[root@server2 ~]# cd redis-6.2.4/
[root@server2 redis-6.2.4]# make && make install

[root@server2 utils]# ls
build-static-symbols.tcl  redis-copy.rb
cluster_fail_time.tcl     redis_init_script
corrupt_rdb.c             redis_init_script.tpl
create-cluster            redis-sha1.rb
generate-command-help.rb  releasetools
gen-test-certs.sh         speed-regression.tcl
graphs                    srandmember
hashtable                 [email protected]
hyperloglog               systemd-redis_server.service
install_server.sh         tracking_collisions.c
lru                       whatisdoing.sh
[root@server2 utils]# vim install_server.sh

78 #if [ "${_pid_1_exe##*/}" = systemd ]
 79 #then
 80 #       echo "This systems seems to use systemd."
 81 #       echo "Please take a look at the provided example service unit files     in this directory, and adapt and install them. Sorry!"
 82 #       exit 1
 83 #fi     注释

[root@server2 utils]# ./install_server.sh

Installation successful!----------------->成功

[root@server2 utils]# cd /etc/redis/
[root@server2 redis]# ls
6379.conf
[root@server2 redis]# vim 6379.conf

76 bind 0.0.0.0
479 replicaof 172.25.10.1 6379

[root@server2 redis]#  /etc/init.d/redis_6379 restart    
Stopping ...
Redis stopped
Starting Redis server...
[root@server2 redis]# netstat -antlp
  
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      8139/redis-server 0      
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3287/master         

 

 

 [root@server2 redis]# redis-cli
127.0.0.1:6379> get name
"westos"
127.0.0.1:6379> set name name
(error) READONLY You can't write against a read only replica.   server2为slave 只能查看,不能设置
127.0.0.1:6379>


测试:

[root@server1 redis-6.2.4]# redis-cli     能读写

127.0.0.1:6379> set name redhat
OK
127.0.0.1:6379> get name
"redhat"
127.0.0.1:6379> set user1 lee
OK
127.0.0.1:6379> set user2 linux
OK

127.0.0.1:6379> quit

[root@server2 redis]# redis-cli              只能读
127.0.0.1:6379> get name
"redhat" 
127.0.0.1:6379> get user1
"lee"
127.0.0.1:6379> get user2
"linux"
127.0.0.1:6379> set name westos
(error) READONLY You can't write against a read only replica.
127.0.0.1:6379> quit

 [root@server1 redis-6.2.4]#  ps aux | grep redis
root      7971  0.2  0.4 164960  9788 ?        Ssl  10:34   0:09 /usr/local/bin/redis-server 0.0.0.0:6379
root      8278  0.0  0.0 112708   968 pts/0    S+   11:31   0:00 grep --color=auto redis
[root@server1 redis-6.2.4]# /etc/init.d/redis_6379 stop      ##停止
Stopping ...
Redis stopped
[root@server1 redis-6.2.4]# vim /etc/redis/6379.conf
[root@server1 redis-6.2.4]# cat /var/log/redis_6379.log      ##查看日志

14780:M 17 Dec 2021 11:04:22.644 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
14780:M 17 Dec 2021 11:04:22.645 * Loading RDB produced by version 6.2.4

---------------------------------------------------------------------------------------------------------------------------

[root@server2 redis]# cd /usr/local/
[root@server2 local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@server2 local]# cd bin/
[root@server2 bin]# ls
redis-benchmark  redis-check-rdb  redis-sentinel
redis-check-aof  redis-cli        redis-server
[root@server2 bin]# ll
total 18876
-rwxr-xr-x 1 root root 4829576 Dec 19 10:49 redis-benchmark
lrwxrwxrwx 1 root root      12 Dec 19 10:49 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root      12 Dec 19 10:49 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 5002832 Dec 19 10:49 redis-cli
lrwxrwxrwx 1 root root      12 Dec 19 10:49 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 9486672 Dec 19 10:49 redis-serverredis


redis主从切换

 搭建server3

[root@server3 ~]# ls
redis-6.2.4.tar.gz  test.sql
[root@server3 ~]# tar zxf redis-6.2.4.tar.gz
[root@server3 ~]# ls
redis-6.2.4  redis-6.2.4.tar.gz  test.sql
[root@server3 ~]# cd redis-6.2.4/
[root@server3 redis-6.2.4]# ls
[root@server3 redis-6.2.4]# make && make install

[root@server3 redis-6.2.4]# cd utils/
[root@server3 utils]#  vim install_server.sh

78 #if [ "${_pid_1_exe##*/}" = systemd ]
 79 #then
 80 #       echo "This systems seems to use systemd."
 81 #       echo "Please take a look at the provided example service unit f    iles in this directory, and adapt and install them. Sorry!"
 82 #       exit 1
 83 #fi

[root@server3 utils]# ./install_server.sh

Installation successful!------------>成功

[root@server3 ~]# cd /etc/redis/

root@server3 utils]# vim /etc/redis/6379.conf

76 bind 0.0.0.0
479 replicaof 172.25.10.1 6379

 

[root@server3 utils]#  /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
[root@server3 utils]# ps ax | grep redis
 8687 ?        Ssl    0:00 /usr/local/bin/redis-server 0.0.0.0:6379
 8693 pts/0    S+     0:00 grep --color=auto redis

[root@server3 utils]# netstat -antlp
           tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      8687/redis-server 0     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3310/master    
     



 测试:

[root@server1 redis-6.2.4]# redis-cli
127.0.0.1:6379> info
# Replication
role:master
connected_slaves:2

slave0:ip=172.25.10.3,port=6379,state=online,offset=42,lag=0
slave1:ip=172.25.10.2,port=6379,state=online,offset=42,lag=0

[root@server2 bin]# redis-cli
127.0.0.1:6379> info
# Server

# Replication
role:slave
master_host:172.25.10.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0

[root@server3 utils]# redis-cli
127.0.0.1:6379> info
# Server

# Replication
role:slave
master_host:172.25.10.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0


redis自动故障迁移 (配置Sentinel)

[root@server1 redis-6.2.4]# ls
[root@server1 redis-6.2.4]# cp sentinel.conf /etc/redis/
[root@server1 redis-6.2.4]# cd /etc/redis/
[root@server1 redis]# ls
6379.conf  sentinel.conf
[root@server1 redis]# vim sentinel.conf
84 sentinel monitor mymaster 172.25.10.1 6379 2
125 sentinel down-after-milliseconds mymaster 10000

 

[root@server1 redis]# scp sentinel.conf server2:/etc/redis/  
[root@server1 redis]# scp sentinel.conf server3:/etc/redis/   必须在启动之前发送,一旦启动文件会发生变化
[root@server2 redis]# redis-sentinel /etc/redis/sentinel.conf 

[root@server2 redis]# redis-sentinel /etc/redis/sentinel.conf

  [root@server3 redis]# redis-sentinel /etc/redis/sentinel.con

将server1停掉时:

[root@server1 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN
not connected> exit
[root@server1 ~]# ps ax | grep redis
 8451 ?        Ssl    0:06 redis-sentinel *:26379 [sentinel]
 8662 pts/1    S+     0:00 grep --color=auto redis
[root@server1 ~]#
然后去看监控:------>可以看到master变了,server2经过投票变为新的master

 

将server1重新启动后

[root@server1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...

[root@server1 ~]#  ps ax | grep redis
 3704 ?        Sl     0:11 redis-sentinel *:26379 [sentinel]
 3743 ?        Ssl    0:00 /usr/local/bin/redis-server 0.0.0.0:6379
 3750 pts/1    S+     0:00 grep --color=auto redis
[root@server1 ~]# redis-cli        ###将server1启动之后,会自动变成slave端连接在server2上
127.0.0.1:6379> info
# Server
redis_version:6.2.4

redis集群

[root@server1 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@server1 ~]# cd
[root@server1 ~]# cd redis-6.2.4/
[root@server1 redis-6.2.4]# ls
[root@server1 redis-6.2.4]# cd utils/
[root@server1 utils]#  cd create-cluster/
[root@server1 create-cluster]# ls
create-cluster  README
[root@server1 create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006

[root@server1 create-cluster]# pwd
/root/redis-6.2.4/utils/create-cluster
[root@server1 create-cluster]# ls
30001.log  30006.log             appendonly-30005.aof  nodes-30003.conf
30002.log  appendonly-30001.aof  appendonly-30006.aof  nodes-30004.conf
30003.log  appendonly-30002.aof  create-cluster        nodes-30005.conf
30004.log  appendonly-30003.aof  nodes-30001.conf      nodes-30006.conf
30005.log  appendonly-30004.aof  nodes-30002.conf      README
[root@server1 create-cluster]# cat 30001.log        查看日志

795:M 09 Mar 2022 16:11:49.973 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

[root@server1 create-cluster]# sysctl -w vm.overcommit_memory=1
vm.overcommit_memory = 1

 [root@server1 create-cluster]# cat appendonly-30001.aof

[root@server1 create-cluster]# ./create-cluster create

[root@server1 create-cluster]# redis-cli --cluster check 127.0.0.1:30001

 生成三组,30005对应的master为30002,30004对应的masterw为30001,30006dui对应的master为30002

[root@server1 create-cluster]# redis-cli -c -p 30001
127.0.0.1:30001> get name
-> Redirected to slot [5798] located at 127.0.0.1:30002
(nil)
127.0.0.1:30002> set name westos
OK
127.0.0.1:30002> get name
"westos"

[root@server1 create-cluster]# redis-cli -c -p 30004
127.0.0.1:30004> get name
-> Redirected to slot [5798] located at 127.0.0.1:30002
"westos"

127.0.0.1:30002> info

[root@server1 create-cluster]# redis-cli -c -p 30002
127.0.0.1:30002> get name
"redhat"
127.0.0.1:30002> SHUTDOWN
not connected> exit
[root@server1 create-cluster]# ps ax

[root@server1 create-cluster]# ./create-cluster start

[root@server1 create-cluster]# redis-cli --cluster check 127.0.0.1:30003   ##可以看到30005变成master

 将5和2都SHUTDOWN:
[root@server1 create-cluster]# redis-cli -c -p 30005
127.0.0.1:30005> SHUTDOWN
not connected> exit
[root@server1 create-cluster]# redis-cli -c -p 30002
127.0.0.1:30002> SHUTDOWN
not connected> exit

 [root@server1 create-cluster]# ps ax

[root@server1 create-cluster]# redis-cli --cluster check 127.0.0.1:30003

测试:一组出问题------->全部崩

解决:重新开启

注意:哈西槽一定要够,否则会崩盘,删除一组就崩盘

 

配置文件改为8就一次产生8个  

 [root@server1 create-cluster]# vim create-cluster
[root@server1 create-cluster]# ./create-cluster start

 [root@server1 create-cluster]# ps ax | grep redis

 [root@server1 create-cluster]# redis-cli --cluster add-node 127.0.0.1:3007 127.0.0.1:30001    add-node添加节点        127.0.0.1:30001 节点入口

[root@server1 create-cluster]# redis-cli --cluster check 127.0.0.1:30003

 

[root@server1 create-cluster]# redis-cli --cluster add-node 127.0.0.1:30008 127.0.0.1:30001 --cluster-slave --cluster-master-id 34b1256184e68d9fae7e40495b6892606d8f46b3

[root@server1 create-cluster]# redis-cli --cluster check 127.0.0.1:30003

 给30007分配哈西槽:

 [root@server1 create-cluster]# redis-cli --cluster reshard 127.0.0.1:30003

How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID? 34b1256184e68d9fae7e40495b6892606d8f46b3
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: all

 [root@server1 create-cluster]# redis-cli --cluster check 127.0.0.1:30003  ###去测试添加是否成功


PHP与redis连接

1、环境配置

主机名称

ip

角色

server1 172.25.10.2 nginx+php
server2 172.25.10.4 mysql
server3 172.25.10.3 redis

[root@server1 create-cluster]# ./create-cluster stop

[root@server2 redis]# /etc/init.d/redis_6379 stop

留下server3(master)

安装mariadb

[root@server4 local]# yum install  -y mariadb-server
[root@server4 local]# systemctl start mariadb.service
[root@server4 local]# cd /var/lib/mysql/
[root@server4 mysql]# mysql

 安装nginx

 [root@westos_student73 Desktop]# scp nginx-1.20.2.tar.gz [email protected]:/root/

[root@server2 ~]# ls
dump.sql             php-fpm-5.4.16-46.el7.x86_64.rpm  redis-6.2.4.tar.gz
nginx-1.20.2.tar.gz  redis-6.2.4

[root@server1 ~]# tar zxf nginx-1.20.2.tar.gz
[root@server1 ~]# cd nginx-1.20.2/
[root@server1 nginx-1.20.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@server1 nginx-1.20.2]# yum install -y gcc pcre-devel zlib-devel openssl-devel

[root@server1 nginx-1.20.2]# ./configure --with-http_ssl_module

[root@server2 nginx-1.20.2]# make && make install
[root@server2 nginx-1.20.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@server2 nginx-1.20.2]# which nginx
/usr/local/bin/nginx

[root@server2 conf]# useradd -s /sbin/nologin -M -d /user/local/nginx nginx

[root@server2 conf]# id nginx
uid=1002(nginx) gid=1002(nginx) groups=1002(nginx)
[root@server2conf]# ls
fastcgi.conf    koi-utf  mime.types  scgi_params   win-utf
fastcgi_params  koi-win  nginx.conf  uwsgi_params
[root@server1 conf]# vim nginx.conf

2 user  nginx;
 3 worker_processes  auto;
 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         }

[root@server1 conf]# nginx -t                      ##检测语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx                        ##启动nginx
[root@server1 conf]# nginx -s reload         ##重启

[root@server1 conf]# ps ax | grep nginx
29829 ?        Ss     0:00 nginx: master process nginx
29833 ?        S      0:00 nginx: worker process
29834 ?        S      0:00 nginx: worker process
29836 pts/0    S+     0:00 grep --color=auto nginx

[root@server1 conf]# curl localhost

[root@server2 ~]# yum install -y php-fpm-5.4.16-46.el7.x86_64.rpm

[root@server2 ~]# cd /etc/php-fpm.d/
[root@server2 php-fpm.d]# ls
www.conf

[root@server2 php-fpm.d]# vim www.conf

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

[root@server2 php-fpm.d]# systemctl start php-fpm  ##开启
[root@server2 php-fpm.d]# ps ax    ##查看进程
[root@server2 php-fpm.d]# netstat -antlp

访问172.25.10.2/index.php

[root@server3 ~]# scp test.sql [email protected]:/root

[root@server2 ~]# ls
[root@server2 ~]# cp test.php /usr/local/nginx/html/
[root@server2 ~]# cd /usr/local/nginx/html/
[root@server2 html]# vim test.php

在server4中操作:

root@server3 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@server3 ~]# redis-cli
127.0.0.1:6379> info

[root@server4 ~]# ls
test.sql
[root@server4 ~]# vim test.sql
[root@server4 ~]# mysql test < test.sql
[root@server4 ~]# mysql

MariaDB [(none)]> use test
MariaDB [test]> show tables;

MariaDB [test]> select * from test;

MariaDB [test]> grant all on test.* to redis@'%' identified by 'westos';

root@server2 ~]# yum install -y php
[root@server2 ~]# php -m | grep php
[root@server2 ~]# php -m | grep mysql
[root@server2 ~]# yum install -y php-mysql 

 

[root@server2 ~]# php -m | grep mysql
[root@server2 ~]#  php -m | grep redis

[root@westos_student73 Desktop]# scp php-pecl-redis-2.2.8-1.el7.x86_64.rpm [email protected]:/root

[root@westos_student73 Desktop]# scp  php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm [email protected]:/root

[root@server2 ~]# yum install -y php-pecl-redis-2.2.8-1.el7.x86_64.rpm php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm

[root@server2 ~]# php -m | grep redis

[root@server2 ~]# systemctl reload php-fpm       平滑加载

[root@server2 ~]# getenforce
Disabled
[root@server2 ~]# cd /usr/local/nginx/html/

root@server2 html]# chmod 644 test.php

在浏览器中访问: http://172.25.10.2/test.php (第一次数据是从mysql中访问到的)

第二次数据是从redis中访问到的

 从第二次访问后数据都是从redis中提取到的,大大降低了数据库的访问量,起到了缓冲的作用

 在server4中修改之后

MariaDB [test]> update test set name='westos' where id=1;

MariaDB [test]> select * from test; 

[root@server3 ~]# redis-cli
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> get 3
"test3"

 数据库中的数据发生变化,但redis中数据没有变化(数据库与redis中数据不同步)

使其同步的方法:将1中的数据删除,使其重新在数据库中加载,然后再访问时redis中的数据就会同步

[root@server3 ~]# redis-cli
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> get 3
"test3"
127.0.0.1:6379> del 1
(integer) 1
127.0.0.1:6379> get 1
(nil)
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> get 3
"test3"

也可直接在redis 中修改,然后在浏览器中访问

猜你喜欢

转载自blog.csdn.net/gk12050802/article/details/122003791