CentOS 6.8 下 安装Redis 3.2.8

前端之家收集整理的这篇文章主要介绍了CentOS 6.8 下 安装Redis 3.2.8前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、单实例安装

1、安装依赖

yum install gcc-c++
yum install -y tcl
yum install wget
yum install vim

2、获取安装文件并解压安装

wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar xzf redis-3.2.8.tar.gz
cd redis-3.2.8
make
make install

3、修改配置文件

vi redis.conf
#1.设置为后台运行
daemonize no
改为
daemonize yes
#2.修改默认端口
port 6379
改为
port 7963
#3.修改pidfile
pidfile /var/run/redis_6379.pid
改为

pidfile /var/run/redis_7963.pid
#4.设置日志级别
loglevel notice
改为
loglevel notice
#5.修改日志文件路径
logfile ""
改为
logfile "/var/log/redis_7963.log"

复制配置文件到常用目录
mkdir -p /etc/redis/
cp redis.conf /etc/redis/redis_7963.conf

4、启动redis并连接测试

cd /etc/redis/
/usr/local/bin/redis-server redis_7963.conf

/usr/local/bin/redis-cli -p 7963
127.0.0.1:7963> keys *
(empty list or set)
127.0.0.1:7963> set test "hello redis"
OK
127.0.0.1:7963> keys *
1) "test"
127.0.0.1:7963> get test
"hello redis"
127.0.0.1:7963> del test
(integer) 1
127.0.0.1:7963> keys *
(empty list or set)
127.0.0.1:7963> exit

5、关闭Redis

#方案一:
/usr/local/bin/redis-cli -p 7963 shutdown
#方案二:
netstat -lntp | grep 7963 #查看端口是否被占用
ps -ef|grep redis
   root 4726 1 0 10:33 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:7963
   root 4731 1253 0 10:40 pts/0 00:00:00 grep redis

kill -9 4726

#备注说明:由于Redis可以接收并处理SIGTERM信号,所以直接使用kill -9 PID,是可以安全结束进程的;
另外:有两个信号可以停止进程,分别为:SIGTERM和SIGKILL
SIGTERM用于提醒进程关闭,进程可以在关闭之前,结束打开的文件记录和完成正在进行的任务,如果有些任务不能中断,则可以忽略SIGTERM信号;
SIGKILL则是强制关闭进程,即表示不管进程在做什么,应该马上关闭

6、设置开机启动

echo "/usr/local/bin/redis-server /etc/redis/redis_7963.conf" >> /etc/rc.local
#注意:开机启动需要配置在rc.local中,而/etc/profile文件需要有用户登录才会被执行

二、单机多实例安装Cluster

1、创建集群需要的节点文件

mkdir -p /data/cluster/7000
mkdir -p /data/cluster/7001
mkdir -p /data/cluster/7003
mkdir -p /data/cluster/7004
mkdir -p /data/cluster/7002
mkdir -p /data/cluster/7005

2、创建配置文件

cd /data/cluster/7000

#复制配置文件
cp /etc/redis/redis_7963.conf redis.conf

#修改配置文件
vim redis.conf

port 7000 #设置端口号
daemonize yes #设置为后台运行
cluster-enabled yes #开启集群模式
cluster-config-file nodes.conf #节点配置文件路径
cluster-node-timeout 5000 #节点超时时间,单位:毫秒
appendonly yes #开启AOF持久化,防止数据在硬盘丢失

#复制配置文件,并修改端口号
cp redis.conf ../7001
cp redis.conf ../7002
cp redis.conf ../7003
cp redis.conf ../7004
cp redis.conf ../7005
vim ../7001/redis.conf
vim ../7002/redis.conf
vim ../7003/redis.conf
vim ../7004/redis.conf
vim ../7005/redis.conf

3、启动集群节点

redis-server redis.conf
cd /data/cluster/7001
redis-server redis.conf
cd /data/cluster/7002
redis-server redis.conf
cd /data/cluster/7003
redis-server redis.conf
cd /data/cluster/7004
redis-server redis.conf
cd /data/cluster/7005
redis-server redis.conf

#查看redis启动进程
ps -ef|grep redis
root      1170     1  0 11:05 ?        00:00:32 /usr/local/bin/redis-server 127.0.0.1:7963
root      1311     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7000 [cluster]
root      1315     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7001 [cluster]
root      1319     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7002 [cluster]
root      1323     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7003 [cluster]
root      1327     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7004 [cluster]
root      1331     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7005 [cluster]
root      1338  1202  0 16:17 pts/0    00:00:00 grep redis

4、安装依赖并且创建集群

#安装ruby、rubygems
yum install ruby rubygems -y

#安装gem-redis
cd /opt/
wget https://rubygems.org/downloads/redis-3.3.3.gem
gem install -l redis-3.3.3.gem

#复制管理程序
cp redis-3.2.8/src/redis-trib.rb /usr/local/bin/redis-trib

#创建集群
redis-trib create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: af244fe9be7256039357fbf463f8f689544323d1 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 88e426f2501bea4bc17a2a458beb9c57a7321157 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 79dd52da7a66cec1508b2ab75a439a79fa8678bc 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: abfb133576837b9c7dfcfadbe8e2fca47d14a41d 127.0.0.1:7003
   replicates af244fe9be7256039357fbf463f8f689544323d1
S: e1d18594b8b322c264757b7b396a70bcba913968 127.0.0.1:7004
   replicates 88e426f2501bea4bc17a2a458beb9c57a7321157
S: f2bb24dcc4eed871bc4ab7e56eebce9e72e37f44 127.0.0.1:7005
   replicates 79dd52da7a66cec1508b2ab75a439a79fa8678bc
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 127.0.0.1:7000)
M: af244fe9be7256039357fbf463f8f689544323d1 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 88e426f2501bea4bc17a2a458beb9c57a7321157 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: f2bb24dcc4eed871bc4ab7e56eebce9e72e37f44 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 79dd52da7a66cec1508b2ab75a439a79fa8678bc
S: abfb133576837b9c7dfcfadbe8e2fca47d14a41d 127.0.0.1:7003
   slots: (0 slots) slave
   replicates af244fe9be7256039357fbf463f8f689544323d1
M: 79dd52da7a66cec1508b2ab75a439a79fa8678bc 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: e1d18594b8b322c264757b7b396a70bcba913968 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 88e426f2501bea4bc17a2a458beb9c57a7321157
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


#检查集群状态
redis-cli -p 7000 cluster nodes 

88e426f2501bea4bc17a2a458beb9c57a7321157 127.0.0.1:7001 master - 0 1490776018194 2 connected 5461-10922
f2bb24dcc4eed871bc4ab7e56eebce9e72e37f44 127.0.0.1:7005 slave 79dd52da7a66cec1508b2ab75a439a79fa8678bc 0 1490776020201 6 connected
abfb133576837b9c7dfcfadbe8e2fca47d14a41d 127.0.0.1:7003 slave af244fe9be7256039357fbf463f8f689544323d1 0 1490776019197 4 connected
79dd52da7a66cec1508b2ab75a439a79fa8678bc 127.0.0.1:7002 master - 0 1490776018696 3 connected 10923-16383
e1d18594b8b322c264757b7b396a70bcba913968 127.0.0.1:7004 slave 88e426f2501bea4bc17a2a458beb9c57a7321157 0 1490776019699 5 connected
af244fe9be7256039357fbf463f8f689544323d1 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460

5、测试集群节点写入

redis-cli -p 7000
127.0.0.1:7000> keys *
(empty list or set)
127.0.0.1:7000> set test "hello redis cluster"
(error) MOVED 6918 127.0.0.1:7001
127.0.0.1:7000> exit

#7000,写入失败,换为7001进行写入测试
redis-cli -p 7001
127.0.0.1:7001> set test "hello redis cluster"
OK
127.0.0.1:7001> keys *
1) "test"
127.0.0.1:7001> get test
"hello redis cluster"
127.0.0.1:7001> exit

6、测试集群节点故障

ps -ef|grep redis
root      1170     1  0 11:05 ?        00:00:33 /usr/local/bin/redis-server 127.0.0.1:7963
root      1311     1  0 16:16 ?        00:00:01 redis-server 127.0.0.1:7000 [cluster]
root      1315     1  0 16:16 ?        00:00:01 redis-server 127.0.0.1:7001 [cluster]
root      1319     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7002 [cluster]
root      1323     1  0 16:16 ?        00:00:01 redis-server 127.0.0.1:7003 [cluster]
root      1327     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7004 [cluster]
root      1331     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7005 [cluster]
root      1374  1202  0 16:28 pts/0    00:00:00 grep redis

kill 1311

ps -ef|grep redis
root      1170     1  0 11:05 ?        00:00:33 /usr/local/bin/redis-server 127.0.0.1:7963
root      1315     1  0 16:16 ?        00:00:01 redis-server 127.0.0.1:7001 [cluster]
root      1319     1  0 16:16 ?        00:00:01 redis-server 127.0.0.1:7002 [cluster]
root      1323     1  0 16:16 ?        00:00:01 redis-server 127.0.0.1:7003 [cluster]
root      1327     1  0 16:16 ?        00:00:01 redis-server 127.0.0.1:7004 [cluster]
root      1331     1  0 16:16 ?        00:00:00 redis-server 127.0.0.1:7005 [cluster]
root      1376  1202  0 16:29 pts/0    00:00:00 grep redis

#测试是否能连接到7000
redis-cli -p 7000 cluster nodes
Could not connect to Redis at 127.0.0.1:7000: Connection refused

#7000无法连接,改为连接到7001
redis-cli -p 7001 cluster nodes
f2bb24dcc4eed871bc4ab7e56eebce9e72e37f44 127.0.0.1:7005 slave 79dd52da7a66cec1508b2ab75a439a79fa8678bc 0 1490776164137 3 connected
af244fe9be7256039357fbf463f8f689544323d1 127.0.0.1:7000 master,fail - 1490776153654 1490776152049 1 disconnected
79dd52da7a66cec1508b2ab75a439a79fa8678bc 127.0.0.1:7002 master - 0 1490776164137 3 connected 10923-16383
e1d18594b8b322c264757b7b396a70bcba913968 127.0.0.1:7004 slave 88e426f2501bea4bc17a2a458beb9c57a7321157 0 1490776162122 2 connected
abfb133576837b9c7dfcfadbe8e2fca47d14a41d 127.0.0.1:7003 master - 0 1490776163633 7 connected 0-5460
88e426f2501bea4bc17a2a458beb9c57a7321157 127.0.0.1:7001 myself,master - 0 0 2 connected 5461-10922

#集群节点中,7000连接已丢失,再次尝试读取及写入
127.0.0.1:7001> keys *
1) "test"
127.0.0.1:7001> get test
"hello redis cluster"
127.0.0.1:7001> set test1 "kill a node,try write"
(error) MOVED 4768 127.0.0.1:7003
127.0.0.1:7001> exit

#从7001中写入 key=test1失败,改为从7003写入测试
redis-cli -p 7003
127.0.0.1:7003> set test1 "kill a node,try write"
OK
127.0.0.1:7003> keys *
1) "test1"
127.0.0.1:7003> get test
(error) MOVED 6918 127.0.0.1:7001
127.0.0.1:7003> get test1
"kill a node,try write"
127.0.0.1:7003> exit

#恢复节点
cd /data/cluster/7000
redis-server redis.conf

#查看集群节点状态
redis-cli -p 7000 cluster nodes
af244fe9be7256039357fbf463f8f689544323d1 127.0.0.1:7000 myself,slave abfb133576837b9c7dfcfadbe8e2fca47d14a41d 0 0 1 connected
abfb133576837b9c7dfcfadbe8e2fca47d14a41d 127.0.0.1:7003 master - 0 1490778603081 7 connected 0-5460
e1d18594b8b322c264757b7b396a70bcba913968 127.0.0.1:7004 slave 88e426f2501bea4bc17a2a458beb9c57a7321157 0 1490778602075 5 connected
f2bb24dcc4eed871bc4ab7e56eebce9e72e37f44 127.0.0.1:7005 slave 79dd52da7a66cec1508b2ab75a439a79fa8678bc 0 1490778603582 6 connected
79dd52da7a66cec1508b2ab75a439a79fa8678bc 127.0.0.1:7002 master - 0 1490778602578 3 connected 10923-16383
88e426f2501bea4bc17a2a458beb9c57a7321157 127.0.0.1:7001 master - 0 1490778601574 2 connected 5461-10922

参考资料:http://www.cnblogs.com/goMysqL/p/4395504.html

原文链接:https://www.f2er.com/centos/378179.html

猜你在找的CentOS相关文章