一. redis 简介:
Redis是一个key-value存储系统。和Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表)、sets(集合)和zsets(有序集合)几种数据类型。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。
二、redis安装:
(1). 安装基础包
[root@localhost src]# yum-y install make gcc gcc-c++ cmake bison-devel ncurses-devel libaio cpp binutilsglibc glibc-kernheaders glibc-common glibc-devel
(2). 安装tcl
[root@localhost src]# wget http://downloads.sourceforge.net/tcl/tcl8.5.10-src.tar.gz
[root@localhost src]# tar -zxf tcl8.5.10-src.tar.gz
[root@localhost src]# cd tcl8.5.10/unix/
[root@localhostunix]# ./configure
[root@localhost unix]# make && make install
(3). 安装redis
[root@localhost src]# wget http://download.redis.io/releases/redis-3.2.1.tar.gz
[root@localhost src]# tar -zxf redis-3.2.1.tar.gz
[root@localhost src]# cd redis-3.2.1
[root@localhost redis-3.2.1]# make
[root@localhost redis-3.2.1]# make test
注意:这里很可能会在make test 这步出现一个错误:
[err]: Test replication partial resync: ok psync (diskless: yes, reconnect: 1) in tests/integration/replication-psync.tcl
Expected condition '[s -1 sync_partial_ok] > 0' to be true ([s -1 sync_partial_ok] > 0)
出现这个问题的原因可能是"测试点在配置比较低的机器上会因为超时而过不了",本文的环境是一个lxc的虚拟机。不过有2个方法可以避免:
1:在解压目录中修改
# vi tests/integration/replication-psync.tcl
把 after 100 改成 after 500
2:用taskset来make test
# taskset -c 1 make test
!!! WARNING The following tests failed:
*** [err]: PEXPIRE/PSETEX/PEXPIREAT can set sub-second expires in tests/unit/expire.tcl
Expected ‘somevalue {}‘ to equal or match ‘{} {}‘
*** [err]: Slave should be able to synchronize with the master in tests/integration/replication-psync.tcl
Replication not started.
Cleanup: may take some time... OK
Makefile:215: recipe for target ‘test‘ failed
make: *** [test] Error 1
注意看错误信息,注意看错误信息,注意看错误信息!
是修改tests/unit/expire.tcl:
tags {"slow"} {
test {EXPIRE - After 2.1 seconds the key should no longer be here} {
after 21000
list [r get x] [r exists x]
} {{} 0}
}
test {EXPIRE - write on expire should work} {
r del x
r lpush x foo
r expire x 10000
r lpush x bar
r lrange x 0 -1
} {bar foo}
(3). 修改配置文件
[root@localhost redis-3.2.1]# mkdir /etc/redis
[root@localhost redis-3.2.1]# cpredis.conf /etc/redis/
三、redis启动:
[root@localhost redis-3.2.1]# /usr/local/src/redis-3.2.1/src/redis-server/etc/redis/redis.conf
[1650] 27 Apr 14:52:48.552 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.1 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1650
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[1650] 27 Apr 14:52:48.561 # Server started, Redis version 3.2.1
[1650] 27 Apr 14:52:48.562 * The server is now ready to accept connections on port 6379
但是,现在redis仍然是在前台运行。
如果要后台启动该怎么办呢?
如果需要redis后台运行需要将redis.confdaemonize由no改为yes。
[root@localhost ~]# /usr/local/src/redis-3.2.1/src/redis-cli
127.0.0.1:6379> set name liwen
OK
127.0.0.1:6379> get name
"liwen"
127.0.0.1:6379>
停止:
[root@localhost redis-3.2.1]# /usr/local/src/redis-3.2.1/src/redis-cli shutdown
为了以后运维更轻松,可以利用alias做几个别名
[root@localhost ~]# vim .bashrc
alias redis='cd /usr/local/src/redis-3.2.1/src/'
alias startRedis='/usr/local/src/redis-3.2.1/src/redis-server /etc/redis/redis.conf &'
alias stopRedis='/usr/local/src/redis-3.2.1/src/redis-cli shutdown'
alias loginRedis='/usr/local/src/redis-3.2.1/src/redis-cli'
redis 即可直接进入redis根目录
startRedis 即启动redis
stopRedis 即停止redis
loginRedis 即登录redis
四、redis设置密码,提供远程登录
vim redis.conf
requirepass yourpassword
远程登录:
src/redis-cli -h 192.168.5.245
授权命令:
auth yourpassword
五、配置redis仅做为缓存使用
如果不打算使用事务、管线等一堆复杂功能,仅仅把redis当成cache server使用,可以在配置文件中,找到maxmemory、maxmemory-policy这二项,参考下面修改
maxmemory 2048mb
maxmemory-policy allkeys-lru
即:最大允许使用2G内存,所有key全都按LRU(近期最少使用)算法淘汰,这种情况下,不用设置过期时间,只要内存使用达到上限,不怎么使用的key自然被干掉。
附:redis.conf的微优化配置(从百度百科上抄过来的)
其中:
端口、38行-最大内存使用量、以及var目录路径,大家根据实际情况自行调整