1、安装Redis
解压 tar -xzvf redis-3.2.8.tar.gz
编译安装 cd /data/redis-3.2.8
make && make install
[root@centos redis-3.2.8]# make && make install cd src && make all make[1]: Entering directory `/data/redis-3.2.8/src' CC adlist.o /bin/sh: cc: command not found make[1]: *** [adlist.o] Error 127 make[1]: Leaving directory `/data/redis-3.2.8/src' make: *** [all] Error 2
提示gcc和cc命令令未找到
解决方法
yum -y install gcc gcc-c++ libstdc++-devel
再次执行make && make install
cc: error: ../deps/hiredis/libhiredis.a: No such file or directory cc: error: ../deps/lua/src/liblua.a: No such file or directory cc: error: ../deps/geohash-int/geohash.o: No such file or directory cc: error: ../deps/geohash-int/geohash_helper.o: No such file or directory make[1]: *** [redis-server] Error 1 make[1]: Leaving directory `/data/redis-3.2.8/src' make: *** [all] Error 2
解决方法
cd deps/
make lua hiredis linenoise
再次执行make && make install
[root@centos redis-3.2.8]# make && make install cd src && make all make[1]: Entering directory `/data/redis-3.2.8/src' LINK redis-server cc: error: ../deps/geohash-int/geohash.o: No such file or directory cc: error: ../deps/geohash-int/geohash_helper.o: No such file or directory make[1]: *** [redis-server] Error 1 make[1]: Leaving directory `/data/redis-3.2.8/src' make: *** [all] Error 2
解决方法:
进入源码包目录下的deps目录中执行
make geohash-int hiredis jemalloc linenoise lua
再次make && make install
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@centos redis-3.2.8]# make && make install cd src && make all make[1]: Entering directory `/data/redis-3.2.8/src' LINK redis-server INSTALL redis-sentinel CC redis-cli.o LINK redis-cli CC redis-benchmark.o LINK redis-benchmark INSTALL redis-check-rdb CC redis-check-aof.o LINK redis-check-aof Hint: It 's a good idea to run ' make test' ;) make[1]: Leaving directory `/data/redis-3.2.8/src' cd src && make install make[1]: Entering directory `/data/redis-3.2.8/src' Hint: It 's a good idea to run ' make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory `/data/redis-3.2.8/src' |
cd redis-5.0.8
cp redis.conf /data/redis-cluster/conf/redis_7000.conf
cp redis.conf /data/redis-cluster/conf/redis_7001.conf
2.修改Redis配置文件
1、修改redis-master配置文件
vi /data/redis-cluster/conf/redis_7000.conf
port 7000 #修改redis监听端口(可以自定义) bind 0.0.0.0 #表示redis允许所有地址连接。默认127.0.0.1,仅允许本地连接。 daemonize yes #允许redis后台运行 pidfile /var/run/redis_7000.pid #pid存放目录 logfile "/var/log/redis-sentinel.log" #设置Sentinel日志存放路径 dir /data/redis-cluster/data/redis_7000 #工作目录 cluster-enabled yes #是否开启集群 cluster-config-file /data/redis-cluster/conf/nodes_7000.conf #集群配置文件的名称,每个节点都有一个集群相关的配置文件,持久化保存集群的信息 #这个文件并不需要手动配置,这个配置文件有Redis生成并更新, cluster-node-timeout 15000 #节点互连超时的阀值。集群节点超时毫秒数,默认15秒 appendonly yes #Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件, #每次启动时Redis都会先把这个文件的数据读入内存里,先忽略RDB文件。 requirepass 123456 #设置redis密码 masterauth 123456 #主从同步master的密码(如果没有设置redis密码,则无需配置)
2、修改redis-slave配置文件
vi /data/redis-cluster/conf/redis_7001.conf #修改redis-slave配置文件,具体如下:
1 2 3 4 5 6 7 8 9 10 11 12 | port 7001 bind 0.0.0.0 daemonize yes pidfile / var /run/redis-cluster/redis_7001.pid logfile "/data/redis-cluster/log/redis_7001.log" dir /data/redis-cluster/data/redis_7001 cluster-enabled yes cluster-config-file /data/redis-cluster/conf/nodes_7001.conf cluster-node-timeout 15000 appendonly yes requirepass 123456 masterauth 123456 |
3.分发到集群其他服务器
复制redis以及工作目录到其他服务器
scp -r /data/redis-3.2.8 redis-cluster root@10.58.20.16:/data
scp -r /data/redis-3.2.8 redis-cluster root@10.58.20.17:/data
设置软链接,方便启动redis服务
ln -s /data/redis-3.2.8/src/redis-server /usr/bin/redis-server
ln -s /data/redis-3.2.8/src/redis-cli /usr/bin/redis-cli
4.启动Redis
集群内每台服务器分别启动两个redis
redis-server /data/redis-cluster/conf/redis_7000.conf
redis-server /data/redis-cluster/conf/redis_7001.conf
验证是否启动成功
ps -ef | grep redis
查看版本
redis-cli --version
redis-server --version
5.创建Redis Cluster
复制redis-trib.rb
redis-trib.rb 复制到 /usr/local/bin 目录
cd src
cp redis-trib.rb /usr/local/bin/
注意:redis5.0以上集群创建方式改为了C编写的redis-cli创建,不用再安装麻烦的ruby。
创建集群,--cluster-replicas 1指定从库数量1,创建顺序三主-三从。即主-主-主-从-从-从。
redis-trib.rb create --replicas 1 10.58.20.15:7000 10.58.20.16:7000 10.58.20.17:7000 10.58.20.15:7001 10.58.20.16:7001 10.58.20.17:7001
[root@centos src]# redis-trib.rb create --replicas 1 10.58.20.15:7000 10.58.20.16:7000 10.58.20.17:7000 10.58.20.15:7001 10.58.20.16:7001 10.58.20.17:7001 /usr/bin/env: ruby: No such file or directory
若执行以上命令报错,则服务器上没有安装 ruby,需要参考下一步骤安装ruby
安装ruby
下载ruby-2.4.0.tar.gz ,并安装
下载Ruby的归档文件后,解压后得到文件并转入到新创建的目录:
$ tar -xvzf ruby-2.4.0.tar.gz
$ cd ruby-2.4.0
配置和编译源代码
$ ./configure
最后安装
$ su root
$ make install
$ exit
redis还需要下载并安装ruby的第三方接口
下载 redis-3.3.3.gem
并安装: gem install -l redis-3.3.3.gem
若安装 gem报错,则是缺少openssl ,需要先安装openssl 才行
yum install openssl ,再执行 install -l redis-3.3.3.gem
1 2 3 4 5 | [root@zhoulei rubygems-2.6.11]# gem install -l redis-3.3.3.gem ERROR: Loading command: install (LoadError) cannot load such file -- zlib ERROR: While executing gem ... (NoMethodError) undefined method `invoke_with_build_args' for nil:NilClass |
解决方法:
既然没有zlib,那我们进行安装
(1)yum -y install zlib-devel
(2)cd ruby-2.4.0/ext/zlib/
(3)ruby ./extconf.rb
(4)make
(5)make install
紧接着报这错:
1 2 | [root@zhoulei zlib]# make make: *** No rule to make target `/include/ruby.h ', needed by `zlib.o' . Stop. |
解决办法:
find / -name 'client.rb'
vi /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/client.rb
修改路径 为../../include/ruby.h
make
make install
gem install -l redis-3.3.3.gem
完成。
集群时出现的问题:
[root@centos data]# redis-trib.rb create --replicas 1 10.58.20.15:7000 10.58.20.16:7000 10.58.20.17:7000 10.58.20.15:7001 10.58.20.16:7001 10.58.20.17:7001
>>> Creating cluster
[ERR] Sorry, can't connect to node 10.58.20.15:7000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Client DEFAULTS = { :url => lambda { ENV[ "REDIS_URL" ] }, :scheme => "redis" , :host => "127.0.0.1" , :port => 6379, :path => nil, :timeout => 5.0, :password => "passwd123" , :db => 0, :driver => nil, :id => nil, :tcp_keepalive => 0, :reconnect_attempts => 1, :inherit_socket => false |
以后部署不使用3.2.8版本了,折腾了一晚,之后改用5.0版本。
redis5.0以上集群创建方式改为了C编写的redis-cli创建,不用再安装麻烦的ruby。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义