Install redis

2. Start in single-machine mode. 3


mac

There is no redis.conf configuration file when installing, so use default values such as IP and port when starting.

Set up the cluster

Instead of installing multiple instances of Redis, clusters are created using different ports of the profile to represent one instance. In particular, each profile/port represents one instance.

Then, just start in cluster mode, and once started, each instance is running in cluster mode.

Finally, although the cluster mode is now running, there is no relationship between the instances. The last step is to combine the instances into a cluster, which has two main points: 1. Allocate slot for active node 2. Allocate slot for active node to secondary node.

Create configuration files for each node


Redis. Conf

Cluster - enabled yes / / open cluster pattern port 30001 / / the current node port cluster - config file/usr/local/etc/redisCluster/node1 node. Conf // Specify the cluster configuration file directory and filename (MAC is in /user/local/gongzhihao by default) dbfilename dump. RDB // Specify the persistent filename dir / usr/local/etc/redisCluster/node1 / / / persistence specified file directory (MAC default is in/user/local/gongzhihao directory)Copy the code

Start and run in cluster mode

bogon:~ gongzhihao$ redis-server /usr/local/etc/redisCluster/node1/redis.conf //一个节点一个节点的启动
1332:M 20 Dec 22:37:05.922 * Increased maximum number of open files to 10032 (it was originally set to 256).
1332:M 20 Dec 22:37:05.925 * No cluster configuration found, I'm 101873767c0b6bcf18bb047c61011b5dadb56764
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 30001
 |    `-._   `._    /     _.-'    |     PID: 1332
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

1332:M 20 Dec 22:37:05.929 # Server started, Redis version 3.0.7
1332:M 20 Dec 22:37:05.930 * The server is now ready to accept connections on port 30001


^Z
[1]+  Stopped                 redis-server /usr/local/etc/redisCluster/node1/redis.conf
bogon:~ gongzhihao$
bogon:~ gongzhihao$
bogon:~ gongzhihao$ redis-server /usr/local/etc/redisCluster/node2/redis.conf
1333:M 20 Dec 22:37:24.302 * Increased maximum number of open files to 10032 (it was originally set to 256).
1333:M 20 Dec 22:37:24.304 * No cluster configuration found, I'm db9fdbe242da713b5036723041a9075791bf76e0
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 30002
 |    `-._   `._    /     _.-'    |     PID: 1333
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

1333:M 20 Dec 22:37:24.305 # Server started, Redis version 3.0.7
1333:M 20 Dec 22:37:24.305 * The server is now ready to accept connections on port 30002

^Z
[2]+  Stopped                 redis-server /usr/local/etc/redisCluster/node2/redis.conf
bogon:~ gongzhihao$
bogon:~ gongzhihao$ redis-server /usr/local/etc/redisCluster/node3/redis.conf
1334:M 20 Dec 22:37:34.295 * Increased maximum number of open files to 10032 (it was originally set to 256).
1334:M 20 Dec 22:37:34.297 * No cluster configuration found, I'm d9db9e0ff5cffc46dbe88109580b491af9eb2015
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 30003
 |    `-._   `._    /     _.-'    |     PID: 1334
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

1334:M 20 Dec 22:37:34.298 # Server started, Redis version 3.0.7
1334:M 20 Dec 22:37:34.298 * The server is now ready to accept connections on port 30003


^Z
[3]+  Stopped                 redis-server /usr/local/etc/redisCluster/node3/redis.conf
bogon:~ gongzhihao$ redis-server /usr/local/etc/redisCluster/node4/redis.conf
1335:M 20 Dec 22:37:40.795 * Increased maximum number of open files to 10032 (it was originally set to 256).
1335:M 20 Dec 22:37:40.796 * No cluster configuration found, I'm d5c2e64650ef36a718b8e3f3ba196f6898a7f366
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 30004
 |    `-._   `._    /     _.-'    |     PID: 1335
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

1335:M 20 Dec 22:37:40.798 # Server started, Redis version 3.0.7
1335:M 20 Dec 22:37:40.798 * The server is now ready to accept connections on port 30004


^Z
[4]+  Stopped                 redis-server /usr/local/etc/redisCluster/node4/redis.conf
bogon:~ gongzhihao$ redis-server /usr/local/etc/redisCluster/node5/redis.conf
1336:M 20 Dec 22:37:49.521 * Increased maximum number of open files to 10032 (it was originally set to 256).
1336:M 20 Dec 22:37:49.523 * No cluster configuration found, I'm 432ad51d4913c6faf3b178c57ced84f9105589be
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 30005
 |    `-._   `._    /     _.-'    |     PID: 1336
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

1336:M 20 Dec 22:37:49.524 # Server started, Redis version 3.0.7
1336:M 20 Dec 22:37:49.524 * The server is now ready to accept connections on port 30005
^Z
[5]+  Stopped                 redis-server /usr/local/etc/redisCluster/node5/redis.conf
bogon:~ gongzhihao$
bogon:~ gongzhihao$
bogon:~ gongzhihao$ redis-server /usr/local/etc/redisCluster/node6/redis.conf
1337:M 20 Dec 22:37:55.068 * Increased maximum number of open files to 10032 (it was originally set to 256).
1337:M 20 Dec 22:37:55.070 * No cluster configuration found, I'm 45a48e40b7ab70403e94dffdc2e9c164b507e1a8
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 30006
 |    `-._   `._    /     _.-'    |     PID: 1337
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

1337:M 20 Dec 22:37:55.071 # Server started, Redis version 3.0.7
1337:M 20 Dec 22:37:55.071 * The server is now ready to accept connections on port 30006
^Z
[6]+  Stopped                 redis-server /usr/local/etc/redisCluster/node6/redis.conf
bogon:~ gongzhihao$
bogon:~ gongzhihao$
bogon:~ gongzhihao$ ps -ef | grep redis //启动成功
  501  1332  1081   0 10:37PM ttys002    0:00.02 redis-server *:30001 [cluster]
  501  1333  1081   0 10:37PM ttys002    0:00.01 redis-server *:30002 [cluster]
  501  1334  1081   0 10:37PM ttys002    0:00.01 redis-server *:30003 [cluster]
  501  1335  1081   0 10:37PM ttys002    0:00.01 redis-server *:30004 [cluster]
  501  1336  1081   0 10:37PM ttys002    0:00.01 redis-server *:30005 [cluster]
  501  1337  1081   0 10:37PM ttys002    0:00.01 redis-server *:30006 [cluster]
  501  1345  1081   0 10:38PM ttys002    0:00.00 grep redis
bogon:~ gongzhihao$
Copy the code

Associate nodes to a cluster

Although the above startup succeeded, no primary/secondary relationship was formed between the nodes, and no slot was allocated. To truly form a cluster relationship, you also need to allocate slots.

The problem

When building, encountered a lot of errors, are all kinds of configuration problems. For finishing.

reference

zhuanlan.zhihu.com/p/59172042

Huang Jianzhang’s book

Redis operations