本文共 9880 字,大约阅读时间需要 32 分钟。
从官方网站下载最新的稳定版本
$ wget https://download.redis.io/releases/redis-6.2.4.tar.gz$ tar xzf redis-6.2.4.tar.gz$ cd redis-6.2.4$ make
make test使用tcl的版本低,就更新下
apt-get install tcl
执行make test,成功后就可以运行redis了
$ echo "PATH=/home/user/redis/redis-6.2.4/src:$PATH" >> ~/.zshrc$ source ~/.zshrc$ redis-server$ redis-server redis6379.conf30438:C 17 Jun 2021 12:31:35.231 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo30438:C 17 Jun 2021 12:31:35.231 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=30438, just started30438:C 17 Jun 2021 12:31:35.231 # Configuration loaded30438:M 17 Jun 2021 12:31:35.232 * Increased maximum number of open files to 10032 (it was originally set to 1024).30438:M 17 Jun 2021 12:31:35.232 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.2.4 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 30438 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'30438:M 17 Jun 2021 12:31:35.232 # Server initialized30438:M 17 Jun 2021 12:31:35.232 # 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.30438:M 17 Jun 2021 12:31:35.232 * Loading RDB produced by version 6.2.4
Redis的主从设置只要执行slaveof ip port 就可以了,当然也可以配置文件设置
使用下载源文件的配置redis.conf,拷贝两个文件做主从
修改配置文件的以下内容
port 6379pidfile /home/user/redis/pid/6379.piddir /home/user/redis/lib/6379/
保持两个文件中的端口,配置文件,数据文件,日志等不同就可以,默认日志配置是标准输出可以不管。这里设置6379和6380两个配置文件。
$ redis-server redis6379.conf30438:C 17 Jun 2021 12:31:35.231 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo30438:C 17 Jun 2021 12:31:35.231 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=30438, just started30438:C 17 Jun 2021 12:31:35.231 # Configuration loaded30438:M 17 Jun 2021 12:31:35.232 * Increased maximum number of open files to 10032 (it was originally set to 1024).30438:M 17 Jun 2021 12:31:35.232 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.2.4 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 30438 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'30438:M 17 Jun 2021 12:31:35.232 # Server initialized30438:M 17 Jun 2021 12:31:35.232 # 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.30438:M 17 Jun 2021 12:31:35.232 * Loading RDB produced by version 6.2.4
两个服务都启动后在另外预设为从服务的客户端中执行命令
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379OK127.0.0.1:6380> info# Server... //这里省略一堆输出...# Replicationrole:slavemaster_host:127.0.0.1master_port:6379
看到上面的role已经改为slave
然后在mater下看下添加一些key在从服务器上是否有影响
127.0.0.1:6379> set port 6379OK
在从服务器下执行get命令
127.0.0.1:6380> get port"6379"
这样主从设置就ok了。
根据官方文档要求修改配置文件,并准备至少3个实例,最好是6个实例做到主从配置,这里只是演示只设置3个实例的效果
配置文件改为
port 6379cluster-enabled yescluster-config-file nodes-6379.confcluster-node-timeout 5000appendonly yes
其他两个实例的配置文件,修改端口和 cluster-config-file配置为nodes-XXX.conf
XXX为端口号, 同时设置好每个实例的日志和数据存储目录
启动Redis-Server,这里就不展示每一个的启动画面了
$ redis-server conf/redis6379.conf4909:C 18 Jun 2021 15:45:21.701 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo4909:C 18 Jun 2021 15:45:21.701 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=4909, just started4909:C 18 Jun 2021 15:45:21.701 # Configuration loaded4909:M 18 Jun 2021 15:45:21.702 * Increased maximum number of open files to 10032 (it was originally set to 1024).4909:M 18 Jun 2021 15:45:21.702 * monotonic clock: POSIX clock_gettime4909:M 18 Jun 2021 15:45:21.702 * No cluster configuration found, I'm 7f4d249cf670cd9015af61964ed45c720c01f1ae _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 6.2.4 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in cluster mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 4909 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-'4909:M 18 Jun 2021 15:45:21.705 # Server initialized4909:M 18 Jun 2021 15:45:21.705 # 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.4909:M 18 Jun 2021 15:45:21.706 * Ready to accept connections4909:M 18 Jun 2021 15:52:23.913 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH4909:M 18 Jun 2021 15:52:23.972 # IP address for this node updated to 127.0.0.14909:M 18 Jun 2021 15:52:28.882 # Cluster state changed: ok$ redis-server conf/redis6380.conf$ redis-server conf/redis6381.conf
可以从启动输出日志里面看到cluster的相关信息
执行Cluster启动设置
文档说明是redis-trib.rb脚本创建启动,但是执行后提示使用redis-cli执行。
$ redis-trib.rb create --replicas 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381WARNING: redis-trib.rb is not longer available!You should use redis-cli instead.All commands and features belonging to redis-trib.rb have been movedto redis-cli.In order to use them you should call redis-cli with the --clusteroption followed by the subcommand name, arguments and options.Use the following syntax:redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]Example:redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 --cluster-replicas 127.0.0.1:6379To get help about all subcommands, type:redis-cli --cluster help
正确的cluster建立命令
$ redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381>>> Performing hash slots allocation on 3 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383M: 7f4d249cf670cd9015af61964ed45c720c01f1ae 127.0.0.1:6379 slots:[0-5460] (5461 slots) masterM: 1e2b502694404174ffe002eb153c371805acb014 127.0.0.1:6380 slots:[5461-10922] (5462 slots) masterM: f79d6d16b940e706f08c416f26fa52f8a75c2273 127.0.0.1:6381 slots:[10923-16383] (5461 slots) masterCan 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 clusterWaiting for the cluster to join.>>> Performing Cluster Check (using node 127.0.0.1:6379)M: 7f4d249cf670cd9015af61964ed45c720c01f1ae 127.0.0.1:6379 slots:[0-5460] (5461 slots) masterM: 1e2b502694404174ffe002eb153c371805acb014 127.0.0.1:6380 slots:[5461-10922] (5462 slots) masterM: f79d6d16b940e706f08c416f26fa52f8a75c2273 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
中间有提示对master和槽位的分配,没有问题就输入yes就可以,后面会自动生成cluster 配置文件。
[OK] All 16384 slots covered.
最后输出这个就表示每个槽位都有一个节点在处理,一切正常。
下面来验证下效果:
通过redis-cli连接几个节点看看
$ redis-cli 127.0.0.1:6379> set key 1(error) MOVED 12539 127.0.0.1:6381127.0.0.1:6379> info# Serverredis_version:6.2.4redis_git_sha1:00000000redis_git_dirty:0redis_build_id:3597310f3a270994redis_mode:clusteros:Linux 5.4.0-74-generic x86_64arch_bits:64multiplexing_api:epollatomicvar_api:c11-builtingcc_version:7.5.0process_id:4909process_supervised:norun_id:22233f73aeb4f225ee7cc3b8210ecf4bab525f46tcp_port:6379server_time_usec:1624003248141743uptime_in_seconds:927uptime_in_days:0hz:10configured_hz:10lru_clock:13390512executable:/home/mark/redis/redis-serverconfig_file:/home/mark/redis/conf/redis6379.confio_threads_active:0.........# Replicationrole:masterconnected_slaves:0master_failover_state:no-failovermaster_replid:f259cb2f37d380de34a094e648b4e48b3dd48d43master_replid2:0000000000000000000000000000000000000000master_repl_offset:0second_repl_offset:-1repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0# CPUused_cpu_sys:0.682022used_cpu_user:0.815773used_cpu_sys_children:0.000000used_cpu_user_children:0.000000used_cpu_sys_main_thread:0.675267used_cpu_user_main_thread:0.819111# Modules# Errorstatserrorstat_ERR:count=2errorstat_MOVED:count=1# Clustercluster_enabled:1set key1234567 1OK127.0.0.1:6379> keys *1) "key1234567"
可以看到目前6379端口的服务已经在cluster下了,通过set 命令发现使用key为参数会被要求移到6381的服务节点,set 使用 key1234567参数,会被保存在当前节点.
我们再通过 redis-cli -c -p 6379 命令测试下,-c 是使用cluster mode执行
$ redis-cli -c -p 6379127.0.0.1:6379> set key 2-> Redirected to slot [12539] located at 127.0.0.1:6381OK127.0.0.1:6381> exit$ redis-cli -c -p 6381127.0.0.1:6381> keys *1) "key"127.0.0.1:6381> get key"2"127.0.0.1:6381>
现在看起来是ok的。
引用:
转载地址:http://ezrgf.baihongyu.com/