初识redis:安装与配置


    redis是一个开源的key-value存储引擎。它支持string、hash、list、set和sorted set等多种数值类型。提供了Python,Ruby,Erlang,PHP等客户端。它跟memcached类似,不过数据可以持久化,支持在服务器端计算集合的并,交和补集(difference)等,还支持多种排序功能。所以Redis也可以被看成是一个数据结构服务 器。   

    Redis的所有数据都是保存在内存中,然后不定期的通过异步方式保存到磁盘上(这称为“半持久化模式”);也可以把每一次数据变化都写入到一个append only file(aof)里面(这称为“全持久化模式”)。

    接下来就来尝试安装,并让其run起来。


  • 安装

1. 源码下载

wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz

或者,curl http://redis.googlecode.com/files/redis-2.4.2.tar.gz > redis-2.4.2.tar.gz

2. 解压

tar -xvf redis-2.4.2.tar.gz

3. 编译,安装

在redis-2.4.2目录下执行make;make install。


OK,这样在src/目录下就会生成redis-server,redis-cli,redis-check-dump,redis-benchmark等可执行文件。

其中,

redis-server:Redis服务器的daemon启动程序

redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作

redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能


  • 配置

redis.conf:

# 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.
# 是否以后台daemon形式运行
daemonize no

# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
# pid文件的位置
 pidfile /var/run/redis.pid

# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
# 端口号
 port 6379

# Close the connection after a client is idle for N seconds (0 to disable)
# 请求超时时间
 timeout 300

# Set server verbosity to 'debug'
# it can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
# log信息的级别
 loglevel verbose

# Specify the log file name. Also 'stdout' 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
# 打印log的输出流
logfile stdout

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
# 开启数据库的数量
databases 16

#   save <seconds> <changes>
# 保存快照的频率,表示多长时间执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。
save 900 1
save 300 10
save 60 10000

# 是否使用压缩
rdbcompression yes

# The filename where to dump the DB
# 数据快照的文件名,不包括目录,只是文件名
 dbfilename dump.rdb

# The working directory.
# 数据快照的目录
dir ./

# When a slave lost the connection with the master, or when the replication
# is still in progress, the slave can act in two different ways:
#
# 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
#    still reply to client requests, possibly with out of data data, or the
#    data set may just be empty if this is the first synchronization.
#
# 2) if slave-serve-stale data is set to 'no' the slave will reply with
#    an error "SYNC with master in progress" to all the kind of commands
#    but to INFO and SLAVEOF.
#
slave-serve-stale-data yes

# 是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率
appendonly no
# appendonlylog如何同步到磁盘,默认为“everysec”
appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

slowlog-log-slower-than 10000

vm-enabled no

vm-swap-file /tmp/redis.swap

vm-max-memory 0

vm-page-size 32

vm-pages 134217728

vm-max-threads 4

############################### ADVANCED CONFIG ###############################

hash-max-zipmap-entries 512
hash-max-zipmap-value 64

list-max-ziplist-entries 512
list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128
zset-max-ziplist-value 64

activerehashing yes

可以根据需要修改该配置,如:

daemonize yes
pidfile ./redis.pid
port 6379
timeout 300
loglevel debug
save 900 1
save 300 10
save 60 10000

  • run it

1. 启动服务

redis-server ../redis.conf
查看redis是否启动:ps -aux | grep "redis"

或者

./redis-cli ping  

PONG

2. 命令行客户端

redis-cli

set key-value

./redis-cli set key1 value1

OK

./redis-cli set key2 value2

OK

get key

./redis-cli get key1

"value1"

./redis-cli get key3
(nil)

del key

./redis-cli del key1
(integer) 1

更多redis-cli命令:http://redis.io/commands

http://slj.me/2011/04/redis-cli-commands/

3. 停止服务

./redis-cli shutdown 

或者,关闭指定端口的redis-server

 ./redis-cli -p 6379 shutdown


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值