摘 要:本文主要介绍在离线情况下,Redis集群的安装方法、步骤以及注意事项。
1、环境准备
(1)查看服务器是否安装gcc-c++编译环境
yum list gcc-c++
已安装
未安装
(2)安装gcc-c++编译环境
yum install -y gcc-c++
2、依赖包
- apr-1.3.9-5.el6_2.src.rpm
- apr-util-1.3.9-3.el6_0.1.src.rpm
- curl-7.19.7-52.el6.src.rpm
- httpd-2.2.15-59.el6.centos.src.rpm
- mysql-5.1.73-8.el6_8.src.rpm
- openssl-1.0.1e-57.el6.src.rpm
- zlib-1.2.3-29.el6.src.rpm
3、安装依赖包
rpm -ivh apr-1.3.9-5.el6_2.src.rpm
注:此时会报错,需添加mockbuild组,同时使用root用户。
groupadd mockbuild useradd mockbuild -g mockbuild rpm -ivh apr-1.3.9-5.el6_2.src.rpm rpm -ivh apr-util-1.3.9-3.el6_0.1.src.rpm rpm -ivh curl-7.19.7-52.el6.src.rpm rpm -ivh httpd-2.2.15-59.el6.centos.src.rpm rpm -ivh mysql-5.1.73-8.el6_8.src.rpm rpm -ivh openssl-1.0.1e-57.el6.src.rpm rpm -ivh zlib-1.2.3-29.el6.src.rpm
2、安装redis: (redis-4.0.6.tar.gz)
(1)将redis-4.0.6.tar.gz离线包上传至/data/redis_cluster目录
mkdir /data/redis_cluster cd /data/redis_cluster redis-4.0.6.tar.gz
(2)解压redis离线包,编译安装
tar -zxvf redis-4.0.6.tar.gz cd redis-4.0.6 make 如果报错
解决办法:make 后面带参数 make MALLOC=libc make && make install
5、安装ruby:(ruby-2.4.3.tar.gz)
(1)将ruby-2.4.3.tar.gz离线包上传至/data/redis_cluster目录
(2)解压,编译ruby
cd /data/redis_cluster tar -zxvf ruby-2.4.3.tar.gz cd ruby-2.4.3 ./configure make make install
6、安装zlib:(zlib-1.2.11.tar.gz)
(1)将zlib-1.2.11.tar.gz离线包上传至/data/redis_cluster目录
(2)解压,编译zlib
cd /data/redis_cluster tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure --prefix=/usr/local/zlib make make install
(3)进入ruby-2.4.3目录
cd ../ruby-2.4.3 cd ext/zlib ruby extconf.rb --with-zlib-include=/usr/local/zlib/include/ --with-zlib-lib=/usr/local/zlib/lib make && make install 注:此时会报错,方法: vi Makefile 将文件中所有的$(top_srcdir)替换为../..
:%s/$(top_srcdir)/../../g
修改完成,然后保存 make && make install
7、安装openssl:(openssl-1.0.2j.tar.gz)
(1)将openssl-1.0.2j.tar.gz离线包上传至/data/redis_cluster目录
(2)解压、编译openssl
cd /data/redis_cluster tar -zxvf openssl-1.0.2j.tar.gz cd openssl-1.0.2j ./config -fPIC --prefix=/usr/local/openssl enable-shared ./config -t make && make install
(3)进入ruby-2.4.3目录
cd ../ruby-2.4.3 cd ext/openssl ruby extconf.rb --with-openssl-include=/usr/local/openssl/include/ --with-openssl-lib=/usr/local/openssl/lib make && make install 注:此时会报错,方法: vi Makefile 将文件中所有的$(top_srcdir)替换为../..(特别多),可以考虑将Makefile下载到本地修改完成再上传至服务器该目录。 或者使用此命令直接替换 :%s/$(top_srcdir)/../../g 修改完成,然后保存 make && make install
8、安装redis-4.0.0.gem
(1)将redis-4.0.0.gem离线包上传至/data/redis_cluster目录
(2)安装redis-4.0.0.gem
gem install redis-4.0.0.gem
9、配置redis集群
注意:以上8个步骤是在192.168.52.130服务器操作,192.168.52.131
需要执行相同的操作
ip:
192.168.52.130 192.168.52.131
(1)192.168.52.130
cd /data/redis_cluster
创建3个节点6379、6380、6381
mkdir 6379 mkdir 6380 mkdir 6381 cp redis-4.0.6/redis.conf 6379 cp redis-4.0.6/redis.conf 6380 cp redis-4.0.6/redis.conf 6381 编译文件:vi 6379/redis.conf 编译文件:vi 6380/redis.conf 编译文件:vi 6381/redis.conf
(2)将文件中的对应信息填好
mkdir /data/redis_cluster/redis-4.0.6/log cd /data/redis_cluster vim 6379/redis.conf 69:bind x.x.x.x #将bing注释(#bind) 88:protected-mode no 92:port 6379 #端口6379,6380 ,6381 136:daemonize yes #redis后台运行 158:pidfile /var/run/redis_6379.pid #pidfile文件对应6379,6380 ,6381 171:logfile /data/redis_cluster/redis-4.0.6/log/${自定义}.log #${自定义}可以写成6379.log、6380.log...依次对应6379/redis.conf...... 263:dir “/data/redis_cluster/6379” #数据目录 814:cluster-enabled yes #开启集群 把注释#去掉 822:cluster-config-file nodes_6379.conf #集群的配置,配置文件首次启动自动生成 6379,6380 ,6381 cluster-node-timeout 5000 #请求超时,设置5秒够了 appendonly yes #aof日志开启 有需要就开启,它会每次写操作都记录一条日志
(3)192.168.52.131
cd /data/redis_cluster 创建3个节点6382、6383、6384 mkdir 6382 mkdir 6383 mkdir 6384 cp redis-4.0.6/redis.conf 6382 cp redis-4.0.6/redis.conf 6383 cp redis-4.0.6/redis.conf 6384 编译文件:vi 6382/redis.conf 编译文件:vi 6383/redis.conf 编译文件:vi 6384/redis.conf
(4)将文件中的对应信息填好
mkdir /data/redis_cluster/redis-4.0.6/log cd /data/redis_cluster vim 6382/redis.conf daemonize yes #redis后台运行 pidfile /var/run/redis_6382.pid #pidfile文件对应6382,6383 ,6384 protected-mode no port 6382 #端口6382,6383 ,6384 cluster-enabled yes #开启集群 把注释#去掉 cluster-config-file nodes_6382.conf #集群的配置,配置文件首次启动自动生成 6382,6383 ,6384 dir “/data/redis_cluster/6382” #数据目录 cluster-node-timeout 5000 #请求超时,设置5秒够了 appendonly yes #aof日志开启 有需要就开启,它会每次写操作都记录一条日志 logfile /data/redis_cluster/redis-4.0.6/log/${自定义}.log #${自定义}可以写成6382.log、6383.log...依次对应6382/redis.conf...... bind x.x.x.x #将bing注释(#bind)
10、启动redis服务
(1)192.168.52.130
cd /data/redis_cluster redis-4.0.6/src/redis-server 6379/redis.conf redis-4.0.6/src/redis-server 6380/redis.conf redis-4.0.6/src/redis-server 6381/redis.conf
(2)192.168.52.131
cd /data/redis_cluster redis-4.0.6/src/redis-server 6382/redis.conf redis-4.0.6/src/redis-server 6383/redis.conf redis-4.0.6/src/redis-server 6384/redis.conf
11、查看redis服务
ps -ef | grep redis #查看是否启动成功 netstat -tnlp | grep redis #可以看到redis监听端口
12、启动redis集群
cd /data/redis_cluster ./redis-4.0.6/src/redis-trib.rb create --replicas 1 192.168.52.130:6379 192.168.52.130:6380 192.168.52.130:6381 192.168.52.131:6382 192.168.52.131:6383 192.168.52.131:6384
13、登陆redis集群
cd /data/redis_cluster ./redis-4.0.6/src/redis-cli -h 192.168.52.130 -p 6379 -c ./redis-4.0.6/src/redis-cli -h 192.168.52.130 -p 6380 -c ./redis-4.0.6/src/redis-cli -h 192.168.52.130 -p 6381 -c ./redis-4.0.6/src/redis-cli -h 192.168.52.131 -p 6382 -c ./redis-4.0.6/src/redis-cli -h 192.168.52.131 -p 6383 -c ./redis-4.0.6/src/redis-cli -h 192.168.52.131 -p 6384 –c 集群信息:登录以后:cluster info
14、redis集群设置密码
(1)使用redis-trib.rb工具构建集群,集群构建完成前不要配置密码,集群构建完毕再通过config set 命令逐个redis实例设置密码。
(2)对集群设置密码,那么requirepass和masterauth都需要设置,否则发生主从切换时,就会遇到授权问题。
(3)各个实例的密码都必须一致,否则Redirected就会失败
方法:
cd /data/redis_cluster 1)、./redis-4.0.6/src/redis-cli -h 192.168.52.130 -p 6379 -c 192.168.52.130:6379> config set masterauth fdmp!@# 192.168.52.130:6379> config set requirepass fdmp!@# 192.168.52.130:6379> CONFIG REWRITE (error) NOAUTH Authentication required. 192.168.52.130:6379> auth fdmp!@# 192.168.52.130:6379> CONFIG REWRITE 192.168.52.130:6379> exit 2)、./redis-4.0.6/src/redis-cli -h 192.168.52.130 -p 6380 -c 192.168.52.130:6380> config set masterauth fdmp!@# 192.168.52.130:6380> config set requirepass fdmp!@# 192.168.52.130:6380> CONFIG REWRITE (error) NOAUTH Authentication required. 192.168.52.130:6380> auth fdmp!@# 192.168.52.130:6380> CONFIG REWRITE 192.168.52.130:6380> exit 3)、./redis-4.0.6/src/redis-cli -h 192.168.52.130 -p 6381 -c 192.168.52.130:6381> config set masterauth fdmp!@# 192.168.52.130:6381> config set requirepass fdmp!@# 192.168.52.130:6381> CONFIG REWRITE (error) NOAUTH Authentication required. 192.168.52.130:6381> auth fdmp!@# 192.168.52.130:6381> CONFIG REWRITE 192.168.52.130:6381> exit 4)、./redis-4.0.6/src/redis-cli -h 192.168.52.131 -p 6382 -c 192.168.52.131:6382> config set masterauth fdmp!@# 192.168.52.131:6382> config set requirepass fdmp!@# 192.168.52.131:6382> CONFIG REWRITE (error) NOAUTH Authentication required. 192.168.52.131:6382> auth fdmp!@# 192.168.52.131:6382> CONFIG REWRITE 192.168.52.131:6382> exit 5)、./redis-4.0.6/src/redis-cli -h 192.168.52.131 -p 6383 -c 192.168.52.131:6383> config set masterauth fdmp!@# 192.168.52.131:6383> config set requirepass fdmp!@# 192.168.52.131:6383> CONFIG REWRITE (error) NOAUTH Authentication required. 192.168.52.131:6383> auth fdmp!@# 192.168.52.131:6383> CONFIG REWRITE 192.168.52.131:6383> exit 6)、./redis-4.0.6/src/redis-cli -h 192.168.52.131 -p 6384 -c 192.168.52.131:6384> config set masterauth fdmp!@# 192.168.52.131:6384> config set requirepass fdmp!@# 192.168.52.131:6384> CONFIG REWRITE (error) NOAUTH Authentication required. 192.168.52.131:6384> auth fdmp!@# 192.168.52.131:6384> CONFIG REWRITE 192.168.52.131:6384> exit
(4)设置密码之后如果需要使用redis-trib.rb的各种命令报错问题
如:
cd /data/redis_cluster/redis-4.0.6/src ./redis-trib.rb check 192.168.52.130:6379 [ERR] Sorry, can't connect to node 192.168.52.130:6379
解决办法:
找到建立集群时执行:gem install redis-4.0.0.gem
生成的client.rb文件,如果不知道该文件在哪可以通过下面命令查找
find / -name client.rb /usr/local/lib/ruby/gems/2.4.0/gems/xmlrpc-0.2.1/lib/xmlrpc/client.rb /usr/local/lib/ruby/gems/2.4.0/gems/redis-4.0.0/lib/redis/client.rb /data/redis_cluster/ruby-2.4.3/gems/xmlrpc-0.2.1/lib/xmlrpc/client.rb
我这边的那个文件是:
/usr/local/lib/ruby/gems/2.4.0/gems/redis-4.0.0/lib/redis/client.rb
然后修改该文件
vim /usr/local/lib/ruby/gems/2.4.0/gems/redis-4.0.0/lib/redis/client.rb
修改password对应的值然后保存,就可以了