部署LNP+Redis、创建Redis集群、连接集群、集群工作原理

常用命令的使用

64

配置64本机的nginx网站服务可以把数据存储在本机的内存里

在64运行nginx服务,并配置可以解析php脚本

在64主机运行redis服务,并允许在127地址连接服务

[root@redis64 ~]# yum -y install redis    # 安装软件
[root@redis64 ~]# systemctl start redis    # 启动服务
[root@redis64 ~]# netstat -utnlp | grep redis-server    # 查看端口
tcp        0      0 127.0.0.1:6379    0.0.0.0:*    LISTEN      1970/redis-server 1
[root@redis64 ~]# redis-cli    # 连接服务
127.0.0.1:6379> ping    # 检查能否正在访问
PONG 
127.0.0.1:6379> set test aaa    # 存储变量
OK
127.0.0.1:6379> get test    # 查看变量
"aaa"
127.0.0.1:6379> exit    # 断开连接
[root@redis64 ~]#

修改服务运行参数

[root@redis64 ~]# systemctl stop redis
[root@redis64 ~]# vim /etc/redis.conf
bind 192.168.88.64
port 6364
requirepass 123456
[root@redis64 ~]# setenforce 0
[root@redis64 ~]# systemctl start redis
[root@redis64 ~]# netstat  -utnlp  | grep redis-server
tcp   0    0 192.168.88.64:6364   0.0.0.0:*    LISTEN      2009/redis-server 1 
[root@redis64 ~]#
[root@redis64 ~]# redis-cli  -h 192.168.88.64 -p 6364 连接服务
192.168.88.64:6364> ping 不输入密码无法正常访问
(error) NOAUTH Authentication required.
192.168.88.64:6364> auth 123456  输入密码
OK
192.168.88.64:6364> keys *  查看存储的变量
1) "test"
192.168.88.64:6364> exit  断开连接
[root@redis64 ~]#

部署LNP + Redis

配置nginx网站的php脚本可以连接redis服务 存储数据

在64部署LNP环境

安装源码nginx及php

[root@redis64 ~]# yum  -y  install gcc make pcre-devel zlib-devel    # 安装依赖
[root@redis64 ~]# tar -xf nginx-1.22.1.tar.gz    # 解压源码
[root@redis64 ~]# cd nginx-1.22.1    # 进源码目录
[root@redis64 ~]# ./configure    # 配置
[root@redis64 ~]# make    # 编译
[root@redis64 ~]# make install    # 安装
[root@redis64 ~]# ls /usr/local/nginx/    # 查看安装目录
conf  html  logs  sbin
[root@redis64 ~]# yum -y install php php-fpm php-devel    # 安装 php 软件

配置动静分离

[root@redis64 ~]# vim +65 /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }

启动nginx服务

[root@redis64 ~]# /usr/local/nginx/sbin/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@redis64 ~]# /usr/local/nginx/sbin/nginx     # 启动服务
[root@redis64 ~]# netstat -utnlp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10469/nginx: master 

启动php-fpm服务

[root@redis64 ~]# vim /etc/php-fpm.d/www.conf
38 ;listen = /run/php-fpm/www.sock
39 listen = 127.0.0.1:9000
[root@redis64 ~]# systemctl  start php-fpm    # 启动服务
[root@redis64 ~]# netstat -utnlp | grep 9000    # 查看端口
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      10477/php-fpm: mast 
[root@redis64 ~]#

测试配置

[root@redis64 ~]# cat /usr/local/nginx/html/test.php    # 编写php脚本    
<?php
$i = 100 ;
echo $i ;
echo "\n" ;
?>
[root@redis64 ~]# curl  http://localhost/test.php    # 访问php脚本
100

配置PHP支持redis

安装提供redis模块的软件

[root@redis64 ~]# tar -xf redis-cluster-4.3.0.tgz 
[root@redis64 ~]# ls
package.xml  redis-4.3.0  redis-cluster-4.3.0.tgz
[root@redis64 ~]# cd redis-4.3.0/
[root@redis64 redis-4.3.0]# ls
arrays.markdown    config.m4   INSTALL.markdown  README.markdown     redis.c           redis_session.c
cluster_library.c  config.w32  liblzf            redis_array.c       redis_cluster.c   redis_session.h
cluster_library.h  COPYING     library.c         redis_array.h       redis_cluster.h   tests
cluster.markdown   crc16.h     library.h         redis_array_impl.c  redis_commands.c
common.h           CREDITS     php_redis.h       redis_array_impl.h  redis_commands.h
[root@redis64 redis-4.3.0]# phpize    # 获取php版本信息 
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
[root@redis64 redis-4.3.0]# ls
acinclude.m4       common.h      COPYING           ltmain.sh        redis_array_impl.c  redis_session.h
aclocal.m4         config.guess  crc16.h           Makefile.global  redis_array_impl.h  run-tests.php
arrays.markdown    config.h.in   CREDITS           missing          redis.c             tests
autom4te.cache     config.m4     INSTALL.markdown  mkinstalldirs    redis_cluster.c
build              config.sub    install-sh        php_redis.h      redis_cluster.h
cluster_library.c  configure     liblzf            README.markdown  redis_commands.c
cluster_library.h  configure.ac  library.c         redis_array.c    redis_commands.h
cluster.markdown   config.w32    library.h         redis_array.h    redis_session.c
[root@redis64 redis-4.3.0]# ls /usr/bin/php
php         php-cgi     php-config  phpize      
[root@redis64 redis-4.3.0]# ls /usr/bin/php-config 
/usr/bin/php-config
[root@redis64 redis-4.3.0]# cat /usr/bin/php-config
[root@redis64 redis-4.3.0]# ./configure --help | grep php
  --with-php-config=PATH  Path to php-config php-config
[root@redis64 redis-4.3.0]# ./configure --with-php-config=/usr/bin/php-config
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/lib64/php/modules/
[root@redis64 redis-4.3.0]# ls /usr/lib64/php/modules/
bz2.so       ctype.so  exif.so      ftp.so      iconv.so  redis.so    tokenizer.so
calendar.so  curl.so   fileinfo.so  gettext.so  phar.so   sockets.so
[root@redis64 redis-4.3.0]# php -m | grep -i redis

调用模块

编写php脚本 连接redis服务存储数据

存储数据脚本 s.php

查看数据脚本 g.php

[root@redis64 ~]# tar -xf redis-cluster-4.3.0.tgz 
[root@redis64 ~]# ls
package.xml  redis-4.3.0  redis-cluster-4.3.0.tgz
[root@redis64 ~]# cd redis-4.3.0/
[root@redis64 redis-4.3.0]# ls
arrays.markdown    config.m4   INSTALL.markdown  README.markdown     redis.c           redis_session.c
cluster_library.c  config.w32  liblzf            redis_array.c       redis_cluster.c   redis_session.h
cluster_library.h  COPYING     library.c         redis_array.h       redis_cluster.h   tests
cluster.markdown   crc16.h     library.h         redis_array_impl.c  redis_commands.c
common.h           CREDITS     php_redis.h       redis_array_impl.h  redis_commands.h
[root@redis64 redis-4.3.0]# phpize    # 获取php版本信息
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
[root@redis64 redis-4.3.0]# ls
acinclude.m4       common.h      COPYING           ltmain.sh        redis_array_impl.c  redis_session.h
aclocal.m4         config.guess  crc16.h           Makefile.global  redis_array_impl.h  run-tests.php
arrays.markdown    config.h.in   CREDITS           missing          redis.c             tests
autom4te.cache     config.m4     INSTALL.markdown  mkinstalldirs    redis_cluster.c
build              config.sub    install-sh        php_redis.h      redis_cluster.h
cluster_library.c  configure     liblzf            README.markdown  redis_commands.c
cluster_library.h  configure.ac  library.c         redis_array.c    redis_commands.h
cluster.markdown   config.w32    library.h         redis_array.h    redis_session.c
[root@redis64 redis-4.3.0]# ls /usr/bin/php
php         php-cgi     php-config  phpize      
[root@redis64 redis-4.3.0]# ls /usr/bin/php-config 
/usr/bin/php-config
[root@redis64 redis-4.3.0]# cat /usr/bin/php-config
[root@redis64 redis-4.3.0]# ./configure --help | grep php
  --with-php-config=PATH  Path to php-config php-config
[root@redis64 redis-4.3.0]# ./configure --with-php-config=/usr/bin/php-config
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/lib64/php/modules/
[root@redis64 redis-4.3.0]# ls /usr/lib64/php/modules/
bz2.so       ctype.so  exif.so      ftp.so      iconv.so  redis.so    tokenizer.so
calendar.so  curl.so   fileinfo.so  gettext.so  phar.so   sockets.so
[root@redis64 redis-4.3.0]# php -m | grep -i redis
[root@redis64 redis-4.3.0]# vim /etc/php.ini 
[root@redis64 redis-4.3.0]# php -m | grep -i redis
redis
[root@redis64 redis-4.3.0]# systemctl restart php-fpm.service 
[root@redis64 redis-4.3.0]# vim /usr/local/nginx/html/s.php
<?php
$redis = new redis();
$redis->connect("127.0.0.1", "6364");
$redis->auth("123456");
$redis->set("class","nsd2310");
echo "save ok\n";
?>

[root@redis64 redis-4.3.0]# redis-cli -h 127.0.0.1 -p 6364 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6364> keys *
(empty list or set)
127.0.0.1:6364> exit
[root@redis64 redis-4.3.0]# curl http://localhost/s.php
save ok
[root@redis64 redis-4.3.0]# cat /usr/local/nginx/html/test.php 
<?php
$i = 100 ;
echo $i ;
echo "\n" ;
?>
[root@redis64 redis-4.3.0]# redis-cli -h 127.0.0.1 -p 6364 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6364> keys *
1) "class"
127.0.0.1:6364> get class
"nsd2310"
127.0.0.1:6364> 
127.0.0.1:6364> exit
[root@redis64 redis-4.3.0]# vim /usr/local/nginx/html/g.php
[root@redis64 redis-4.3.0]# curl http://localhost/g.php
[root@redis64 redis-4.3.0]# cat /usr/local/nginx/html/g.php
<?php
$redis = new redis();
$redis->connect("127.0.0.1", "6364");
$redis->auth("123456");
echo $redis->get("class");
echo "\n";
?>


[root@redis64 redis-4.3.0]# vim /usr/local/nginx/html/g.php
[root@redis64 redis-4.3.0]# curl http://localhost/g.php
nsd2310
[root@redis64 redis-4.3.0]# redis-cli -h 127.0.0.1 -p 6364 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6364> set class nsd2311
OK
127.0.0.1:6364> exit
[root@redis64 redis-4.3.0]# curl http://localhost/g.php
nsd2311
[root@redis64 redis-4.3.0]

部署redis集群 给网站提供数据存储服务,解决如下问题

redis服务单点故障

数据的自动备份

数据存储的速度和存储不足的问题

准备6台redis服务器做集群

51 52 53 54 55 56

]# yum -y install redis    # 安装软件
]# vim  /etc/redis.conf    # 修改主配置文件夹
    92 port  6379    # 端口号
    69 bind   192.168.88.51    # IP地址
    838 cluster-enabled  yes    # 启用集群功能  
    846 cluster-config-file  nodes-6379.conf    # 存储集群信息文件
    852 cluster-node-timeout  5000    # 集群中主机通信超时时间
]# systemctl   start  redis
]# netstat -utnlp  | grep redis-serve
tcp  0  0 192.168.88.51:6379  0.0.0.0:*   LISTEN      21201/redis-serve  
tcp  0  0 192.168.88.51:16379   0.0.0.0:*   LISTEN   21201/redis-serve    # 集群端口   

创建集群

在 51~56 任意一台服务器上执行创建集群的命令都可以

--cluster-replicas 1 给每个master服务器分配一台slave服务器,每个主至少要分配1台slave服务器,不然无法实现redis服务的高可用

创建集群时,会自动创建主从角色,默认把主机列表中的前3台服务器创建为Master角色的redis服务器,剩下的均配置为slave角色服务器

创建集群时,会自动给master角色的主机分配hash槽 ,通过hash槽实现数据的分布式存储

查看命令帮助

[root@Host51 ~]# redis-cli --cluster help

创建集群

[root@Host51 ~]# redis-cli --cluster create 192.168.88.51:6379 192.168.88.52:6379 192.168.88.53:6379 192.168.88.54:6379 192.168.88.55:6379 192.168.88.56:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.88.54:6379 to 192.168.88.51:6379
Adding replica 192.168.88.55:6379 to 192.168.88.52:6379
Adding replica 192.168.88.56:6379 to 192.168.88.53:6379
M: 0ceaf74c80061a9cf8075ebae1361ae579f113bd 192.168.88.51:6379
   slots:[0-5460] (5461 slots) master
M: beedff7f08f050499112d7b032ebdcc991dcaeab 192.168.88.52:6379
   slots:[5461-10922] (5462 slots) master
M: 0b2841b7fdb173b3f74bf5a75d01ef31ae451e07 192.168.88.53:6379
   slots:[10923-16383] (5461 slots) master
S: 0e2e8ea308d98961c6277dd772d3fce9730333ca 192.168.88.54:6379
   replicates 0ceaf74c80061a9cf8075ebae1361ae579f113bd
S: f6ac666e65d328b1950cd7665a6dc64aadf5c3ac 192.168.88.55:6379
   replicates beedff7f08f050499112d7b032ebdcc991dcaeab
S: 91d50cfc022f607ef11944e406028ba7cfba459c 192.168.88.56:6379
   replicates 0b2841b7fdb173b3f74bf5a75d01ef31ae451e07
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.88.51:6379)
M: 0ceaf74c80061a9cf8075ebae1361ae579f113bd 192.168.88.51:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 0b2841b7fdb173b3f74bf5a75d01ef31ae451e07 192.168.88.53:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 91d50cfc022f607ef11944e406028ba7cfba459c 192.168.88.56:6379
   slots: (0 slots) slave
   replicates 0b2841b7fdb173b3f74bf5a75d01ef31ae451e07
S: 0e2e8ea308d98961c6277dd772d3fce9730333ca 192.168.88.54:6379
   slots: (0 slots) slave
   replicates 0ceaf74c80061a9cf8075ebae1361ae579f113bd
M: beedff7f08f050499112d7b032ebdcc991dcaeab 192.168.88.52:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f6ac666e65d328b1950cd7665a6dc64aadf5c3ac 192.168.88.55:6379
   slots: (0 slots) slave
   replicates beedff7f08f050499112d7b032ebdcc991dcaeab
[OK] All nodes agree about slots configuration.
>>> Check for open slots...                                                                               
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看统计信息

[root@Host51 ~]# redis-cli --cluster info 192.168.88.52:6379
192.168.88.52:6379 (beedff7f...) -> 0 keys | 5462 slots | 1 slaves.
192.168.88.53:6379 (0b2841b7...) -> 0 keys | 5461 slots | 1 slaves.
192.168.88.51:6379 (0ceaf74c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.        

查看详细信息

[root@Host51 ~]# redis-cli --cluster check 192.168.88.52:6379
192.168.88.52:6379 (beedff7f...) -> 0 keys | 5462 slots | 1 slaves.
192.168.88.53:6379 (0b2841b7...) -> 0 keys | 5461 slots | 1 slaves.
192.168.88.51:6379 (0ceaf74c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.                                                                            
>>> Performing Cluster Check (using node 192.168.88.52:6379)
M: beedff7f08f050499112d7b032ebdcc991dcaeab 192.168.88.52:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 0b2841b7fdb173b3f74bf5a75d01ef31ae451e07 192.168.88.53:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 0e2e8ea308d98961c6277dd772d3fce9730333ca 192.168.88.54:6379
   slots: (0 slots) slave
   replicates 0ceaf74c80061a9cf8075ebae1361ae579f113bd
S: 91d50cfc022f607ef11944e406028ba7cfba459c 192.168.88.56:6379
   slots: (0 slots) slave
   replicates 0b2841b7fdb173b3f74bf5a75d01ef31ae451e07
S: f6ac666e65d328b1950cd7665a6dc64aadf5c3ac 192.168.88.55:6379
   slots: (0 slots) slave
   replicates beedff7f08f050499112d7b032ebdcc991dcaeab
M: 0ceaf74c80061a9cf8075ebae1361ae579f113bd 192.168.88.51:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...                                                                               
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看保存集群信息

[root@Host51 ~]# cat /var/lib/redis/nodes-6379.conf 
0ceaf74c80061a9cf8075ebae1361ae579f113bd 192.168.88.51:6379@16379 myself,master - 0 1705887242000 1 connected 0-5460
0b2841b7fdb173b3f74bf5a75d01ef31ae451e07 192.168.88.53:6379@16379 master - 0 1705887242000 3 connected 10923-16383
91d50cfc022f607ef11944e406028ba7cfba459c 192.168.88.56:6379@16379 slave 0b2841b7fdb173b3f74bf5a75d01ef31ae451e07 0 1705887242129 6 connected
0e2e8ea308d98961c6277dd772d3fce9730333ca 192.168.88.54:6379@16379 slave 0ceaf74c80061a9cf8075ebae1361ae579f113bd 0 1705887242630 4 connected
beedff7f08f050499112d7b032ebdcc991dcaeab 192.168.88.52:6379@16379 master - 0 1705887241528 2 connected 5461-10922
f6ac666e65d328b1950cd7665a6dc64aadf5c3ac 192.168.88.55:6379@16379 slave beedff7f08f050499112d7b032ebdcc991dcaeab 0 1705887241628 5 connected
vars currentEpoch 6 lastVoteEpoch 0

使用数据库命令查看集群信息

[root@Host51 ~]# redis-cli -h 192.168.88.52
192.168.88.52:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:1158
cluster_stats_messages_pong_sent:1162
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:2325
cluster_stats_messages_ping_received:1162
cluster_stats_messages_pong_received:1163
cluster_stats_messages_received:2325
192.168.88.52:6379> 

测试集群

解决redis服务单点故障

master角色的主机宕机后对应的slave会自动升级为master

master角色的主机恢复后会自动做slave角色的主机

[root@Host51 ~]# redis-cli --cluster info 192.168.88.52:6379
192.168.88.52:6379 (beedff7f...) -> 0 keys | 5462 slots | 1 slaves.
192.168.88.53:6379 (0b2841b7...) -> 0 keys | 5461 slots | 1 slaves.
192.168.88.51:6379 (0ceaf74c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

[root@Host53 ~]# systemctl stop redis
[root@Host53 ~]# ss -ntulp | grep redis-server
[root@Host53 ~]# 

[root@Host51 ~]# redis-cli --cluster info 192.168.88.52:6379
Could not connect to Redis at 192.168.88.53:6379: Connection refused
192.168.88.52:6379 (beedff7f...) -> 0 keys | 5462 slots | 1 slaves.
192.168.88.56:6379 (91d50cfc...) -> 0 keys | 5461 slots | 0 slaves.
192.168.88.51:6379 (0ceaf74c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

[root@Host53 ~]# systemctl start redis
[root@Host53 ~]# ss -ntulp | grep redis-server
tcp   LISTEN 0      128    192.168.88.53:16379      0.0.0.0:*    users:(("redis-server",pid=818,fd=8))
tcp   LISTEN 0      128    192.168.88.53:6379       0.0.0.0:*    users:(("redis-server",pid=818,fd=6))

[root@Host51 ~]# redis-cli --cluster info 192.168.88.52:6379
192.168.88.52:6379 (beedff7f...) -> 0 keys | 5462 slots | 1 slaves.
192.168.88.56:6379 (91d50cfc...) -> 0 keys | 5461 slots | 1 slaves.
192.168.88.51:6379 (0ceaf74c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@Host51 ~]# redis-cli --cluster check 192.168.88.52:6379
192.168.88.52:6379 (beedff7f...) -> 0 keys | 5462 slots | 1 slaves.
192.168.88.56:6379 (91d50cfc...) -> 0 keys | 5461 slots | 1 slaves.
192.168.88.51:6379 (0ceaf74c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.88.52:6379)
M: beedff7f08f050499112d7b032ebdcc991dcaeab 192.168.88.52:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 0b2841b7fdb173b3f74bf5a75d01ef31ae451e07 192.168.88.53:6379
   slots: (0 slots) slave
   replicates 91d50cfc022f607ef11944e406028ba7cfba459c
S: 0e2e8ea308d98961c6277dd772d3fce9730333ca 192.168.88.54:6379
   slots: (0 slots) slave
   replicates 0ceaf74c80061a9cf8075ebae1361ae579f113bd
M: 91d50cfc022f607ef11944e406028ba7cfba459c 192.168.88.56:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f6ac666e65d328b1950cd7665a6dc64aadf5c3ac 192.168.88.55:6379
   slots: (0 slots) slave
   replicates beedff7f08f050499112d7b032ebdcc991dcaeab
M: 0ceaf74c80061a9cf8075ebae1361ae579f113bd 192.168.88.51:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...                                                                               
>>> Check slots coverage...
[OK] All 16384 slots covered.

数据的自动备份

slave角色主机自动同步master角色主机数据

数据分布式存储

连接集群 存储的多个变量 不是存储在一台redis服务里 而是根据集群crc16 算法的计算结果存储数据 根据算法的计算结果把数据分别存储在集群的master角色主机上

连接集群的命令

redis-cli -c -h 51~56的ip -p 6379

集群存储数据的工作过程

根据算法的计算结果把数据分别存储在集群的master角色主机上

编写php脚本 连接集群

存储数据

[root@redis64 ~]# ss -ntulp | grep 80
[root@redis64 ~]# ss -ntulp | grep 9000
[root@redis64 ~]# php -m | grep -i redis
[root@redis64 ~]# vim /usr/local/nginx/html/s1.php
<?php
$redis_list = ['192.168.88.51:6379','192.168.88.52:6379','192.168.88.53:6379','192.168.88.54:6379','192.168.88.55:6379','192.168.88.56:6379'];
$client = new RedisCluster(NUll,$redis_list);
$client->set("i","tarenaA ");
$client->set("j","tarenaB ");
$client->set("k","tarenaC ");
echo "save ok\n";
?>

查询数据

[root@redis64 ~]# cp /usr/local/nginx/html/s1.php /usr/local/nginx/html/g1.php
[root@redis64 ~]# vim /usr/local/nginx/html/g1.php
<?php
$redis_list = ['192.168.88.51:6379','192.168.88.52:6379','192.168.88.53:6379','192.168.88.54:6379','192.168.88.55:6379','192.168.88.56:6379'];
$client = new RedisCluster(NUll,$redis_list);
echo $client->get("i");
echo $client->get("j");
echo $client->get("k");
?>
[root@redis64 ~]# curl http://localhost/s1.php
save ok
[root@redis64 ~]# curl http://localhost/g1.php
tarenaA tarenaB tarenaC [root@redis64 ~]# 

连接redis集群主机查看存储的数据

[root@Host51 ~]# redis-cli --cluster info 192.168.88.51:6379
192.168.88.51:6379 (0ceaf74c...) -> 2 keys | 5461 slots | 1 slaves.
192.168.88.56:6379 (91d50cfc...) -> 1 keys | 5461 slots | 1 slaves.
192.168.88.52:6379 (beedff7f...) -> 2 keys | 5462 slots | 1 slaves.
[OK] 5 keys in 3 masters.
0.00 keys per slot on average.
[root@Host51 ~]# redis-cli -c -h 192.168.88.51 -p 6379
192.168.88.51:6379> keys *
1) "age"
2) "j"
192.168.88.51:6379> get j
"tarenaB "
192.168.88.51:6379> 
[root@Host51 ~]# redis-cli -c -h 192.168.88.52 -p 6379
192.168.88.52:6379> keys *
1) "name"
2) "k"
192.168.88.52:6379> get k
"tarenaC "
192.168.88.52:6379> 
[root@Host51 ~]# redis-cli -c -h 192.168.88.56 -p 6379
192.168.88.56:6379> keys *
1) "i"
192.168.88.56:6379> get i
"tarenaA "
192.168.88.56:6379> 
相关推荐
酷讯网络_2408701605 分钟前
【全开源】Java多语言tiktok跨境商城TikTok内嵌商城送搭建教程
java·开发语言·开源
海威的技术博客33 分钟前
JS中的原型与原型链
开发语言·javascript·原型模式
WPG大大通40 分钟前
基于DIODES AP43781+PI3USB31531+PI3DPX1207C的USB-C PD& Video 之全功能显示器连接端口方案
c语言·开发语言·计算机外设·开发板·电源·大大通
蓝天星空1 小时前
spring cloud gateway 3
java·spring cloud
罗政1 小时前
PDF书籍《手写调用链监控APM系统-Java版》第9章 插件与链路的结合:Mysql插件实现
java·mysql·pdf
从以前1 小时前
【算法题解】Bindian 山丘信号问题(E. Bindian Signaling)
开发语言·python·算法
一根稻草君1 小时前
利用poi写一个工具类导出逐级合并的单元格的Excel(通用)
java·excel
kirito学长-Java1 小时前
springboot/ssm网上宠物店系统Java代码编写web宠物用品商城项目
java·spring boot·后端
木头没有瓜1 小时前
ruoyi 请求参数类型不匹配,参数[giftId]要求类型为:‘java.lang.Long‘,但输入值为:‘orderGiftUnionList
android·java·okhttp