Linux Centos5 Redis2.4.2安装与配置

来源:互联网 发布:印度人知乎 编辑:程序博客网 时间:2024/06/05 14:24

第一步:下载安装编译

#wgethttp://redis.googlecode.com/files/redis-2.4.2.tar.gz
#tar zxvf redis-2.4.2.tar.gz
#cd redis-2.4.2
#make
#make install
#cp redis.conf /etc/

第二步:修改配置

#vi /etc/redis.conf

配置见附录

第三步:启动进程

#redis-server/etc/redis.conf

查看进程有没有成功启动

#ps -ef | grep redis

测试输入一个键值

#redis-cli set test"123456"

获取键值

#redis-cli get test

关闭redis
# redis-cli shutdown //关闭所有
关闭某个端口上的redis
# redis-cli -p 6397 shutdown //关闭6397端口的redis

说明:关闭以后缓存数据会自动dump到硬盘上,硬盘地址见redis.conf中的dbfilename dump.rdb

附录:无错配置

只要做如下配置即可:

daemonize yes
pidfile /var/run/redis.pid
port 6379

#bind 127.0.0.1
timeout 600
loglevel notice
logfile /elain/logs/redis/redis.log

databases 16

save 900 1
save 300 10
save 60 10000

rdbcompression yes
dbfilename dump.rdb

dir /elain/data/redis/

# maxclients 128

appendonly yes
appendfilename appendonly.aof

# appendfsync always
appendfsync everysec
# appendfsync no

requirepass elain

no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

slowlog-log-slower-than 10000

slowlog-max-len 1024

really-use-vm yes
vm-enabled no
vm-swap-file /tmp/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4

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

# include /path/to/local.conf
# include /path/to/other.conf

 

 

1,是否以后台进程运行,默认为no
daemonize no

2,如以后台进程运行,则需指定一个pid,默认为/var/run/redis.pid
pidfile /var/run/redis.pid

3,监听端口,默认为6379
port 6379

4,绑定主机IP,默认值为127.0.0.1(注释)
bind 127.0.0.1

5,超时时间,默认为300(秒)
timeout 300

6,日志记录等级,有4个可选值,debug,verbose(默认值),notice,warning
loglevel verbose

7,日志记录方式,默认值为stdout
logfile stdout

8,可用数据库数,默认值为16,默认数据库为0
databases 16

9,指出在多长时间内,有多少次更新操作,就将数据同步到数据文件。这个可以多个条件配合,比如默认配置文件中的设置,就设置了三个条件。

900秒(15分钟)内至少有1个key被改变
save 900 1
300秒(5分钟)内至少有10个key被改变
save 300 10

10,存储至本地数据库时是否压缩数据,默认为yes
rdbcompression yes

11,本地数据库文件名,默认值为dump.rdb
dbfilename /root/redis_db/dump.rdb

12,本地数据库存放路径,默认值为 ./
dir /root/redis_db/

13,当本机为从服务时,设置主服务的IP及端口(注释)
slaveof <masterip> <masterport>

14,当本机为从服务时,设置主服务的连接密码(注释)
masterauth <master-password>

15,连接密码(注释)
requirepass foobared

16,最大客户端连接数,默认不限制(注释)
maxclients 128

17,设置最大内存,达到最大内存设置后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理后,任到达最大内存设置,将无法再进行写入操作。(注释)
maxmemory <bytes>

18,是否在每次更新操作后进行日志记录,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认值为no
appendonly yes

19,更新日志文件名,默认值为appendonly.aof(注释)
appendfilename /root/redis_db/appendonly.aof

20,更新日志条件,共有3个可选值。no表示等操作系统进行数据缓存同步到磁盘,always表示每次更新操作后手动调用fsync()将数据写到磁盘,everysec表示每秒同步一次(默认值)。
appendfsync everysec

21,是否使用虚拟内存,默认值为no
vm-enabled yes

22,虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
vm-swap-file /tmp/redis.swap

23,将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的 (Redis的索引数据就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0。
vm-max-memory 0

24,虚拟内存文件以块存储,每块32bytes
vm-page-size 32

25,虚拟内在文件的最大数
vm-pages 134217728

26,可以设置访问swap文件的线程数,设置最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的.可能会造成比较长时间的延迟,但是对数据完整性有很好的保证.
vm-max-threads 4

27,把小的输出缓存放在一起,以便能够在一个TCP packet中为客户端发送多个响应,具体原理和真实效果我不是很清楚。所以根据注释,你不是很确定的时候就设置成yes
glueoutputbuf yes

28,在redis 2.0中引入了hash数据结构。当hash中包含超过指定元素个数并且最大的元素没有超过临界时,hash将以一种特殊的编码方式(大大减少内存使用)来存储,这里可以设置这两个临界值
hash-max-zipmap-entries 64

29,hash中一个元素的最大值
hash-max-zipmap-value 512

30,开启之后,redis将在每100毫秒时使用1毫秒的CPU时间来对redis的hash表进行重新hash,可以降低内存的使用。当你的使 用场景中,有非常严格的实时性需要,不能够接受Redis时不时的对请求有2毫秒的延迟的话,把这项配置为no。如果没有这么严格的实时性要求,可以设置 为yes,以便能够尽可能快的释放内存
activerehashing yes

 

 

JAVA测试类如下:

package com.vopzoon.app.base.queue;import org.springframework.stereotype.Component;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;/** * 利用key-value内存数据库redis实现的任务队列集合 * @author liuwang * */@Componentpublic class QueueHelper {/** * redis pool池 */private JedisPool pool;/** * redis客服端 */private Jedis client;/** * 构造函数 * @param host IP地址 * @param port 端口 * @param isAuth 是否需要密码 false表示不需要密码 * @param password */public QueueHelper(String host,int port,boolean isAuth,String password){pool = new JedisPool(new JedisPoolConfig(), host, port);client = pool.getResource();if(isAuth){client.auth(password);}}public QueueHelper(String host, int port) {this(host, 6379, false, null);}public QueueHelper(String host) {this(host, 6379, false, null);}public QueueHelper(String host, String password) {this(host, 6379, true, password);}public QueueHelper(String host, int port, String password) {this(host, port, true, password);}/** * 从 List 头部添加一个元素 * @param key * @param value * @return 1表示成功 0表示key不存在 key对应的不是list返回错误 */public long pushAtHead(String key,String value){return client.lpush(key,value);}/** * 从 List 尾部添加一个元素 * @param key * @param value * @return 1表示成功 0表示key不存在 key对应的不是list返回错误 */public long pushAtTail(String key,String value){return client.rpush(key,value);}/** * 弹出key对应的list第一个元素 * @param key * @return 第一个元素,如果key对应的list不存在或者是空返回null *         如果key对应值不是list返回错误 */public String popAtHead(String key){return client.lpop(key);}/** * 弹出key对应的list最后一个元素 * @param key * @return 最后一个元素,如果key对应的list不存在或者是空返回null *         如果key对应值不是list返回错误 */public String popAtTail(String key){return client.rpop(key);}/** * 覆盖原来的数据 * @param key 对应的key值 * @param value 对应的value值 * @return */public String replace(String key, String value) {return client.set(key, value);}/** * 删除队列中的记录 * @param key 记录的key值 * @return */public long delete(String key) {return client.del(key);}/** * 获取key对应的list的长度 * @param key * @return */public long getQueueSize(String key){return client.llen(key);}/** * 清空所有数据库 */public void clearAllData(){client.flushAll();}/** * 关闭资源 */    public void destory() {//使用完之后将连接返回到连接池中        pool.returnResource(client);                //销毁连接池中的所有连接        pool.destroy();    }//test实例public static void main(String []args){QueueHelper client = new QueueHelper("10.1.101.222","xxxx");//long result = client.pushAtHead("aaa", "bbb");//System.out.println(result);System.out.println(client.popAtHead("aaa"));//清空redis所有数据库(慎用)//client.clearAllData();}}


QueueHelper client = new QueueHelper("10.1.101.222","xxxx");xxxx是配置的密码

在redis.conf中的requirepass xxxx配置

补充一下, 在linux中, 如果设置了密码, 连接客户端的时候

redis-cli -a xxxx

[root@queue001 ~]# redis-cli --helpredis-cli 2.4.2Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]  -h <hostname>    Server hostname (default: 127.0.0.1)  -p <port>        Server port (default: 6379)  -s <socket>      Server socket (overrides hostname and port)  -a <password>    Password to use when connecting to the server  -r <repeat>      Execute specified command N times  -i <interval>    When -r is used, waits <interval> seconds per command.                   It is possible to specify sub-second times like -i 0.1.  -n <db>          Database number  -x               Read last argument from STDIN  -d <delimiter>   Multi-bulk delimiter in for raw formatting (default: \n)  --raw            Use raw formatting for replies (default when STDOUT is not a tty)  --latency        Enter a special mode continuously sampling latency.  --help           Output this help and exit  --version        Output version and exit

 

自己可以查看帮助嘛..呵呵

 

 

原创粉丝点击