yum -y install redis
安装版本降低
获取源码包
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
...
[root@ryan ~]# ls
anaconda-ks.cfg redis-5.0.0.tar.gz
解压
[root@ryan ~]# tar -zxvf redis-5.0.0.tar.gz
[root@ryan ~]# ls -tl
total 1908
-rw-------. 1 root root 1257 Feb 26 00:04 anaconda-ks.cfg
-rw-r--r-- 1 root root 1947721 Oct 18 2018 redis-5.0.0.tar.gz
drwxrwxr-x 6 root root 309 Oct 17 2018 redis-5.0.0
编译安装
#下载编译所需环境
yum -y install gcc gcc-c++
#进入redis-5.0.0目录编译
[root@ryan redis-5.0.0]# make
......等待编译结束
#安装redis至/usr/local/redis目录下
[root@ryan redis-5.0.0]# make PREFIX=/usr/local/redis install
#拷贝Redis的redis.conf配置文件至redis的安装目录下,
[root@ryan redis-5.0.0]# cp redis.conf /usr/local/redis
#修改Redis配置文件,使Redis以后台进程的形式启动
[root@ryan ~]# vi /usr/local/redis/redis.conf
如将daemonize no这行修改为daemonize yes。如果为no就会以进程的方式启动,占用一个终端
[root@ryan ~]# cd /usr/local/redis/
[root@ryan redis]# /usr/local/redis/bin/redis-server redis.conf
[root@ryan redis]# redis-cli -p 6379 -h 127.0.0.1
127.0.0.1:6379>
查看端口
[root@ryan redis]# ss -tnl | grep 6379
LISTEN 0 128 127.0.0.1:6379 *:*
连接redis数据库
[root@ryan ~]# redis-cli -p 6379 -h 127.0.0.1
127.0.0.1:6379>
[root@ryan redis]# /usr/local/redis/bin/redis-cli shutdown
redis主从搭建可以在一台主机上搭建,也可在两台主机上
此处用一台主机
cp redis.conf slave.conf
vim slave-6380.conf
......
bind 192.168.30.140 #此前由127.0.0.1修改的
slaveof 192.168.30.140 6379 #这里紧跟着添加此条,slaveof意思是从属于192.168.30.140这个主节点
......
port 6380 #修改端口号为6380
[root@ryan redis]# ps -ef | grep redis
root 17247 1 0 19:48 ? 00:00:05 /usr/local/redis/bin/redis-server 192.168.30.140:6379
root 17301 12174 0 20:24 pts/3 00:00:00 grep --color=auto redis
[root@ryan redis]# kill -9 17247
启用主从redis
[root@ryan redis]# /usr/local/redis/bin/redis-server redis.conf
17305:C 16 May 2019 20:25:02.499 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17305:C 16 May 2019 20:25:02.499 # Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=17305, just started
17305:C 16 May 2019 20:25:02.500 # Configuration loaded
[root@ryan redis]# /usr/local/redis/bin/redis-server slave-6380.conf
17310:C 16 May 2019 20:25:18.290 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17310:C 16 May 2019 20:25:18.290 # Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=17310, just started
17310:C 16 May 2019 20:25:18.290 # Configuration loaded
查看端口
[root@ryan redis]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 192.168.30.140:6379 *:*
LISTEN 0 128 192.168.30.140:6380 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25
主节点:
[root@ryan redis]# redis-cli -p 6379 -h 192.168.30.140
192.168.30.140:6379>
192.168.30.140:6379> set username haha
OK
从节点:
[root@ryan ~]# redis-cli -p 6380 -h 192.168.30.140
192.168.30.140:6380> keys *
1) "username"
192.168.30.140:6380> get username
"haha"
我用一台主机搭建redis集群,三主三从
主机地址:192.168.30.140
redis版本:5.0
这里我在root目录下创建了一个conf目来放6个redis配置文件
[root@ryan ~] mkdir conf
[root@ryan ~] cd conf
[root@ryan ~] vim 7001.conf
port 7001 #绑定端口
bind 192.168.30.140 #绑定对外连接提供的ip
daemonize yes #开启守护进程
pidfile 7001.pid #进程文件名
cluster-enabled yes #是否是集群
cluster-config-file 7001_node.conf #集群配置文件
cluster-node-timeout 15000 #集群连接超时时间
appendonly yes #数据持久化类型
其他5个只需修改三处
port
pidfile
cluster-config-file
[root@ryan conf]# redis-server 7001.conf
[root@ryan conf]# redis-server 7002.conf
[root@ryan conf]# redis-server 7003.conf
[root@ryan conf]# redis-server 7004.conf
[root@ryan conf]# redis-server 7005.conf
[root@ryan conf]# redis-server 7006.conf
[root@ryan conf]# ps -ef | grep redis
root 17724 1 0 14:00 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.30.140:7001 [cluster]
root 17726 1 0 14:00 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.30.140:7002 [cluster]
root 17731 1 0 14:00 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.30.140:7003 [cluster]
root 17736 1 0 14:00 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.30.140:7004 [cluster]
root 17741 1 0 14:00 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.30.140:7005 [cluster]
root 17746 1 0 14:00 ? 00:00:00 /usr/local/redis/bin/redis-server 192.168.30.140:7006 [cluster]
root 17774 17567 0 14:01 pts/0 00:00:00 grep --color=auto redis
[root@ryan conf]# /usr/local/redis/bin/redis-cli --cluster create 192.168.30.140:7001 192.168.30.14040:7004 192.168.30.140:7005 192.168.30.140: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.30.140:7004 to 192.168.30.140:7001
Adding replica 192.168.30.140:7005 to 192.168.30.140:7002
Adding replica 192.168.30.140:7006 to 192.168.30.140:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 5fb90e247526fb15262316ff4ffc38faa88835b5 192.168.30.140:7001
slots:[0-5460] (5461 slots) master
M: 053d3feeea8ad20928990a3adc80c41484ed75cb 192.168.30.140:7002
slots:[5461-10922] (5462 slots) master
M: 0c171b363d0ad192d1f665354b57f570c5cdadd4 192.168.30.140:7003
slots:[10923-16383] (5461 slots) master
S: 98d95a2c3c35ca9489126e062715f97ad1c88b42 192.168.30.140:7004
replicates 0c171b363d0ad192d1f665354b57f570c5cdadd4
S: c2c8bd50b400532efaa683f532e5e0cf303b3e48 192.168.30.140:7005
replicates 5fb90e247526fb15262316ff4ffc38faa88835b5
S: 13da299348a0c0ceea78c8c5968cc9b2e690b83e 192.168.30.140:7006
replicates 053d3feeea8ad20928990a3adc80c41484ed75cb
Can I set the above configuration? (type 'yes' to accept): 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.30.140:7001)
M: 5fb90e247526fb15262316ff4ffc38faa88835b5 192.168.30.140:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: c2c8bd50b400532efaa683f532e5e0cf303b3e48 192.168.30.140:7005
slots: (0 slots) slave
replicates 5fb90e247526fb15262316ff4ffc38faa88835b5
S: 98d95a2c3c35ca9489126e062715f97ad1c88b42 192.168.30.140:7004
slots: (0 slots) slave
replicates 0c171b363d0ad192d1f665354b57f570c5cdadd4
M: 0c171b363d0ad192d1f665354b57f570c5cdadd4 192.168.30.140:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 053d3feeea8ad20928990a3adc80c41484ed75cb 192.168.30.140:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 13da299348a0c0ceea78c8c5968cc9b2e690b83e 192.168.30.140:7006
slots: (0 slots) slave
replicates 053d3feeea8ad20928990a3adc80c41484ed75cb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@ryan conf]# redis-cli -p 7001 -c -h 192.168.30.140
192.168.30.140:7001> set username bic
-> Redirected to slot [14315] located at 192.168.30.140:7003
OK
192.168.30.140:7003> set username1 bic
-> Redirected to slot [4746] located at 192.168.30.140:7001
OK
192.168.30.140:7001> set username2 bic
-> Redirected to slot [8937] located at 192.168.30.140:7002
OK
192.168.30.140:7002> set username3 bic
-> Redirected to slot [13000] located at 192.168.30.140:7003
OK
192.168.30.140:7003> set username4 bic
-> Redirected to slot [559] located at 192.168.30.140:7001
OK
测试成功,所设置的数据会在主节点上存储,而从节点不可写。
配置文件与运行所产生的文件
[root@ryan conf]# ls
7001.conf 7002_node.conf 7003.pid 7005.conf 7006_node.conf
7001_node.conf 7002.pid 7004.conf 7005_node.conf 7006.pid
7001.pid 7003.conf 7004_node.conf 7005.pid appendonly.aof
7002.conf 7003_node.conf 7004.pid 7006.conf dump.rdb