ubuntu下redis安装配置

  • A+
所属分类:Redis

ubuntu下redis安装配置

一.redis介绍

redis是一个key-value存储系统,与memcached类似,但是解决了断电后数据完全丢失的现象。支持数据类型有string,lists,sets,zsets。这些数据类型都支持push/pop,add/remove以及取交集并集差集等操作,对这些操作都是原子性的,redis还支持各种不同的排序能力。

二.redis安装

$ sudo wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz

$ sudo tar zxvf redis-2.2.12.tar.gz -C ../software/

$ sudo cd /usr/local/src/software/redis-2.2.12/src

$ sudo make PREFIX=/usr/local/redis-2.2.12

$ sudo make test

Testing Redis version 2.2.12 (00000000)

831 tests, 831 passed, 0 failed

$ sudo make PREFIX=/usr/local/redis-2.2.12 install

常见错误:

(cd ..; tclsh8.5 tests/test_helper.tcl --tags "" --file "")

/bin/sh: tclsh8.5: not found

make: *** [test] Error 127

解决方法:

$ sudo apt-get install tcl8.5

三.redis配置

$ sudo mkdir -p /usr/local/redis-2.2.12/{etc,var}

redis-server:redis服务的启动程序

redis-cli:redis命令行操作工具

redis-benchmark:redis性能测试工具

redis-check-aof:更新日志检查

redis-check-dump:本地数据检查

$ sudo cp redis.conf /usr/local/redis-2.2.12/etc/

redis.conf配置参数说明:

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

pidfile /var/run/redis.pid //pid文件路径

port 6379 //监听端口

bind 127.0.0.1 //绑定主机ip

unixsocket /tmp/redis.sock //sock文件路径

timeout 300 //超时时间,默认是300s

# 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)

loglevel verbose //日志等级,可选项有debug,verbose,notice,warning 默认是erbose

logfile stdout //日志记录方式,默认是stdout

syslog-enabled no //日志记录到系统日志中,默认是no

syslog-ident redis //指定系统日志标识

# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.

syslog-facility local0 //指定系统日志设备,默认是local0

# 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 //可用数据库数,默认值是16,默认数据库是0

save <seconds> <changes> //在多长时间内,有多少次更新操作,就将数据同步到数据文件。

save 900 1 //15min内至少1个key被改变

save 300 10 //5min内至少有300个key被改变

save 60 10000 //60s内至少有10000个key被改变

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

dbfilename dump.rdb //本地数据库文件名,默认是dump.rdb

dir ./ //本地数据库存放路径,默认是./

slaveof <masterip> <masterport> //当本机为从服务时,设置主服务的ip以及端口

masterauth <master-password> //主服务的连接密码

# 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

requirepass foobared //连接密码foobared

maxclients 128 //最大连接数,默认不限制

maxmemory <bytes> //设置最大内存,达到最大内存设置后,redis会先尝试清除已到期或即将到期的key,当此方法处理后,任然到达最大内存设置,将无法再进行写入操作

maxmemory设置策略

# volatile-lru -> remove the key with an expire set using an LRU algorithm

# allkeys-lru -> remove any key accordingly to the LRU algorithm

# volatile-random -> remove a random key with an expire set

# allkeys->random -> remove a random key, any key

# volatile-ttl -> remove the key with the nearest expire time (minor TTL)

# noeviction -> don't expire at all, just return an error on write operations

maxmemory-policy volatile-lru //maxmemory设置策略,默认是volatile-lru.

maxmemory-samples 3

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

appendfilename appendonly.aof //更新日志文件名,默认是appendonly.aof

redis支持的三种不同的同步方式:

# no: don't fsync, just let the OS flush the data when it wants. Faster. //等待OS进行数据缓存同步到硬盘

# always: fsync after every write to the append only log . Slow, Safest. //每次更新操作后调用fsync()将数据写到磁盘

# everysec: fsync only if one second passed since the last fsync. Compromise. //每秒同步一次

appendfsync everysec //更新日志条件,默认是everysec

no-appendfsync-on-rewrite no

slowlog-log-slower-than 10000 //设置redis slow log时间,只包括命令执行时间,不包括IO操作时间,比如客户端连接,应答相应时间等等。单位是microseconds(一百万分之一秒),默认是10000.负值表示禁用slow log,0表示记录所有命令。

slowlog-max-len 1024 //slowlog最大长度1024.这会消耗内存,使用SLOWLOG RESET来回收slowlog内存。

#在redis2.4版本,强烈不建议使用virtual memory。

vm-enabled no //是否使用虚拟内存,默认是no

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

vm-max-memory 0 //设置最大vm,默认为0,所有的value存在于磁盘中。

vm-page-size 32 //设置vm的page大小,默认是32

vm-pages 134217728 //设置swap文件中最大memory pages,默认是134217728。swap大小=vm-page-size * vm-pages

vm-max-threads 4 //vm同时运行的最大io线程

#指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法

hash-max-zipmap-entries 512 //配置字段最多512个

hash-max-zipmap-value 64 //配置value最大为64字节

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

activerehashing yes //是否重置hash表

include /path/to/other.conf //引用其他配置文件

$ sudo vim /etc/sysctl.conf

vm.overcommit_memory = 1 //指定内核针对内存分配的策略,其值可以是0,1,2

0表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。

1表示内核允许分配所有的物理内存,而不管当前的内存状态如何。

2表示内核允许分配超过所有物理内存和交换空间总和的内存

$ sudo sysctl -p

$ sudo vim redis.conf

daemonize yes

pidfile /var/run/redis.pid

port 6379

timeout 300

loglevel verbose

logfile /usr/local/redis-2.2.12/var/log/redis.log

databases 16

save 900 1

save 300 10

save 60 10000

rdbcompression yes

dbfilename dump.rdb

dir /usr/local/redis-2.2.12/var/data

appendonly no

appendfsync everysec

no-appendfsync-on-rewrite no

slowlog-log-slower-than 10000

slowlog-max-len 1024

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

activerehashing yes

启动:

$ sudo /usr/local/redis-2.2.12/bin/redis-server /usr/local/redis-2.2.12/etc/redis.conf

四.测试

$ cat /etc/passwd | ./redis-cli -x set mykey

OK

$ ./redis-cli get mykey

"root:x:0:0:root:/root:/bin/bash\daemon:x:1:1:daemon:/usr/sbin:/bin/sh\mysql:x:111:120:MySQL Server,,,:/var/lib/mysql:/bin/false\"

转载请注明出处:http://www.ttlsa.com/html/1226.html

weinxin
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: