redis集群

  • 2019-09-06
  • 109
  • 0
  • 1

redis???

菲关系型数据库,以键值对的形式存储数据,将数据存储在内存中,通过RDB,AOF实现数据的持久化,从3版本开始,支持多种数据类型:字符串(string)、哈希(hash)、列表(list)、集合(set)、有序集合(zset)

redis常用指令

  • select:切换数据库,redis默认有16个数据库,从0开始到15结束
  • set:创建键值对,如果指定的键名已经存在,则会对该键值对的值进行覆盖
  • mset:批量创建键值对
  • get:查看指定的键对应的值
  • mget:批量查看指定的键对应的值
  • append:对指定的键的值的内容进行追加
  • del:删除指定的键对应的值,可以一次删除多个键值对
  • key *:查看当前数据库中所有的键
  • move:对数据库中的数据进行移动
  • flushall:清空所有键值对!!!

redis集群

  1. 节点之间通过总线端口实现互相通信,去中心化,客户端可以与任何一个节点相连
  2. 通过客户端请求一个键值对或者要写入键值对时,会对key进行CRC16的计算并对计算的结果对16384进行取余得到一个值,集群中的节点会均分槽点(共16384个槽点,0-16383),对key计算的值对进行节点的槽点范围的匹配,属于哪个节点的范围就将数据写入或者从该节点读取客户端请求的键值对

哈希槽   槽点 16384 (0–16383)

crc16算法

  1. redis集群最少要部署三台节点,而且节点的个数得是奇数个,集群中节点可以进行投票,等超过半数以上的节点认为某个节点不可用时,则该节点不可用
  2. redis支持主从复制,集群中的主节点可以有多个从节点,当主节点不可用时,会从该主机点的从节点中随机找出一台作为新的主节点提供服务
  3. 当某一个主节点不可用时,同时其从节点也不可用时,则整个集群不可用

redis的安装与配置

实验环境

实验步骤

安装redis

[root@localhost ~]# tar -zxvf redis-4.0.6.tar.gz
[root@localhost ~]# mv redis-4.0.6 /usr/local/redis
[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# make && make install

修改redis配置文件

[root@localhost redis]# vim redis.conf
# 修改以下这三个地方
bind 192.168.1.11 port 6379 daemonize yes

启动redis

[root@localhost redis]# redis-server redis.conf 
12988:C 06 Sep 15:47:22.409 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12988:C 06 Sep 15:47:22.409 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=12988, just started
12988:C 06 Sep 15:47:22.409 # Configuration loaded

检查端口是否开启

[root@localhost redis]# netstat -anpt | grep redis
tcp 0 0 192.168.1.11:6379 0.0.0.0:* LISTEN 12989/redis-server

登录redis

[root@localhost redis]# redis-cli -h 192.168.1.11 -p 6379
192.168.1.11:6379> ping
PONG

安装 phpredis

[root@localhost ~]# unzip phpredis-master.zip
[root@localhost ~]# cd phpredis-master/
[root@localhost phpredis-master]# yum -y install autoconf
[root@localhost phpredis-master]# ln -s /usr/local/php/bin/* /usr/local/bin/
# 生成configure文件
[root@localhost phpredis-master]# phpize 
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
configure.in:3: warning: prefer named diversions
configure.in:3: warning: prefer named diversions
# 编译安装
[root@localhost phpredis-master]# ./configure --enable-redis --with-php-config=/usr/local/php/bin/php-config && make && make install
# 查看模块文件
[root@localhost phpredis-master]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
redis.so

修改php配置文件

[root@localhost phpredis-master]# vim /usr/local/php/php.ini
# 在最后一行添加
extension=redis.so

重启PHP

[root@localhost phpredis-master]# systemctl restart php-fpm.service

编辑PHP页面

[root@localhost phpredis-master]# cd /usr/local/nginx/html/
[root@localhost html]# vim index.php 
<?php
    phpinfo();
?>

访问PHP页面

[root@localhost html]# firefox 192.168.1.11

设置MySQL密码

[root@localhost ~]# mysqladmin -uroot -p password 123.com
Enter password:

进入MySQL数据库

[root@localhost ~]# mysql -uroot -p123.com
# 授权
mysql> grant all on *.* to 'root'@'192.168.1.%' identified by '123.com';
Query OK, 0 rows affected (0.00 sec)
# 刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

创建数据库表

mysql> create database abc;
Query OK, 1 row affected (0.05 sec)

mysql> create table test(id int, name char(10));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test values(1,'one'),(2,'two'),(3,'three'),(4,'four'),(5,'five'); 
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from abc.test;
+------+-------+
| id   | name  |
+------+-------+
|    1 | one   |
|    2 | two   |
|    3 | three |
|    4 | four  |
|    5 | five  |
+------+-------+
5 rows in set (0.00 sec)

编辑前端页面

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim redis.php
<?php
$redis=new  Redis;
$redis->connect("192.168.1.11","6379") or  die ("could not connect redis");
$query="select * from abc.test   limit 5;";
for ($key=1;$key<=5;$key++){
       if (!$redis->get($key)) {
              $conn=mysql_connect("192.168.1.11","root","123.com");
              $result=mysql_query($query);
              while ($row=mysql_fetch_assoc($result)) {
                     $redis->set($row["id"],$row["name"]);
              }
              break;
       }
else {
      $name="redis";
      $data[$key]=$redis->get($key);
     }
}
echo $name;
echo "</br>";
for  ($key=1;$key<=5;$key++)  {
        echo "id is $key";
        echo "</br>";
        echo "name is $data[$key]";
        echo "</br>";
       }
?>

访问redis.php页面

[root@localhost html]# firefox 192.168.1.11/redis.php

redis集群

创建redis集群目录及节点端口的目录

[root@localhost html]# mkdir redis_cluster
[root@localhost html]# cd redis_cluster/
[root@localhost redis_cluster]# mkdir {7000..7005}
[root@localhost redis_cluster]# ls
7000 7001 7002 7003 7004 7005

编辑每个节点的配置文件

[root@localhost redis_cluster]# cp /usr/local/redis/redis.conf 7000
[root@localhost redis_cluster]# vim 7000/redis.conf

拷贝7000配置文件到其他节点配置文件

[root@localhost redis_cluster]# cp 7000/redis.conf 7001/
[root@localhost redis_cluster]# cp 7000/redis.conf 7002/
[root@localhost redis_cluster]# cp 7000/redis.conf 7003/
[root@localhost redis_cluster]# cp 7000/redis.conf 7004/
[root@localhost redis_cluster]# cp 7000/redis.conf 7005/

修改各个节点的配置文件

# 修改端口号、节点端口配置文件号即可
[root@localhost redis_cluster]# vim 7001/redis.conf 
[root@localhost redis_cluster]# vim 7002/redis.conf 
[root@localhost redis_cluster]# vim 7003/redis.conf 
[root@localhost redis_cluster]# vim 7004/redis.conf 
[root@localhost redis_cluster]# vim 7005/redis.conf

启动redis各个节点

[root@localhost redis_cluster]# redis-server 7000/redis.conf
[root@localhost redis_cluster]# redis-server 7001/redis.conf
[root@localhost redis_cluster]# redis-server 7002/redis.conf
[root@localhost redis_cluster]# redis-server 7003/redis.conf
[root@localhost redis_cluster]# redis-server 7004/redis.conf
[root@localhost redis_cluster]# redis-server 7005/redis.conf

安装ruby

[root@localhost ~]# yum -y install ruby

安装gem

[root@localhost ~]# gem install redis-3.3.0.gem

创建集群

  • M:master(主节点)
  • S:slave(从节点)
# 默认前三个ip:port为主节点,后三个为从节点
[root@localhost ~]# redis-trib.rb create --replicas 1 192.168.1.11:7000 192.168.1.11:7001 192.168.1.11:7002 192.168.1.11:7003 192.168.1.11:7004 192.168.1.11:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.11:7000
192.168.1.11:7001
192.168.1.11:7002
Adding replica 192.168.1.11:7003 to 192.168.1.11:7000
Adding replica 192.168.1.11:7004 to 192.168.1.11:7001
Adding replica 192.168.1.11:7005 to 192.168.1.11:7002
M: c8b0d225af33469d6b2390764c5a42eee09448af 192.168.1.11:7000
slots:0-5460 (5461 slots) master
M: eb543164773a190831f77de209ff914b64e83a27 192.168.1.11:7001
slots:5461-10922 (5462 slots) master
M: 05b2023caeeb053f2a833506c50c381d051f3048 192.168.1.11:7002
slots:10923-16383 (5461 slots) master
S: 8770d83c2988392bb03deddaae58443936bc0fd1 192.168.1.11:7003
replicates c8b0d225af33469d6b2390764c5a42eee09448af
S: 4c8a677e404766106b3ee5baf7504982351489ea 192.168.1.11:7004
replicates eb543164773a190831f77de209ff914b64e83a27
S: 6a3314ea20f1ce08091e2a031bef4b680c2c6593 192.168.1.11:7005
replicates 05b2023caeeb053f2a833506c50c381d051f3048
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.1.11:7000)
M: c8b0d225af33469d6b2390764c5a42eee09448af 192.168.1.11:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 4c8a677e404766106b3ee5baf7504982351489ea 192.168.1.11:7004
slots: (0 slots) slave
replicates eb543164773a190831f77de209ff914b64e83a27
S: 6a3314ea20f1ce08091e2a031bef4b680c2c6593 192.168.1.11:7005
slots: (0 slots) slave
replicates 05b2023caeeb053f2a833506c50c381d051f3048
M: eb543164773a190831f77de209ff914b64e83a27 192.168.1.11:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 8770d83c2988392bb03deddaae58443936bc0fd1 192.168.1.11:7003
slots: (0 slots) slave
replicates c8b0d225af33469d6b2390764c5a42eee09448af
M: 05b2023caeeb053f2a833506c50c381d051f3048 192.168.1.11:7002
slots:10923-16383 (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.

redis写入键值对

[root@localhost ~]# redis-cli -h 192.168.1.11 -p 7000 -c
192.168.1.11:7000> 
192.168.1.11:7000> SET shuai guoer
-> Redirected to slot [6441] located at 192.168.1.11:7001
OK

关闭一个主,查看对应的从节点是否会变成主节点

[root@localhost ~]# ps -ef | grep redis
root 12785 1 0 17:19 ? 00:00:04 redis-server 192.168.1.11:6379
root 17132 1 0 18:06 ? 00:00:01 redis-server 192.168.1.11:7000 [cluster]
root 17149 1 0 18:06 ? 00:00:01 redis-server 192.168.1.11:7001 [cluster]
root 17166 1 0 18:06 ? 00:00:01 redis-server 192.168.1.11:7002 [cluster]
root 17177 1 0 18:06 ? 00:00:01 redis-server 192.168.1.11:7003 [cluster]
root 17188 1 0 18:06 ? 00:00:01 redis-server 192.168.1.11:7004 [cluster]
root 17205 1 0 18:06 ? 00:00:01 redis-server 192.168.1.11:7005 [cluster]
root 19816 9233 0 18:40 pts/0 00:00:00 grep --color=auto redis
# 杀死7000端口的主节点
[root@localhost ~]# kill 17132

# 检查节点状态变化
[root@localhost ~]# redis-trib.rb check 192.168.1.11:7001
>>> Performing Cluster Check (using node 192.168.1.11:7001)
M: eb543164773a190831f77de209ff914b64e83a27 192.168.1.11:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 4c8a677e404766106b3ee5baf7504982351489ea 192.168.1.11:7004
   slots: (0 slots) slave
   replicates eb543164773a190831f77de209ff914b64e83a27
M: 8770d83c2988392bb03deddaae58443936bc0fd1 192.168.1.11:7003
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
S: 6a3314ea20f1ce08091e2a031bef4b680c2c6593 192.168.1.11:7005
   slots: (0 slots) slave
   replicates 05b2023caeeb053f2a833506c50c381d051f3048
M: 05b2023caeeb053f2a833506c50c381d051f3048 192.168.1.11:7002
   slots:10923-16383 (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.

创建好节点7006、 7007、 7008 并开启

# 拷贝配置文件
[root@localhost redis_cluster]# cp 7000/redis.conf 7006/
[root@localhost redis_cluster]# cp 7000/redis.conf 7007/
[root@localhost redis_cluster]# cp 7000/redis.conf 7008/
# 修改配置文件
[root@localhost redis_cluster]# vim 7006/redis.conf 
[root@localhost redis_cluster]# vim 7007/redis.conf 
[root@localhost redis_cluster]# vim 7008/redis.conf 
# 启动各个节点
[root@localhost redis_cluster]# redis-server 7006/redis.conf
[root@localhost redis_cluster]# redis-server 7007/redis.conf
[root@localhost redis_cluster]# redis-server 7008/redis.conf

添加从节点

[root@localhost redis_cluster]# redis-trib.rb add-node --slave 192.168.1.11:7006 192.168.1.11:7001
# 检查节点状态
[root@localhost redis_cluster]# redis-trib.rb check 192.168.1.11:7001
>>> Performing Cluster Check (using node 192.168.1.11:7001)
M: eb543164773a190831f77de209ff914b64e83a27 192.168.1.11:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 4c8a677e404766106b3ee5baf7504982351489ea 192.168.1.11:7004
slots: (0 slots) slave
replicates eb543164773a190831f77de209ff914b64e83a27
M: 8770d83c2988392bb03deddaae58443936bc0fd1 192.168.1.11:7003
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 6a3314ea20f1ce08091e2a031bef4b680c2c6593 192.168.1.11:7005
slots: (0 slots) slave
replicates 05b2023caeeb053f2a833506c50c381d051f3048
S: 2551675a0c813651b5b058350d5b55f33fa0717b 192.168.1.11:7006
slots: (0 slots) slave
replicates 8770d83c2988392bb03deddaae58443936bc0fd1
M: 05b2023caeeb053f2a833506c50c381d051f3048 192.168.1.11:7002
slots:10923-16383 (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@localhost redis_cluster]# redis-trib.rb add-node 192.168.1.11:7007 192.168.1.11:7001
# 检查节点状态
[root@localhost redis_cluster]# redis-trib.rb check 192.168.1.11:7001
>>> Performing Cluster Check (using node 192.168.1.11:7001)
M: eb543164773a190831f77de209ff914b64e83a27 192.168.1.11:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 8c074feccb794d1751953c79ec0364fc9cf3f33c 192.168.1.11:7007
slots: (0 slots) master
0 additional replica(s)
S: 4c8a677e404766106b3ee5baf7504982351489ea 192.168.1.11:7004
slots: (0 slots) slave
replicates eb543164773a190831f77de209ff914b64e83a27
M: 8770d83c2988392bb03deddaae58443936bc0fd1 192.168.1.11:7003
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 6a3314ea20f1ce08091e2a031bef4b680c2c6593 192.168.1.11:7005
slots: (0 slots) slave
replicates 05b2023caeeb053f2a833506c50c381d051f3048
S: 2551675a0c813651b5b058350d5b55f33fa0717b 192.168.1.11:7006
slots: (0 slots) slave
replicates 8770d83c2988392bb03deddaae58443936bc0fd1
M: 05b2023caeeb053f2a833506c50c381d051f3048 192.168.1.11:7002
slots:10923-16383 (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.

给指定主节点添加从节点

redis-trib-rb add-node --slave --master-id  主节点node_id 192.168.1.11:7008 192.168.1.11:7007

[root@localhost redis_cluster]# redis-trib.rb add-node --slave --master-id 8c074feccb794d1751953c79ec0364fc9cf3f33c 192.168.1.11:7008 192.168.1.11:7007
# 检查节点状态
[root@localhost redis_cluster]# redis-trib.rb check 192.168.1.11:7001
>>> Performing Cluster Check (using node 192.168.1.11:7001)
M: eb543164773a190831f77de209ff914b64e83a27 192.168.1.11:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 8c074feccb794d1751953c79ec0364fc9cf3f33c 192.168.1.11:7007
slots: (0 slots) master
1 additional replica(s)
M: 8770d83c2988392bb03deddaae58443936bc0fd1 192.168.1.11:7003
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 05b2023caeeb053f2a833506c50c381d051f3048 192.168.1.11:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 4c8a677e404766106b3ee5baf7504982351489ea 192.168.1.11:7004
slots: (0 slots) slave
replicates eb543164773a190831f77de209ff914b64e83a27
S: 2b31b2ea4a2d9d0ee4fcd09e29a581899f22bc01 192.168.1.11:7008
slots: (0 slots) slave
replicates 8c074feccb794d1751953c79ec0364fc9cf3f33c
S: 6a3314ea20f1ce08091e2a031bef4b680c2c6593 192.168.1.11:7005
slots: (0 slots) slave
replicates 05b2023caeeb053f2a833506c50c381d051f3048
S: 2551675a0c813651b5b058350d5b55f33fa0717b 192.168.1.11:7006
slots: (0 slots) slave
replicates 8770d83c2988392bb03deddaae58443936bc0fd1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

集群槽点(slot)重分配

[root@localhost redis_cluster]# redis-trib.rb reshard 192.168.1.11:7002
>>> Performing Cluster Check (using node 192.168.1.11:7002)
M: 05b2023caeeb053f2a833506c50c381d051f3048 192.168.1.11:7002
   slots:78-9405 (9328 slots) master
   1 additional replica(s)
S: 2551675a0c813651b5b058350d5b55f33fa0717b 192.168.1.11:7006
   slots: (0 slots) slave
   replicates 05b2023caeeb053f2a833506c50c381d051f3048
M: 8c074feccb794d1751953c79ec0364fc9cf3f33c 192.168.1.11:7007
   slots: (0 slots) master
   0 additional replica(s)
S: 6a3314ea20f1ce08091e2a031bef4b680c2c6593 192.168.1.11:7005
   slots: (0 slots) slave
   replicates eb543164773a190831f77de209ff914b64e83a27
S: 4c8a677e404766106b3ee5baf7504982351489ea 192.168.1.11:7004
   slots: (0 slots) slave
   replicates 8770d83c2988392bb03deddaae58443936bc0fd1
M: 8770d83c2988392bb03deddaae58443936bc0fd1 192.168.1.11:7003
   slots:0-77 (78 slots) master
   1 additional replica(s)
M: eb543164773a190831f77de209ff914b64e83a27 192.168.1.11:7001
   slots:9406-16383 (6978 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.
How many slots do you want to move (from 1 to 16384)? 9328                 # 要删除的槽点(slot)个数
What is the receiving node ID? eb543164773a190831f77de209ff914b64e83a27    # 接收槽点的ID
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:05b2023caeeb053f2a833506c50c381d051f3048                    # 要删除槽点的节点ID
Source node #2:done                                                        # 开始执行

删除从节点

[root@localhost redis_cluster]# redis-trib.rb del-node 192.168.1.11:7008 2b31b2ea4a2d9d0ee4fcd09e29a581899f22bc01
>>> Removing node 2b31b2ea4a2d9d0ee4fcd09e29a581899f22bc01 from cluster 192.168.1.11:7008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

删除主节点

# 如果主节点被删除从节点将会自动代替主节点,如果主节点有分配的槽点(slot)那么先去掉分配的槽点(slot),然后再删除节点

# 重分配槽点
[root@localhost redis_cluster]# redis-trib.rb reshard 192.168.1.11:7002
# 删除槽点
[root@localhost redis_cluster]# redis-trib.rb del-node 192.168.1.11:7002 05b2023caeeb053f2a833506c50c381d051f3048
>>> Removing node 05b2023caeeb053f2a833506c50c381d051f3048 from cluster 192.168.1.11:7002
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

评论

还没有任何评论,你来说两句吧

发表评论

:zhenbang: :yiwen: :yinyue: :yinxian: :yi: :xxyl: :xinsui: :xiaonian: :weiqu: :tushe: :tu: :taiyang: :taikaixin: :shuijiao: :shenli: :shengqi: :ruo: :qianbi: :qian: :pen: :neng: :mianqiang: :meigui: :lu: :liwu: :kuanghan: :ku: :kaixin: :jinya: :jiku: :huaxin: :huaji: :hu: :heixian: :hehe: :han: :haha: :haha2: :guai: :dangao: :damuzhi: :daku: :chabei: :caihong: :bugaoxing: :bishi: :aixin: :a: :OK: :) :(