1、設定集群密碼
各個節點的配置中設定密碼,注意集群模式下,下面兩行都需要
1
2
|
masterauth passwd123
requirepass passwd123
|
2、各個節點的配置中開啟集群配置的相關選項,如下:
1
2
3
4
5
|
port 30001
cluster-enabled yes
cluster-config-file nodes-30001.conf
cluster-node-timeout 5000
appendonly yes
|
3、修改create-cluster工具
新版的Redis中給我們提供了利用redis-trib.rb創建集群的工具,就是:utils/create-cluster/create-cluster
我們稍作修改,把start部分的redis-server命令的參數,直接替換為各個節點配置文件路徑
1
2
3
4
5
6
7
8
9
10
|
if [ "$1" == "start" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Starting $PORT"
# 通過redis-server並指定各個節點的redis配置文件路徑
../../src/redis-server /redis/redis-$PORT.conf
done
exit 0
fi
|
4、修改redis-trib.rb腳本
如果Redis設定了密碼,那么通過redis-trib.rb腳本創建集群時,是會類似這樣的錯誤的:[ERR] Sorry, can’t connect to node *.*.*.*:7001
這是因為redis-trib.rb腳本中連接Redis時,並未設定密碼,這確實是個很大的坑。我的解決方法時,修改該腳本中連接Redis時的代碼,修改內容如下:
找到這一行:
1
|
@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60)
|
修改為:
1
|
@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60, :password => "你的密碼")
|
5、修改完成后,依次運行:
./craete-cluster start
./craete-cluster create
即可啟動集群