摘要
- redis4.0单机的安装与配置
- redis4.0集群的安装与配置
- redis4.0集群安装配置踩坑全纪录
ubuntu单机安装与配置redis4.0
环境
ubuntu14.04 LTS
准备工作
1、下载
下载redis4.0.9,网址
http://download.redis.io/releases/
2、安装GCC
sudo apt-get build-dep gcc
#or
sudo apt-get install build-essential
安装与配置redis
1、编译
将redis-4.0.9.tar.gz放在usr/server中,然后
#解压
tar zxvf redis-4.0.9.tar.gz
#编译
cd /usr/server/redis-4.0.9
make
2、修改配置文件
sudo vim redis.conf
#修改如下几项
1、注释掉bind,去掉本地回环网络,保证别的机器访问
#bind 127.0.0.1
2、修改daemonize,使其可以守护进程运行
daemonize yes
3、持久化,如需关闭,则注释掉save配置
#save 900 1
#save 300 10
#save 60 10000
4、protected-mode 改为no,即可允许别的机器访问这个redis
5、配置密码
requirepass 818
3、 启动、测试与关闭redis
cd redis-4.0.9/src
# 启动redis
./redis-server
# 进入客户端,测试redis
./redis-cli -h 127.0.0.1 -p 6379
#如果有密码需在进入客户端后输入
auth 818
#测试
set a like
get a
#like
#关闭redis
./redis-cli shutdown
ubuntu14安装与配置redis4.0集群
环境
三台ubuntu14.04 LTS,ip为:
xxx.133.17.168
xxx.133.17.169
xxx.133.17.170
安装与配置redis集群
1、根据“单机安装与配置redis4.0”的步骤,在三台机器分别安装redis并编译。
2、在xxx.133.17.168机器,进入编译后的redis目录下,创建两个文件夹,作为两个redis节点存放配置文件的路径(其他机器也如此,节点从7000分配到7005)。
mkdir -p redis-cluster/7000
mkdir -p redis-cluster/7001
3、把默认配置文件复制到节点文件夹(其他机器也如此)。
cp redis.conf redis-cluster/7000
cp redis.conf redis-cluster/7001
4、修改6个节点的redis.conf(以7000为例)。
sudo vim redis-cluster/7000/redis.conf
bind 127.0.0.1 xxx.133.17.168
port 7000
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 15000
requirepass 818
5、进入redis的src文件夹,分别启动这6个节点。
./redis-server ../redis-cluster/7000/redis.conf
#查看是否启动成功
ps aux | grep redis
6、安装ruby环境,这里特比强调,由于之前是直接安装ruby,总是报缺少oepnssl的错误,也踩过很多其他的坑,所以要用rvm安装ruby。
curl -L get.rvm.io | bash -s stable
echo "source ~/.bashrc" >> ~/.bash_profile
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
source ~/.bashrc
rvm -v
rvm install 2.3.3
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
gem install redis
./redis-trib.rb create --replicas 1 xxx.xxx.xxx.168:7000 xxx.xxx.xxx.168:7001 xxx.xxx.xxx.169:7002 xxx.xxx.xxx.169:7003 xxx.xxx.xxx.170:7004 xxx.xxx.xxx170:7005
输入yes
出现
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
120.133.17.168:7000
120.133.17.169:7002
120.133.17.170:7004
Adding replica 120.133.17.169:7003 to 120.133.17.168:7000
Adding replica 120.133.17.170:7005 to 120.133.17.169:7002
Adding replica 120.133.17.168:7001 to 120.133.17.170:7004
.
.
.
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
大功告成!
7、启动集群并测试。
- 进入两个节点的客户端,一个set,另一个get,看是否可以get到。
./redis-cli -c -h xxx.133.17.168 -p 7001
auth 818
set a like
./redis-cli -c -h xxx.133.17.170 -p 7003
get a
出现like,搞定。
- 在某节点的redis-cli下,执行cluster info,出现
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:304
cluster_stats_messages_pong_sent:277
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:584
cluster_stats_messages_ping_received:274
cluster_stats_messages_pong_received:307
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:584
8、单机未设定密码,为集群添加密码。
- 进入6个节点的客户端(以7000为例)
redis-cli -h xxx.133.17.168 -p 7000
config set masterauth 818
config set requirepass 818
auth 818
config rewrite
exit
- 修改集群启动文件
find / -name client.rb
找到带GEM的
sudo vim /home/rdadmin/.rvm/gems/ruby-2.3.3/gems/redis-4.0.1/lib/redis/client.rb
修改这一项:
:password => "818",
具体如下:
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "818",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
修改后登录客户端时,需要两步:
./redis-cli -c -h 192.168.1.104 -p 7001
auth 818
9、重启集群
每台机器,pkill -9 redis ,再挨个重启6个节点,好像就OK了。。。
又有人说redis集群重启需要删除各个节点下的文件,删除命令如下:
rm -rf appendonly.aof | rm -rf dump.rdb | rm -rf nodes.conf | rm -rf redis.log
待验证。。。
ubuntu安装与配置redis4.0集群踩坑历程
在安装配置ruby时,踩了N多坑,在此记录下来。
1、未安装ruby时,在src下执行
./redis-trib.rb create --replicas 1 120.133.17.168:7000 120.133.17.168:7001
120.133.17.169:7002 120.133.17.169:7003 120.133.17.170:7004 120.133.17.170:7005
会出现redis-trib.rb: command not found,说明需要安装ruby。
2、安装ruby
sudo apt-get install ruby
sudo gem install redis
此时会提示ruby版本太低,然后按照一个教程安了最新的ruby2.4.4
参考链接:https://blog.csdn.net/fengbingchun/article/details/61614738
安装最新的ruby后,再sudo gem install redis,又出现Could not find a valid gem 'redis' (>= 0) in any repository
通过gem source查看ruby是否在gem的source里,但是出现新的错误,Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
网上又搜到一篇很复杂的ssl安装配置教程,还是不行。
最后搜到正确的方案是通过rvm安装ruby,会自动配置openssl,于是安装rvm:
curl -L get.rvm.io | bash -s stable
echo "source ~/.bashrc" >> ~/.bash_profile
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
source ~/.bashrc
rvm -v
type rvm | head -1
rvm is a function
rvm install 2.3.3
再次运行命令gem install redis 发现卡死,换个源吧:
gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
再次gem install redis
然后在redis的src
./redis-trib.rb create --replicas 1 xxx.133.17.168:7000 xxx.133.17.168:7001 xxx.133.17.169:7002 xxx.133.17.169:7003 xxx.133.17.170:7004 xxx.133.17.170:7005
出现 [ERR] Sorry, can't connect to node 120.133.17.168:7000
原来是忘启动redis服务了
./redis-server ../redis-cluster/7000/redis.conf后再./redis-trib.rb
出现Node xxx.133.17.168:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
原来是之前的redis被使用过,里面有内容,此时需要:
redis-cli -h xxx.133.17.168 -p 7000 -a 818
redis-cli -h xxx.xxx.xxx.168 -p 7000 flushdb
然后执行./redis-trib.rb,出现
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
xxx.133.17.168:7000
xxx.133.17.169:7002
xxx.133.17.170:7004
Adding replica xxx.133.17.169:7003 to xxx.133.17.168:7000
Adding replica xxx.133.17.170:7005 to xxx.133.17.169:7002
Adding replica xxx.133.17.168:7001 to xxx.133.17.170:7004
M: dcc389f4c868cc007440850d9c0c236a24df7063 xxx.133.17.168:7000
slots:0-5460,5474,5798,14537 (5464 slots) master
S: d894018e978846a3c65b72eb1313a7b22f7499b5 xxx.133.17.168:7001
replicates 5be3afcc4518d2ed7abe3c9459d60700c07c92e0
M: 198e9fb972c5605030ed6d1e5cc359ae5e956666 xxx.133.17.169:7002
slots:5461-10922,15495 (5463 slots) master
xxx replicates dcc389f4c868cc007440850d9c0c236a24df7063
M: 5be3afcc4518d2ed7abe3c9459d60700c07c92e0 xxx.133.17.170:7004
slots:10923-16383 (5461 slots) master
S: fc813d3533157a59c5159525ad1612ca13513652 xxx.133.17.170:7005
replicates 198e9fb972c5605030ed6d1e5cc359ae5e956666
Can I set the above configuration? (type 'yes' to accept):
激动的输入yes后,出现
/home/rdadmin/.rvm/gems/ruby-2.3.3/gems/redis-4.0.1/lib/redis/client.rb:119:in `call': ERR Slot 5474 is already busy (Redis::CommandError)
继续进入客户端
redis-cli -h xxx.133.17.168 -p 7000
auth 818
flushall
cluster resetsoft
exit
再次在src下执行 ./redis-trib.rb,大功告成!
参考链接
官方文档:http://www.redis.cn/topics/cluster-tutorial.html
正确搭建流程:https://blog.csdn.net/u011726984/article/details/78819542
密码问题:https://www.jianshu.com/p/40fbcadde1ac
redis不为空:https://blog.csdn.net/vtopqx/article/details/50235737
rvm安装ruby:https://www.cnblogs.com/custer/p/6267506.html