我的这篇文章介绍了如何安装redis https://blog.csdn.net/weixin_39202006/article/details/102915831
安装完redis后,接下来可以搭建redis集群。
首先在redis安装目录下新建目录
redis_cluster/7000
然后在7000目录下创建文件
touch 7000.conf
输入以下内容:
bind 0.0.0.0
port 7000
protected-mode no #允许其他机器连接
daemonize yes #后台运行
cluster-enabled yes #开启集群
cluster-config-file nodes-7000.conf#集群配置文件,首次启动自动生成
cluster-node-timeout 15000 #集群连接过时时间
logfile redis-7000.log #日志文件
appendonly yes #记录每次redis写入
appendfilename "appendonly-7000.aof" #aof文件
loglevel notice
logfile ""
requirepass xxxx #登陆该redis需要的密码
masterauth xxxx #集群设置密码,redis集群设置的密码应该与登陆密码一致
同理可以创建7001和7002
返回到redis安装目录,新建一个bin目录
mkdir bin
cd bin
在其中创建redis的启动和关闭shell
touch start.sh
输入:
#!/bin/bash
../src/redis-server ../redis.conf
../src/redis-server ../redis_cluster/7000/7000.conf
../src/redis-server ../redis_cluster/7001/7001.conf
../src/redis-server ../redis_cluster/7002/7002.conf
touch stop.sh
输入:
#!/bin/bash
pid=`ps -ef | grep redis | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
echo "kill -9 pid:" $pid
kill -9 $pid
fi
grep -v grep是过滤掉前面查询到的grep的pid
awk '{print $2}'表示将文件逐行读入,选择第二列,也就是pid
touch redis-cluster.sh
输入:
#!/bin/bash
/usr/local/redis/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
然后修改start.sh stop.sh redis-cluster.sh权限为744
redis-trib.rb为redis启动集群方式,在src目录下,如果找不到,将redis.conf的cluster-enabled改成yes,然后启动,就可以找到了。
cluster-enabled yes
运行redis-trib.rb需要安装ruby
在/usr/local目录下新建rvm目录
cd /usr/local
touch rvm
cd rvm
#下载ruby
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz
tar -zxvf ruby-2.5.1.tar.gz
cd ruby-2.5.1
./configure --prefix=/usr/local/rvm
make && make install
完后后会在/usr/local/rvm目录下出现bin等其他目录,将该目录添加到/etc/profile下
vim /etc/profile
export PATH=$PATH:/usr/local/rvm/bin
退出编辑后 source /etc/profile
使用ruby -v查看版本
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
修改国内镜像
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
gem sources -l
如果出现 /usr/bin/gem: No such file or directory
解决方案:ln -s /usr/local/rvm/bin/gem /usr/bin/gem
运行命令:
gem install redis 完成
以上操作完成后,就可以构建集群了。
cd /usr/local/redis/bin
./redis-cluster.sh
如果保密码认证错误,需要找到client.rb文件
cd /
find / -name client.rb
我路径是/usr/local/rvm/lib/ruby/gems/2.5.0/gems/redis-4.1.3/lib/redis/client.rb
修改client.rb,找到其中password对应栏目,修改为conf文件中的密码
重新启动集群,成功!