Redis Quick Start

https://redis.io/topics/quickstart

安装redis

官方建议方式:从源代码编译安装,依赖gcc和libc。linux的包管理工具可能不是最新版。
下载地址:redis.io或者 http://download.redis.io/redis-stable.tar.gz.
编译步骤:

wget http://download.redis.io/redis-stable.tar.gz
tar zxvf redis-stable.tar.gz
cd redis-stable
make

至此,编译完成,可以试试输入make test
src目录中包含多个可执行文件:

  • redis-server:redis服务器
  • redis-sentinel:redis哨兵
  • redis-cli:命令行交互接口
  • redis-benchmark:用于检查redis性能
  • redis-check-aof 和 redis-check-dump:用于罕见的崩溃数据文件

进行如下复制或者直接 sudo make install

sudo cp src/redis-server /usr/local/bin/
sudo cp src/redis-cli /usr/local/bin/

开始使用Redis

启动redis服务
$ redis-server

$ redis-server
[28550] 01 Aug 19:29:28 # Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'
[28550] 01 Aug 19:29:28 * Server started, Redis version 2.2.12
[28550] 01 Aug 19:29:28 * The server is now ready to accept connections on port 6379
... more logs ...

以上使用默认参数启动,未指明配置文件,仅用于开发。生产环境应指定配置文件,使用 $ redis-server /etc/redis.conf ,参考redis源代码中的 redis.conf。

检查Redis是否正常工作

外部程序使用相应的库与redis进行交互。直接交互则使用redis-cli
首先,通过PING检查redis是否启动。

$ redis-cli ping
PONG

执行redis-cli后面跟命令和参数,将会把命令发送给运行在6379端口上的redis实例。
可以直接执行redis-cli,启动命令行交互模式:

$redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set mkey mvalue
OK
127.0.0.1:6379> get mkey
mvalue

学习一下redis的数据类型必不可少,https://redis.io/topics/data-types-intro

安全

Redis默认没有安全验证,需要与外网隔离,确保安全。按照如下步骤进行安全确认:
1. 将redis监听端口加入防火墙,默认6379,集群模式再加上16379,26379用于哨兵。
2. Use a configuration file where the bind directive is set in order to guarantee that Redis listens just in as little network interfaces you are using. For example only the loopback interface (127.0.0.1) if you are accessing Redis just locally from the same computer, and so forth.
3. 在conf中使用requirepass选项添加一层安全层,使client需要使用AUTH命令进行安全验证。
4. 使用spiped或ssl对redis server与redis client之间的通信进行加密。

在应用程序中使用redis

在应用程序中使用redis,需要先下载相应语言的redis client 库。不同语言的client库
例如,Java可以选择Jedis,Ruby可以选择redis-rb。使用方法是先构造一个redis对象,然后执行相应的调用redis方法的命令。
以Ruby为例:

>> require 'rubygems'
=> false
>> require 'redis'
=> true
>> r = Redis.new
=> #<Redis client v2.2.1 connected to redis://127.0.0.1:6379/0 (Redis v2.3.8)>
>> r.ping
=> "PONG"
>> r.set('foo','bar')
=> "OK"
>> r.get('foo')
=> "bar"

持久化

Redis如何进行持久化
如果使用默认配置启动Redis,持久化只能依赖Redis自发进行,如果想要将数据持久化并在重启后重新加载,则应在需要时手动调用 SAVE 命令,另外确保使用 SHUTDOWN 关闭:$redis-cli shutdown
以上方式,确保退出前,redis完成了数据的持久化。

正确安装Redis

官方建议使用初始化脚本安装Redis。
假定已经把 redis-serverredis-cli 拷贝到 /usr/local/bin

  • 创建存放redis配置文件和数据的路径:
sudo mkdir /etc/redis
sudo mkdir /var/redis
  • util路径下的初始化脚本拷贝到 /etc/init.d 路径下,官方建议命名带端口:
sudo cp utils/redis_init_script /etc/init.d/redis_6379
  • 编辑初始化脚本:
sudo vi /etc/init.d/redis_6379

Make sure to modify REDISPORT accordingly to the port you are using. Both the pid file path and the configuration file name depend on the port number.

  • 将redis根目录下的配置文件拷贝到/etc/redis/,用端口号作为配置文件名:
sudo cp redis.conf /etc/redis/6379.conf
  • /var/redis 目录下创建一个目录,作为Redis实例的工作路径:
sudo mkdir /var/redis/6379
  • 编辑配置文件,改动如下:
###GENERAL###
daemonize yes  #设置daemonize为yes,后台运行,默认为no,
picfile /var/run/redis_6379.pid  #后台运行时,redis会写pidfile,设置redis写入到/var/run/redis_6379.pid
port 6379  #指定端口
tcp-backlog 511
bind 192.168.x.x 127.0.0.1
timeout 0  #client空闲N秒后关闭连接,0则禁止关闭
tcp-keepalive 0  #
loglevel debug  #设置日志级别
logfile "/usr/local/redis_6379.log"  #设置日志文件
databases 16  #数据库的数量
###SNAPSHOTTING###
dbfilename dump.rdb  #filename where to dump the DB
dir /var/redis/6379  #设置工作路径,DB将会被写入该目录,使用dbfilename指定的文件名
  • 最后一步,将redis初始化脚本加入默认的runlevels:
sudo update-rc.d redis_6379 defaults

然后就可以使用 sudo /etc/init.d/redis_6379 start 运行redis实例。
验证:
- 使用redis-cli 能ping通
- 测试保存数据,确认数据保存到了/var/redis/6379/或对应目录
- 确认redis记录了日志
- 确认机器重启后一切仍正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值