下载:
wget http://download.redis.io/releases/redis-3.2.12.tar.gz
解压:
tar xf redis-3.2.12.tar.gz
mv redis-3.2.12 redis
[root@db01 redis]# pwd
/data/redis
安装:
cd redis
make
启动:
src/redis-server &
添加环境变量:
vim /etc/profile
export PATH=/data/redis/src:$PATH
source /etc/profile
测试
redis-cli
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> get num
10
Redis基本管理操作
基础配置文件介绍:
mkdir /data/6379
vim /data/6379/redis.conf
daemonize yes
port 6379
logfile /data/6379/redis.log
dir /data/6379
dbfilename dump.rdb
重启redis
redis-cli shutdown
redis-server /data/6379/redis.conf
[root@db01 redis]# netstat -lnp|grep 63
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 9445/redis-server 1
tcp 0 0 10.0.0.51:6379 0.0.0.0:* LISTEN
截取部分
+++++++++++配置文件说明++++++++++++++
redis.conf
是否后台运行:
daemonize yes
默认端口:
port 6379
日志文件位置
logfile /var/log/redis.log
持久化文件存储位置
dir /data/6379
RDB持久化数据文件:
dbfilename dump.rdb
+++++++++++++++++++++++++
redis-cli 客户端命令常用参数说明
redis-cli 刚装完,可以在redis服务器上直接登录redis
-p 6379 指定端口号
-h 指定链接地址
-a 指定链接密码
redis-cli set num 10 ,无交互执行redis命令
cat /tmp/1.txt |redis-cli
[root@db01 ~]# redis-cli -h 10.0.0.51 -p 6379
10.0.0.51:6379>
(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.
-------------------------
redis安全配置
redis默认开启了保护模式,只允许本地回环地址登录并访问数据库。
禁止protected-mode
protected-mode yes/no (保护模式,是否只允许本地访问)
Bind :指定IP进行监听
vim /data/6379/redis.conf
bind 10.0.0.51 127.0.0.1
增加requirepass {password}
vim /data/6379/redis.conf
requirepass 123
----------验证-----
方法一:
[root@db01 ~]# redis-cli -a 123
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> exit
方法二:
[root@db01~]# redis-cli
127.0.0.1:6379> auth 123
OK
127.0.0.1:6379> set a b
在线查看和修改配置
CONFIG GET *
CONFIG GET requirepass
CONFIG SET requirepass 123
redis持久化(内存数据保存到磁盘)
作用:可以有效防止,在redis宕机后,缓存失效的问题.
RDB
AOF
RDB 持久化
可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot)。
优点:速度快,适合于用做备份,主从复制也是基于RDB持久化功能实现的。
缺点:会有数据丢失
rdb持久化核心配置参数:
vim /data/6379/redis.conf
dir /data/6379
dbfilename dump.rdb
save 900 1
save 300 10
save 60 10000
配置分别表示:
900秒(15分钟)内有1个更改
300秒(5分钟)内有10个更改
60秒内有10000个更改
AOF持久化(append-only log file)
记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原数据集。
AOF 文件中的命令全部以 Redis 协议的格式来保存,新命令会被追加到文件的末尾。
优点:可以最大程度保证数据不丢
缺点:日志记录量级比较大
AOF持久化配置
appendonly yes
appendfsync everysec
appendfsync always
appendfsync no
是否打开aof日志功能
每1个命令,都立即同步到aof
每秒写1次
写入工作交给操作系统,由操作系统判断缓冲区大小,统一写入到aof.
vim /data/6379/redis.conf
appendonly yes
appendfsync everysec