1. Redis简介
Redis
是一个基于key-value
键值对的持久化
数据库存储系统。
为了保证效率,数据
都是缓存在内存
中提供服务。redis持久化
缓存服务还会周期性
的把更新的数据写入
到磁盘
以及把修改的操作记录追加到文件里记录下来,redis
还支持master-slave
(主从)同步,这点很类似关系型数据库MySQL主从复制
功能。
Redis
是一个开源
的使用C语言
编写(3万多行代码),支持网络
,可基于内存
亦可持久化
的日志型
,Key-Value
数据库,并提供多种语言的API
。
Redis软件的出现,再一定程度上弥补了memcached这类key-value内存缓存服务的不足,在部分场合可以对关系数据库起到很好的补充作用。redis提供了Python,Ruby,Erlang,PHP客户端,使用起来很方便。
redis官方文档如下:
http://www.redis.io/documentation
http://www.redis.cn/
http://www.redis.io/topics/introduction
redis特点:
- key-value键值类型存储
- 单进程单线程高性能服务器
- 数据类型丰富(五种数据类型)
- 支持持久化
- 支持多种内存分配及回收策略
- 支持弱事务
- 消息队列、消息订阅
- 支持高可用
- 支持分布式分片集群
redis缺陷与陷阱:
- 系统运行有毛刺
- 不同命令延迟差别极大
- 内存管理开销大(设置低于物理内存3/5)
- buffer io造成系统OOM(内存溢出)
企业缓存数据库解决方案对比:
- Memcached:
- 优点:高性能读写、支持客户端式分布式集群、一致性hash多核结构、多线程读写性能高。
- 缺点:无持久化、单一数据类型、节点故障可能出现缓存穿透、分布式需要客户端实现、跨机房数据同步困难、架构扩容复杂度高
- Redis:
- 优点:高性能读写、多数据类型支持、数据持久化、高可用架构、支持自定义虚拟内存、支持分布式分片集群、单线程读写性能极高
- 缺点:多线程读写较Memcached慢
- Tair:
- 优点:高性能读写、支持三种存储引擎(ddb、rdb、ldb)、支持高可用、支持分布式分片集群、支撑了几乎所有淘宝业务的缓存。
- 缺点:单机情况下,读写性能较其他两种产品较慢
redis应用场景:
- 数据高速缓存
- web会话缓存
- 排行榜应用
- 消息队列
- 发布订阅
2. Redis快速安装
Redis安装可以通过编译安装或者yum安装,本文中使用 编译安装
redis下载地址:http://download.redis.io/releases/
1.下载redis
mkdir -p /server/tools
cd /server/tools/
wget http://download.redis.io/releases/redis-5.0.9.tar.gz
2.解压并编译redis
编译安装完成后会在当前目录生成src这个目录(/data/redis/src),该目录存放的是redis的各种命令。
tar xf redis-5.0.9.tar.gz
cd redis-5.0.9/
make
Note:为何不用make install呢?因为这里只需要redis的命令,而且不需要自动安装redis,所以只需要执行make即可。
4.移动redis到应用目录
cp -r redis-5.0.9 /application/redis-5.0.9
ln -s /application/redis-5.0.9/ /application/redis
4.配置redis环境变量,使其能直接在命令使用redis命令
echo 'export PATH="/application/redis/src/:$PATH"' >>/etc/profile
source /etc/profile
5.启动redis服务
启动时需要加&符号,后台运行redis-server,默认服务端口号:6379
redis-server &
6.进入redis命令行模式,执行简单的操作
//进入redis命令行的模式
[root@linux-node1 ~]# redis-cli
//set 设置单个键值对
127.0.0.1:6379> set name zhangsan
OK
//get 获取键值对的值
127.0.0.1:6379> get name
"zhangsan"
//mset 设置多个键值对
127.0.0.1:6379> mset id 101 name zhangsan age 20 gender male
OK
//mget 获取多个键值对的值
127.0.0.1:6379> mget id name age gender
1) "101"
2) "zhangsan"
3) "20"
4) "male"
//keys 查看所有键值对
127.0.0.1:6379> keys *
1) "age"
2) "id"
3) "name"
4) "gender"
//del 删除某个键值对
127.0.0.1:6379> del name
(integer) 1
//exists 判断某个键值对是否存在,存在返回1,不存在返回0
127.0.0.1:6379> exists age
(integer) 1
//type 判断某个键值的类型
127.0.0.1:6379> type id
string
//expire 设置某个键值的存活时间(秒/毫秒为单位)
127.0.0.1:6379> expire id 120
(integer) 1
//TTL 查看某个键值的剩余存活时间(秒/毫秒为单位)
127.0.0.1:6379> ttl id
(integer) 38
//persist 取消某个键值的存活时间
127.0.0.1:6379> persist id
(integer) 1
7.添加redis配置文件(基础版)
//1.创建以redis端口号命名的目录用于存放配置文件
mkdir /application/redis-6379
//2.创建配置文件,文件名可随意,但最好规范
# vim /application/redis/redis-6379/redis.conf
daemonize yes
port 6379
logfile /application/redis-6379/redis.log
dir /application/redis-6379
dbfilename dump.rdb
bind 0.0.0.0
参数解释:
daemonize yes # 是否在后台运行
port 6379 # 服务端口
logfile /application/redis-6379/redis.log # 日志文件保存的路径
dir /application/redis-6379 # 数据文件存放路径(持久化)
dbfilename dump.rdb # 定义数据文件名(持久化)
bind 0.0.0.0 # 允许访问的地址
8.指定配置文件来启动redis
//1.先关闭原来启动的redis服务,
# redis-cli
127.0.0.1:6379> shutdown
//2.启动redis,并指定配置文件
# redis-server /application/redis-6379/redis.conf
# netstat -lntup |grep "6379"
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 80203/redis-server
tcp6 0 0 :::6379 :::* LISTEN 80203/redis-server
提示:也可以直接使用 redis-cli shutdown 命令关闭redis服务
redis启动脚本
- systemctl 管理
```bash
vim /lib/systemd/system/redis-6379.service
[Unit] Description=redis-6379 After=network.target
[Service] ExecStart=/application/redis/src/redis-server /application/redis-6379/redis.conf —daemonize no ExecStop=/application/redis/src/redis-cli -h 127.0.0.1 -p 6379 shutdown
[Install] WantedBy=multi-user.target
记得重新reload
```bash
systemctl daemon-reload
- shell脚本
# vim redis_start.sh
#!/bin/bash
Redis_PORT=$1
Redis_Config=redis.conf
Redis_Path=/data/$1/$Redis_Config
#[ -f /etc/init.d/functions ] && . /etc/init.d/functions
fun_usage(){
echo "Usage $0: [port] {start|stop|restart}"
exit 1
}
fun_start(){
Redis_Service=`ss -lntup |grep "${Redis_PORT}"|wc -l`
if [ $Redis_Service -eq 2 ];then
echo "Redis ${Redis_PORT} is running "
exit 1
else
redis-server $Redis_Path
retval=$?
[ $retval -eq 0 ] && echo "Redis ${Redis_PORT} start successful" || echo "Redis ${Redis_PORT} start failure" || exit 1
fi
}
fun_stop(){
Redis_Service=`ss -lntup |grep "${Redis_PORT}"|wc -l`
if [ $Redis_Service -ne 2 ];then
echo "Redis ${Redis_PORT} don't start"
exit 1
else
#pkill redis
redis-cli -p 6379 shutdown
retval=$?
[ $retval -eq 0 ] && echo "Redis ${Redis_PORT} stop successful" || echo "Redis ${Redis_PORT} stop failure" || exit 1
fi
}
main(){
[ $# -ne 2 ] && fun_usage && exit 1
PORT=$1
STAT=$2
case "$STAT" in
start)
fun_start $PORT $STAT
;;
stop)
fun_stop $PORT $STAT
;;
restart)
fun_stop $PORT $STAT
sleep 2
fun_start $PORT $STAT
;;
*)
fun_usage
esac
}
main $*
3. Redis安全配置
redis
没有用户的概念,只有密码
。redis
默认是工作在保护模式
下,任何用户可以登录到redis
命令行模式,但是无权限做任何操作(protected-mode)。
1.使用另一台机器(172.16.1.171)远程连接到redis服务端(172.16.1.170)观察报错提示
该机器必须要有redis命令,也就是安装了redis,但是不用启动服务
# redis-cli -h 172.16.1.170
10.0.0.170:6379> set name xmh
(error) DENIED Redis is running in protected mode because protected
mode is enabled, no bind address was specified, no authentication
password is requested to clients. In this mode connections are only
accepted from the loopback interface. If you want to connect from
external computers to Redis you may adopt one of the following
solutions:
1) Just disable protected mode sending the command 'CONFIG
SET protected-mode no' from the loopback interface by connecting to
Redis from the same host the server is running, however MAKE SURE Redis
is not publicly accessible from internet if you do so. Use CONFIG
REWRITE to make this change permanent.
2) Alternatively you can just
disable the protected mode by editing the Redis configuration file, and
setting the protected mode option to 'no', and then restarting the
server.
3) If you started the server manually just for testing, restart
it with the '--protected-mode no' option.
4) Setup a bind address or an
authentication password. NOTE: You only need to do one of the above
things in order for the server to start accepting connections from the
outside.
上面提示报错有四种关闭安全模式的方法,但是强烈推荐使用Bind的方式
2.使用第四种方法关闭安全模式
//1.修改/application/redis-6379/redis.conf,添加如下参数
# vim /application/redis-6379/redis.conf
bind 172.16.1.170 127.0.0.1
requirepass xmh123
参数解释:
bind #指定一个供外部访问的地址(Redis网卡地址),还有一个本地访问的地址
requirepass #设置密码
//2.重启redis服务
redis-cli shutdown
redis-server /data/6379/redis.conf
3.远程和本地登录测试
//1.使用linux-node2远程登录到redis服务端测试
# redis-cli -h 172.16.1.170 -a xmh123
172.16.1.170:6379> set name zhangsan
OK
172.16.1.170:6379> get name
"zhangsan"
//2.linux-node1登录本地的redis服务测试
# redis-cli
127.0.0.1:6379> set name zs #没办法直接设置键值,需要认证
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth xmh123 #输入密码,通过认证后才能做操作
OK
127.0.0.1:6379> set name zs
OK
4.通过redis-ctl中的config set参数,直接让配置文件生效,不需要重启redis服务这里我就不修改了
//例如,把登录redis密码修改为123456
127.0.0.1:6379> CONFIG SET requirepass 123456
127.0.0.1:6379> exit #退出下次登录配置就会生效
提示:只有个别配置支持在线修改,而不是所有参数
Note:如果设置了Redis认证密码,在重启redis的时候需要-a 指定密码