参考 Linux 安装 Redis 6.0.6
1.获取资源路径
2.准备
bash
yum install wget
yum install gcc
#***gcc默认安装的gcc是4.8.5,版本过低,需要升级***
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
#查看版本号
gcc -v
2.编译
bash
cd redis-6.0.6
make
make install PREFIX=/usr/local/redis/
# 配置环境变量
#从源码中把可执行文件迁出
make install PREFIX=/usr/local/redis/
vi /etc/profile
#增加以下内容:
export REDIS_HOME=/usr/local/redis/
export PATH=$REDIS_HOME/bin:$PATH
#使配置生效
source /etc/profile
#验证
echo $PATH
3. 创建redis实例
bash
cd /usr/local/redis-6.0.6/utils
./install_server.sh
css
这步有可能会报错:`
`This systems seems to use systemd. Please take a look at the provided example service unit files in this directory, and adapt and install t hem. Sorry!
解决方案:
bash
vi ./install_server.sh
1
注释下面的代码即可
shell
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
然后重新运行 ./install_server.sh
即可。 按几下回车
makefile
如果出现错误:
# Please select the redis executable path [] Mmmmm... it seems like you don't have a redis executable..
需要在path || 后手动输入路径
perl
#查看该实例的运行状态
service redis_6379 status
#停止
service redis_6379 stop
#启动
service redis_6379 start
[root@localhost utils]# ps -ef|grep redis
root 3831 1 0 10:03 ? 00:00:01 /usr/local/redis//bin/redis-server 127.0.0.1:6379
root 12076 1434 0 10:26 pts/0 00:00:00 grep --color=auto redis
#需要开启外部访问
1.找到redis中的redis.cof文件并打开
2.找到bind 127.0.0.1并注释掉
3.修改 protected-mode 属性值为no
#启动 redis-server /usr/local/redis-6.0.6/redis.conf
#注:redis默认是只能本地访问,注释掉并叫保护模式禁用以后可以IP访问
2.集群搭建
准备
复制源目录根目录下的redis.conf文件到生成的/usr/local/redis目录下
使用vi编辑配置redis.conf
yaml
bind 0.0.0.0 设置绑定的主机ip,0.0.0.0表示监听所有地址
protected-mode no 关闭安全模式,允许外网访问
daemonize yes 后台启动
port 7001 修改端口号,各节点不重复
cluster-enabled yes 开启集群支持
复制redis实例
bash
# 创建文件夹
mkdir redis-cluster
# 可以使用一台机器,我使用三台,每台两个
cp -r redis redis-cluster/redis01
cp -r redis redis-cluster/redis02
...
启动所有节点
bash
[root@redis121 redis02] ./redis-server redis.conf
[root@redis121 redis01] ./redis-server redis.conf
...
创建集群
vbnet
# 进入到集群中某个实例下的bin目录执行
[root@redis121 redis01] /usr/local/redis-cluster/redis01/redis-cli --cluster create 192.168.25.104:7001 192.168.25.104:7004 1 92.168.25.121:7002 192.168.25.121:7005 192.168.25.122:7003 192.168.25.122:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.25.121:7005 to 192.168.25.104:7001
Adding replica 192.168.25.122:7006 to 192.168.25.121:7002
Adding replica 192.168.25.104:7004 to 192.168.25.122:7003
M: 81e6d7cb24891849940d73f9e9c10ac0545cd65d 192.168.25.104:7001
slots:[0-5460] (5461 slots) master
S: c2bbaeb68c172aa1fed55e519b86347f6bd2b9b7 192.168.25.104:7004
replicates 63e280dae17aa3d980f6c4fe2c0532a11b83616f
M: f957b6e222f6a3c996a4ba0fa2a77dec01524266 192.168.25.121:7002
slots:[5461-10922] (5462 slots) master
S: b80c9fcea4f6d2a9d1db68954848075311542598 192.168.25.121:7005
replicates 81e6d7cb24891849940d73f9e9c10ac0545cd65d
M: 63e280dae17aa3d980f6c4fe2c0532a11b83616f 192.168.25.122:7003
slots:[10923-16383] (5461 slots) master
S: 2e55f80db5784568a26b3ae2b3418e9700bfe811 192.168.25.122:7006
replicates f957b6e222f6a3c996a4ba0fa2a77dec01524266
Can I set the above configuration? (type 'yes' to accept): yes # 输入yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.25.104:7001)
M: 81e6d7cb24891849940d73f9e9c10ac0545cd65d 192.168.25.104:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 2e55f80db5784568a26b3ae2b3418e9700bfe811 192.168.25.122:7006
slots: (0 slots) slave
replicates f957b6e222f6a3c996a4ba0fa2a77dec01524266
M: f957b6e222f6a3c996a4ba0fa2a77dec01524266 192.168.25.121:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: c2bbaeb68c172aa1fed55e519b86347f6bd2b9b7 192.168.25.104:7004
slots: (0 slots) slave
replicates 63e280dae17aa3d980f6c4fe2c0532a11b83616f
M: 63e280dae17aa3d980f6c4fe2c0532a11b83616f 192.168.25.122:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: b80c9fcea4f6d2a9d1db68954848075311542598 192.168.25.121:7005
slots: (0 slots) slave
replicates 81e6d7cb24891849940d73f9e9c10ac0545cd65d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 如果一直在 Waiting for the cluster to join.....................
# 停掉所有节点,删除文件夹底下的nodes.conf,dump.rdb
# 修改 pidfile /var/run/redis_7001.pid 为端口
# 重启,重新执行
连接
bash
# 使用redis-cli连接 -c 表示集群模式,不带的话无法执行操作 在本机的节点不用输入ip
[root@redis104 redis01] ./redis-cli -c -p 7002
# 如果节点在其他服务器 需要加 -h
[root@redis104 redis01] ./redis-cli -c -h 192.168.25.122 -p 7006