安装Redis
我这里安装的版本是 3.2.2
[root@log2 ~]# yum install readline-devel pcre-devel openssl-devel -y
[root@log2 local]# tar zxf redis-3.2.2.tar.gz
[root@log2 local]# cd redis-3.2.2/
[root@log2 redis-3.2.2]# make
[root@log2 redis-3.2.2]# make install
配置Redis
[root@log2 redis-3.2.2]# cp redis.conf /etc/
[root@log2 redis-3.2.2]# vim /etc/redis.conf
1.配置redis后台启动
打开/etc/redis.conf,将daemonize处修改为yes。
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
2.配置Redis持久化策略
使用RDB和AOF双持久化策略:其中默认开启了RDB持久化,我们只需要开启AOF持久化。
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.
appendonly yes
3.配置redis日志文件
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "/var/log/redis.log"
4.配置数据文件存放路径,在/目录下面创建/RedisData目录
[root@log2 redis-3.2.2]# mkdir /RedisData
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /RedisData
5.修改Redis监听地址
注释掉 #bind 127.0.0.1
否则redis只会监听在127.0.0.1的某个端口上。
6.修改Redis监听端口
为了安全,强烈建议修改redis的监听端口。
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6400
7.在redis3.2之后,redis增加了protected-mode,在这个模式下,即使注释掉了bind 127.0.0.1,再访问redis的时候还是报错,所以要如下设置:
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no
启动/停止
1.启动
[root@log2 ~]# /usr/local/bin/redis-server /etc/redis.conf
2.停止
[root@log2 ~]# /usr/local/redis-3.2.2/src/redis-cli -p 6400 shutdown
Redis简单使用
使用客户端连接redis-server
在Redis的安装目录中有redis客户端,即redis-cli(Redis Command Line Interface),它是Redis自带的基于命令行的Redis的客户端。
# cd /usr/local/bin/
# ./redis-cli -h 172.16.206.30 -p 6400
172.16.206.30:6400>
向Redis服务器发送命令
redis-cli连上redis服务后,可以在命令行发送命令。
1.ping,测试客户端与redis的连接是否正常,如果正常会收到回复PONG
# ./redis-cli -h 172.16.206.30 -p 6400
172.16.206.30:6400> ping
PONG
172.16.206.30:6400>
2.set/get,使用set和get可以向redis设置数据、获取数据
172.16.206.30:6400> set name wisedu
OK
172.16.206.30:6400> get name
"wisedu"
3.del,删除指定key的内容
172.16.206.30:6400> del name
(integer) 1
172.16.206.30:6400> get name
(nil)
4.keys *,查看当前库中所有的key