先去Redis官方下载Redis源码包 下载完成后上传至linux服务器 我这边用的是X Shell,将其解压
[root@localhost test]# tar zxvf redis-*.*.*.tar.gz然后进入解压后的文件夹,将其编译一下,也可以先安装一下gcc
[root@localhost redis-5.0.5]# yum install gcc [root@localhost redis-5.0.5]# make编译完成后可以验证一下,会有反馈的
[root@localhost redis-5.0.5]# make install Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install也可以进入src中遍历一下,发现多了很多文件
[root@localhost src]# ls adlist.c defrag.c lolwut.o redis-benchmark slowlog.c adlist.h defrag.o lzf_c.c redis-benchmark.c slowlog.h adlist.o dict.c lzf_c.o redis-benchmark.o slowlog.o ae.c dict.h lzf_d.c redis-check-aof solarisfixes.h ae_epoll.c dict.o lzf_d.o redis-check-aof.c sort.c ae_evport.c endianconv.c lzf.h redis-check-aof.o sort.o ae.h endianconv.h lzfP.h redis-check-rdb sparkline.c ae_kqueue.c endianconv.o Makefile redis-check-rdb.c sparkline.h ae.o evict.c Makefile.dep redis-check-rdb.o sparkline.o ae_select.c evict.o memtest.c redis-cli stream.h anet.c expire.c memtest.o redis-cli.c syncio.c anet.h expire.o mkreleasehdr.sh redis-cli.o syncio.o anet.o fmacros.h module.c redismodule.h testhelp.h aof.c geo.c module.o redis-sentinel t_hash.c aof.o geo.h modules redis-server t_hash.o asciilogo.h geohash.c multi.c redis-trib.rb t_list.c atomicvar.h geohash.h multi.o release.c t_list.o bio.c geohash_helper.c networking.c release.h t_set.c bio.h geohash_helper.h networking.o release.o t_set.o bio.o geohash_helper.o notify.c replication.c t_stream.c bitops.c geohash.o notify.o replication.o t_stream.o bitops.o geo.o object.c rio.c t_string.c blocked.c help.h object.o rio.h t_string.o blocked.o hyperloglog.c pqsort.c rio.o t_zset.c childinfo.c hyperloglog.o pqsort.h scripting.c t_zset.o childinfo.o intset.c pqsort.o scripting.o util.c cluster.c intset.h pubsub.c sdsalloc.h util.h cluster.h intset.o pubsub.o sds.c util.o cluster.o latency.c quicklist.c sds.h valgrind.sup config.c latency.h quicklist.h sds.o version.h config.h latency.o quicklist.o sentinel.c ziplist.c config.o lazyfree.c rand.c sentinel.o ziplist.h crc16.c lazyfree.o rand.h server.c ziplist.o crc16.o listpack.c rand.o server.h zipmap.c crc64.c listpack.h rax.c server.o zipmap.h crc64.h listpack_malloc.h rax.h setproctitle.c zipmap.o crc64.o listpack.o rax_malloc.h setproctitle.o zmalloc.c db.c localtime.c rax.o sha1.c zmalloc.h db.o localtime.o rdb.c sha1.h zmalloc.o debug.c lolwut5.c rdb.h sha1.o debugmacro.h lolwut5.o rdb.o siphash.c debug.o lolwut.c redisassert.h siphash.o这里按照惯例,把常用的文件给挪出去把 我是在test下建了一个redis文件夹,然后再建etc和bin文件夹,然后把几个常用的文件挪到这些文件夹中
[root@localhost test]# mkdir redis [root@localhost test]# cd redis [root@localhost redis]# mkdir bin [root@localhost redis]# mkdir etc [root@localhost redis]# mv ../redis-5.0.5/redis.conf etc/ [root@localhost redis]# cd ../redis-5.0.5/src/ [root@localhost src]# mv mkreleasehdr.sh redis-check-aof redis-cli redis-server ../../redis/bin/文件挪完后,开始修改redis配置文件,使用vim打开配置文件,用/直接搜索daemonize,使用N健快捷搜索下一个结果,A键开始编辑,将其改为YES(原本是NO,改过之后才会在后台运行),修改完Ctrl+C 输入:wq保存退出,这里我命令记得有点混乱,特贴一下网上复制过来的命令(侵删)
[root@localhost src]# cd ../../redis/etc/ [root@localhost etc]# vim redis.conf到这一步就可以启动了,我也真就启动了,给自己埋了个坑…,然后查了一下进程
[root@localhost etc]# redis-server redis.conf 11624:C 22 May 2019 11:44:47.752 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 11624:C 22 May 2019 11:44:47.752 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=11624, just started 11624:C 22 May 2019 11:44:47.752 # Configuration loaded [root@localhost etc]# ps -aux|grep redis Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ root 11461 0.0 0.0 139756 4968 pts/5 T 11:39 0:00 vim redis.conf root 11625 0.2 0.0 152412 7788 ? Ssl 11:44 0:00 redis-server 127.0.0.1:7777 root 11651 0.0 0.0 103260 852 pts/5 S+ 11:45 0:00 grep redis本地访问,设置了一下,好像确实没错,就是这个ip成了localhost,然后尝试换成服务器ip,发现行不通
[root@localhost bin]# redis-cli -h 127.0.0.1 -p 7777 127.0.0.1:7777> keys * (empty list or set) 127.0.0.1:7777> set gailun 10086 OK 127.0.0.1:7777> get gailun "10086" 127.0.0.1:7777> del gailun (integer) 1 127.0.0.1:7777> keys * (empty list or set) 127.0.0.1:7777> quit [root@localhost bin]# redis-cli -h *.*.*.* -p 7777 Could not connect to Redis at *.*.*.*:7777: Connection refused not connected> not connected> quit这里得自己填坑了,百度了一下发现redis配置文件里默认处于保护模式,只能本地链接,这里要修改两个地方,还是vim redis.conf 文件,搜索/bind 找到这一行
bind 127.0.0.1将其注释
# bind 127.0.0.1再将保护模式关掉
protected-mode no杀死进程,重新启动(有些人说可以直接启动,我这边启动后连接时会报错),所以我选择先杀死相关进程,再启动
[root@localhost bin]# redis-cli -h *.*.*.* -p 7777 *.*.*.*:7777> keys * (error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside. [root@localhost bin]# ps -ef|grep redis root 16334 1 0 14:29 ? 00:00:02 redis-server *:7777 root 16781 6783 0 14:46 pts/5 00:00:00 grep redis [root@localhost bin]# kill -s 9 16334 [root@localhost bin]# ps -ef|grep redis root 16792 6783 0 14:46 pts/5 00:00:00 grep redis [root@localhost bin]# redis-server ../etc/redis.conf [root@localhost bin]# redis-server ../etc/redis.conf 16894:C 22 May 2019 14:50:47.113 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 16894:C 22 May 2019 14:50:47.113 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=16894, just started 16894:C 22 May 2019 14:50:47.113 # Configuration loaded [root@localhost bin]# ps -ef|grep redis root 16895 1 0 14:50 ? 00:00:00 redis-server *:7777 root 16910 6783 0 14:51 pts/5 00:00:00 grep redis [root@localhost bin]# redis-cli -h 172.16.33.85 -p 7777 172.16.33.85:7777> keys * (empty list or set) 172.16.33.85:7777> set gailun zhaohuan OK到这里,一个redis服务器搭建好了,本地cmd连接一下
telnet *.*.*.* 7777ok,redis搭建起来了。
接下来才是重头戏------------------------------------集群的搭建 在usr local下创建文件夹redis-cluster,然后在其下面分别创建配置文件的文件夹(我这边找不到太多的服务器,所以选择了两个服务器,一边创建三个节点)
[root@localhost src]# mkdir -p /usr/local/redis-cluster [root@localhost src]# cd /usr/local/redis-cluster [root@localhost redis-cluster]# mkdir 7777 [root@localhost redis-cluster]# mkdir 7778 [root@localhost redis-cluster]# mkdir 7779将redis.conf文件复制到7777文件夹下
[root@localhost redis-cluster]# cp /root/redis/redis-5.0.5/redis.conf 7777/redis.confvim一下进入修改,主要修改一下几个地方(基本都有,不需要新增,用"/"搜索到之后删除掉注释符再修改即可)
daemonize yes port 7777 bind *.*.*.*(本机ip) dir /usr/local/redis-cluster/7777/(指定数据文件的存放位置) cluster-enabled yes(启用集群模式) cluster-config-file nodes-7777.conf cluster-node-timeout 5000 appendonly yes修改完之后强制保存一下,然后将其复制到其他相关文件夹中,并批量修改文件中的端口
:%s/7777/7778/g :wq!强制保存一下。 接下来遇到一个大坑啊。。。。。 安装集群需要使用ruby命令,公开课上讲的时候好像一切都很顺利
yum install ruby yum install rubygems yum install redis --version 3.0.0 中间省略掉一个一个启动redis节点的指令。。。一套命令下来准备执行redis-trib.rb命令创建集群的时候--------------------------------------崩了 原因是ruby版本太低 好吧,找了下解决办法,踩进另一个坑,rvm安装不下来 这边省略其间各种心酸,贴出解决办法: 手动安装高版本ruby
链接: https://pan.baidu.com/s/19NV8b7PY6DhJX1VqqvKkeg 提取码: 9scv
将三个文件上传到redis目录(随意) 解压一下,再编译一下
[root@localhost redis]# tar -zxvf ruby-2.3.1.tar.gz [root@localhost redis]# cd ruby-2.3.1 [root@localhost ruby-2.3.1]# ./configure -prefix=/usr/local/ruby [root@localhost ruby-2.3.1]# make [root@localhost ruby-2.3.1]# cd /usr/local/ruby/ [root@localhost ruby]# cp bin/ruby /usr/local/bin [root@localhost ruby]# cp bin/gem /usr/local/bin [root@localhost ruby]# gem install redis --version 3.0.0 Successfully installed redis-3.0.0 1 gem installed Installing ri documentation for redis-3.0.0... Installing RDoc documentation for redis-3.0.0... [root@localhost ruby]# cd ../ [root@localhost local]# cd ../../root/redis/ [root@localhost redis]# gem install -l redis-3.3.0.gem到这里,差不多已经成功了,再试了一下公开课上的命令,发现似乎已经过时了。。。
[root@localhost redis]# cd redis-5.0.5/src [root@localhost src]# ./redis-trib.rb create --replicas 1 *.*.*.89:7777 *.*.*.85:7777 *.*.*.89:7778 *.*.*.85:7778 *.*.*.85:7779 *.*.*.85:7779 WARNING: redis-trib.rb is not longer available! You should use redis-cli instead. All commands and features belonging to redis-trib.rb have been moved to redis-cli. In order to use them you should call redis-cli with the --cluster option followed by the subcommand name, arguments and options. Use the following syntax: redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS] Example: redis-cli --cluster create *.*.*.89:7777 *.*.*.85:7777 *.*.*.*:7778 *.*.*.85:7778 *.*.*.85:7779 *.*.*.85:7779 --cluster-replicas 1 To get help about all subcommands, type: redis-cli --cluster help那就听官方的,尝试一下
[root@localhost bin]#redis-cli --cluster create *.*.*.89:7777 *.*.*.85:7777 *.*.*.*:7778 *.*.*.85:7778 *.*.*.85:7779 *.*.*.89:7779 --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 *.*.*.85:7779 to *.*.*.89:7777 Adding replica *.*.*.85:7779 to *.*.*.85:7777 Adding replica *.*.*.85:7778 to *.*.*.89:7778 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 20cc50a841151e7ee18dd5be7393af6e28e144e4 *.*.*.89:7777 slots:[0-5460] (5461 slots) master M: 043d6abc64a82e0e145bf2ccb283c941c09f3e71 *.*.*.85:7777 slots:[5461-10922] (5462 slots) master M: fe7d3030ac80593ca02ea4817cac7b2f612e8382 *.*.*.89:7778 slots:[10923-16383] (5461 slots) master S: 26953f5755a5a5e88160c4aeace1e603feda5f22 *.*.*.85:7778 replicates 20cc50a841151e7ee18dd5be7393af6e28e144e4 S: a309c9ea7fdba337d8a434784bba91e54280a703 *.*.*.85:7779 replicates 043d6abc64a82e0e145bf2ccb283c941c09f3e71 S: a309c9ea7fdba337d8a434784bba91e54280a703 *.*.*.85:7779 replicates fe7d3030ac80593ca02ea4817cac7b2f612e8382 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 *.*.*.89:7777) M: 20cc50a841151e7ee18dd5be7393af6e28e144e4 *.*.*.89:7777 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 26953f5755a5a5e88160c4aeace1e603feda5f22 *.*.*.85:7778 slots: (0 slots) slave replicates 20cc50a841151e7ee18dd5be7393af6e28e144e4 S: a309c9ea7fdba337d8a434784bba91e54280a703 *.*.*.85:7779 slots: (0 slots) slave replicates fe7d3030ac80593ca02ea4817cac7b2f612e8382 M: 043d6abc64a82e0e145bf2ccb283c941c09f3e71 *.*.*.85:7777 slots:[5461-10922] (5462 slots) master M: fe7d3030ac80593ca02ea4817cac7b2f612e8382 *.*.*.89:7778 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 bin]# ./redis-cli -c -h *.*.*.85 -p 7777 *.*.*.85:7777> CLUSTER INFO cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:5 cluster_size:3 cluster_current_epoch:5 cluster_my_epoch:2 cluster_stats_messages_ping_sent:121 cluster_stats_messages_pong_sent:129 cluster_stats_messages_meet_sent:4 cluster_stats_messages_sent:254 cluster_stats_messages_ping_received:128 cluster_stats_messages_pong_received:125 cluster_stats_messages_meet_received:1 cluster_stats_messages_received:254 *.*.*.85:7777> CLUSTER NODES 20cc50a841151e7ee18dd5be7393af6e28e144e4 *.*.*.89:7777@17777 master - 0 1559296909928 1 connected 0-5460 043d6abc64a82e0e145bf2ccb283c941c09f3e71 *.*.*.85:7777@17777 myself,master - 0 1559296908000 2 connected 5461-10922 26953f5755a5a5e88160c4aeace1e603feda5f22 *.*.*.85:7778@17778 slave 20cc50a841151e7ee18dd5be7393af6e28e144e4 0 1559296910529 4 connected fe7d3030ac80593ca02ea4817cac7b2f612e8382 *.*.*.89:7778@17778 master - 0 1559296910530 3 connected 10923-16383 a309c9ea7fdba337d8a434784bba91e54280a703 *.*.*.85:7779@17779 slave fe7d3030ac80593ca02ea4817cac7b2f612e8382 0 1559296909000 5 connectedCompleted!
还是不要在搭建前设置密码了,搭建成功后再连接各个节点设置密码吧
redis-cli -c -h *.*.*.* -p 7777 config set masterauth password config set requirepass password auth password config rewrite