标签:
http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage
在Ubuntu下安装reids
redis-2.6.8.tar.gz
root@root01-virtual-machine:/opt# tar -zxvf redis-2.6.8.tar.gz
root@root01-virtual-machine:/opt# ls
redis-2.6.8 redis-2.6.8.tar.gz
root@root01-virtual-machine:/opt# cd redis-2.6.8/
root@root01-virtual-machine:/opt/redis-2.6.8# ls
00-RELEASENOTES COPYING Makefile redis.conf src
BUGS deps MANIFESTO runtest tests
CONTRIBUTING INSTALL README sentinel.conf utils
root@root01-virtual-machine:/opt/redis-2.6.8# make 编译
root@root01-virtual-machine:/opt/redis-2.6.8# sudo make install 安装
在安装过程中会看到生成文件
mkdir -p /usr/local/bin
INSTALL install
INSTALL install
此处学到了一个命令:/* mkdir -p mkdir的-p选项允许你一次性创建多层次的目录
root@root01-virtual-machine:/opt# mkdir -p /opt/a/b/c
/* root@root01-virtual-machine:/opt# mkdir -p /opt/{a,b}/c 创建的a和b的目录里均含有c
root@root01-virtual-machine:/opt# ls
a b redis-2.6.8 redis-2.6.8.tar.gz
root@root01-virtual-machine:/opt/a# ls
c
*/
通过上述mkdir -p生成的目录,安装文件会存在/usr/local/bin目录下
root@root01-virtual-machine:/usr/local/bin# ls
redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server
启动redis的命令之前需要配置redis.conf文件
1.daemonize no 是否以后台进程方式运行 改成yes
2.这两个参数
loglevel warning
logfile/var/log/redis.log
3. c.取消注释
syslog-enabled no #这个改成syslog-enabledyes
syslog-facility local0
d.数据文件目录
# Note that you mustspecify a directory here, not a file name.
dir ./usr/local/ 需设置成相应的目录 例: dir /var/db/redis
e.内存,连接数设置
maxmemory
maxclients
在正式启动redis之前,先创建数据目录
# mkdir /var/db/redis
否则会出现下面的错误
root@root01-virtual-machine:/opt/redis-2.6.8# redis-server /etc/redis.conf
[29058] 12 Jan 10:20:42.413 # Can‘t chdir to ‘./usr/local/‘: No such file or directory
把可执行文件放在
cp redis-benchmark redis-cli redis-server /usr/bin/ #这个倒是很有用,这样就不用再执行时加上./了,而且可以在任何地方执行
拷贝文件
cp redis.conf /etc/ 这个文件时redis启动的配置文件
执行完后可通过下边命令查询
root@root01-virtual-machine:/opt/redis-2.6.8/src# ps -ef|grep redis
root 3385 9325 0 11:11 pts/3 00:00:00 grep --color=auto redis
root 21615 15990 0 10:23 pts/4 00:00:00 vi redis.conf
服务端开启后,需要另打开一个客户端进行访问
redis-server conf/redis.conf
注:此命令仅有一个启动参数,指定<REDIS_FILE>目录下的配置文件,不加参数执行默认配置。
root@root01-virtual-machine:/opt/redis-2.6.8/src# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> ping
返回pong
查看端口是否被占用:
root@root01-virtual-machine:/opt/redis-2.6.8/src# netstat -ntlp |grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 4621/redis-server
关闭客户端即服务端也会关闭
root@root01-virtual-machine:/opt/redis-2.6.8/src# redis-cli shutdown
如果非默认端口,可指定端口:redis-cli -p 6380 shutdown
关闭redis服务
redis-cli shutdown
redis服务关闭后,缓存数据会自动dump到硬盘上,硬盘地址为redis.conf中的配置项dbfilename dump.rdb所设定
强制备份数据到磁盘,使用如下命令
redis-cli save 或者 redis-cli -p 6380 save(指定端口)
设置开机自动启动,关机自动关闭
root@root01-virtual-machine:/etc/init.d# update-rc.d redis-server defaults
update-rc.d: warning: /etc/init.d/redis-server missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
Adding system startup for /etc/init.d/redis-server ...
/etc/rc0.d/K20redis-server -> ../init.d/redis-server
/etc/rc1.d/K20redis-server -> ../init.d/redis-server
/etc/rc6.d/K20redis-server -> ../init.d/redis-server
/etc/rc2.d/S20redis-server -> ../init.d/redis-server
/etc/rc3.d/S20redis-server -> ../init.d/redis-server
/etc/rc4.d/S20redis-server -> ../init.d/redis-server
/etc/rc5.d/S20redis-server -> ../init.d/redis-server
启动Redis:
/etc/init.d/redis-server start
待完善
3. 初始化用户和日志路径
第一次启动Redis前,建议为Redis单独建立一个用户,并新建data和日志文件夹 sudo useradd redis
sudo mkdir -p /var/lib/redis
sudo mkdir -p /var/log/redis
sudo chown redis.redis /var/lib/redis
sudo chown redis.redis /var/log/redis
标签:
原文地址:http://www.cnblogs.com/shareyezi/p/3507737.html