环境:
Redis:3.2.10 ; CentOS 6/7
摘要说明:
Redis:REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。
本篇文章主要讲述redis在liunx下的安装及相关配置;
步骤:
1.Redis的安装及基础使用
a.安装Redis
阿里云下直接安装即可:
yum install redis
其他情况下可能需要安装fedora的epel仓库:
yum install epel-release
若想保证安装最新版本,先更新yum:
yum update
安装后查看redis版本及安装情况:
redis-server -v
或
redis-server --version
b.Redis启动及redis-cli的使用
通用启动:
service redis start
service redis stop
service redis restart
CentOS 7可使用:
systemctl start redis.service
systemctl stop redis.service
systemctl restart redis.service
可根据redis进程查看是否启动成功:
ps -aux|grep redis
使用redis-cli客户端连接服务器:
# redis-cli
127.0.0.1:6379> set age 10
OK
127.0.0.1:6379> get age
"10"
上述是使用redis-cli默认连接本地服务器;远程连接方式为:
redis-cli -h host -p port -a password
如连接到主机为 127.0.0.1,端口为 6379 ,密码为 mypass 的 redis 服务:
redis-cli -h 127.0.0.1 -p 6379 -a "mypass"
c.Redis服务密码开启及设置
上述我们直接使用redis-cli可以直接连接Redis服务器说明服务密码尚未开启;我们也可以通过命令查看
# redis-cli
127.0.0.1:6379> CONFIG get requirepass
1) "requirepass"
2) ""
127.0.0.1:6379>
设置密码:
CONFIG set requirepass password
如:
127.0.0.1:6379> CONFIG set requirepass '111111'
OK
127.0.0.1:6379> CONFIG get requirepass
(error) NOAUTH Authentication required.
我们可以看到我们再想查看服务密码则需要权限验证:
权限验证如下:
AUTH password
如:
127.0.0.1:6379> AUTH 111111
OK
d.设置Redis服务开机启动
设置开机启动:
chkconfig redis on
CentOS 7可使用:
systemctl enable redis.service
e.防火墙配置
阿里云可以通过配置开放6379端口即可;
其他可以通过配置防火墙:
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 6380 -j ACCEPT
/etc/rc.d/init.d/iptables save