一 单机redis安装
首先下载redis到本机
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
解压
tar -zxvf redis-4.0.9.tar.gz
切换到redis-4.0.9目录
cd redis-4.0.9
安装
make
make install PREFIX=/usr/local/redis # PREFIX指定安装目录
配置后台启动,将源文件中的redis.conf文件拷贝到redis安装目录中
sudo cp ~/redis-4.0.9/redis.conf /usr/local/redis/bin/
修改redis.conf配置文件
sudo vi /usr/local/redis/bin/redis.conf
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# 将daemonize no 修改为 daemonize yes
# daemonize no
daemonize yes
启动redis
cd /usr/local/redis/bin
sudo ./redis-server redis.conf
二 用redis实例模拟redis集群
安装ruby, centos7默认使用yum安装的ruby为2.0.6, 但是搭建redis4.0集群需要ruby版本大于2.2.2. 所以需要换源安装.
#会在/etc/yum.repos.d/目录下多出一个CentOS-SCLo-scl-rh.repo源
yum install centos-release-scl-rh
#直接yum安装即可
yum install rh-ruby23 -y
#必要一步
scl enable rh-ruby23 bash
#查看安装版本
ruby -v
接下来创建多个redis实例
首先创建文件夹并将先安装的redis拷贝出一个新的redis实例
sudo mkdir redis-cluster
sudo cp -r /usr/local/redis/bin/ /usr/local/redis-cluster/redis01
修改配置文件, 修改端口, 开启集群功能
# 修改端口,将端口修改为7001
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
# port 6379
port 7001
# 开启集群功能
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
# cluster-enabled yes
cluster-enabled yes
将redis01复制出5个redis, 并修改配置文件, 修改端口
sudo cp -r redis01 redis02
sudo cp -r redis01 redis03
sudo cp -r redis01 redis04
sudo cp -r redis01 redis05
sudo cp -r redis01 redis06
# 修改端口
sudo vi ./redis02/redis.conf
sudo vi ./redis03/redis.conf
sudo vi ./redis04/redis.conf
sudo vi ./redis05/redis.conf
sudo vi ./redis06/redis.conf
创建6个redis实例的统一启动脚本
sudo vim start-all.sh
# 添加如下内容
cd ./redis01
sudo ./redis-server redis.conf
cd ..
cd ./redis02
sudo ./redis-server redis.conf
cd ..
cd ./redis03
sudo ./redis-server redis.conf
cd ..
cd ./redis04
sudo ./redis-server redis.conf
cd ..
cd ./redis05
sudo ./redis-server redis.conf
cd ..
cd ./redis06
sudo ./redis-server redis.conf
cd ..
# 修改权限
sudo chmod 755 start-all.sh
将redis-trib.rb拷贝到/usr/local/redis-cluster目录下
sudo cp /home/mejhwu2/redis-4.0.9/src/redis-trib.rb /usr/local/redis-cluster/
创建集群
sudo /usr/loca/redis-cluster/redis-trib.rb create --replicas 1 192.168.1.202:7001 192.168.1.202:7002 192.168.1.202:7003 192.168.1.202:7004 192.168.1.202:7005 192.168.1.202:7006
可能出现的错误
[ERR] Sorry, can't connect to node 192.168.1.202:7001
解决方法:
需要在配置文件中注释掉bind 127.0.0.1
, 如果不注释掉的话,就只能127.0.0.1能够访问. 关闭protected-mode
# 注释 bind 127.0.0.1
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1
# 修改protected-mode
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
# protected-mode yes
protected-mode no
连接redis集群并测试
redis01/redis-cli -h 192.168.1.202 -p 7002 -c
192.168.1.202:7002> set a 100
-> Redirected to slot [15495] located at 192.168.1.202:7003
OK
192.168.1.202:7003> get a
"100"
192.168.1.202:7003> set hello
(error) ERR wrong number of arguments for 'set' command
192.168.1.202:7003> set hello hello
-> Redirected to slot [866] located at 192.168.1.202:7001
OK
192.168.1.202:7001> get hello
"hello"
192.168.1.202:7001>
网友评论