---
title: "Redis系列-安装部署维护篇"
author: "ssxxjjii"
date: "2013-04-07T22:40:57Z"
tags: []
categories: ["互联网"]
slug: "redis"
draft: false
lastmod: "2013-04-07T22:40:57Z"
id: 1841446
url: "https://tdcq.iteye.com/blog/1841446"
---

http://blog.csdn.net/love\_\_coder/article/details/8271832

 Redis是个高性能的key-value数据库，它的key具有丰富的数据结构：string，hash，list set和sorted set。作为NOSQL，比起memcache之类，不仅仅key数据结构丰富，而且具有持久化的功能，并且能够支持主从复制，很方便构建集群。redis高性能很大程度上源于它是个内存型数据库，它的高性能表现在：set操作11w/s，get操作8.1w/s，与其他类型数据库性能差异，可以而参考：[http://timyang.net/data/mcdb-tt-redis/](http://timyang.net/data/mcdb-tt-redis/)   。为了进一步加深对redis的理解总结，我打算写个redis系列的博客。这里主要谈谈redis安装部署及运维维护。

1、下载安装

 

**\[plain]** [view plain](http://blog.csdn.net/love__coder/article/details/8271832 "view plain")[copy](http://blog.csdn.net/love__coder/article/details/8271832 "copy")

1. \[root@xsf003 tool]# wget -c http://redis.googlecode.com/files/redis-2.4.17.tar.gz    #下载
2. \[root@xsf003 tool]# tar -zxvf redis-2.4.17.tar.gz   #解压
3. \[root@xsf003 tool]# cd redis-2.4.17
4. \[root@xsf003 redis-2.4.17]# make  #编译
5. \[root@xsf003 redis-2.4.17]# make install #安装

安装完毕，常用工具会自动拷贝到/user/loca/bin目录下。做为服务器，我们常常还需要把redis设置成开机自启动，源码包中有个很好用的脚本，执行脚步根据提示输入即可。

 

 

**\[plain]** [view plain](http://blog.csdn.net/love__coder/article/details/8271832 "view plain")[copy](http://blog.csdn.net/love__coder/article/details/8271832 "copy")

01. \[root@xsf003 redis-2.4.17]# cd utils/
02. \[root@xsf003 utils]# ./install\_server.sh
03. Welcome to the redis service installer
04. This script will help you easily set up a running redis server
05. Please select the redis port for this instance: \[6379]
06. Selecting default: 6379
07. Please select the redis config file name \[/etc/redis/6379.conf]
08. Selected default - /etc/redis/6379.conf
09. Please select the redis log file name \[/var/log/redis\_6379.log]
10. Selected default - /var/log/redis\_6379.log
11. Please select the data directory for this instance \[/var/lib/redis/6379]
12. Selected default - /var/lib/redis/6379
13. Please select the redis executable path \[/usr/local/bin/redis-server]
14. Copied /tmp/6379.conf =&gt; /etc/init.d/redis\_6379
15. Installing service...
16. Successfully added to chkconfig!
17. Successfully added to runlevels 345!
18. Starting Redis server...
19. Installation successful!

 

     注意执行install\_server.sh，需要先进入utils目录，不然脚本会报错，提示找不到相应文件。安装完服务，redis自动启动，可以用ps命令查看到相关信息：

 

**\[plain]** [view plain](http://blog.csdn.net/love__coder/article/details/8271832 "view plain")[copy](http://blog.csdn.net/love__coder/article/details/8271832 "copy")

1. \[root@xsf003 utils]# ps -ef | grep redis
2. root      4554     1  0 10:55 ?        00:00:02 /usr/local/bin/redis-server /etc/redis/6379.conf
3. root      4564  2808  0 10:59 pts/0    00:00:00 grep redis

2、手动启动关闭服务

 

 

**\[plain]** [view plain](http://blog.csdn.net/love__coder/article/details/8271832 "view plain")[copy](http://blog.csdn.net/love__coder/article/details/8271832 "copy")

1. \[root@xsf003 utils]# /etc/init.d/redis\_6379 stop   #关闭
2. \[root@xsf003 utils]# /etc/init.d/redis\_6379 start  #启动

也可以用下面类似的命令直接启动关闭redis服务：

 

**\[plain]** [view plain](http://blog.csdn.net/love__coder/article/details/8271832 "view plain")[copy](http://blog.csdn.net/love__coder/article/details/8271832 "copy")

1. /usr/local/bin/redis-server /etc/redis/redis.conf   #指定配置文件 启动
2. /usr/local/bin/redis-cli -p 6379 shutdown   # 关闭，如果默认端口6379 可以直接 /usr/local/bin/redis-cli shutdown

    

 

   3、通过客户端命令行工具连接redis服务查看redis相关信息

 

a)连接

 

**\[plain]** [view plain](http://blog.csdn.net/love__coder/article/details/8271832 "view plain")[copy](http://blog.csdn.net/love__coder/article/details/8271832 "copy")

1. \[root@xsf003 utils]# redis-cli
2. redis 127.0.0.1:6379&gt;

b）其他指令

 

 

**\[plain]** [view plain](http://blog.csdn.net/love__coder/article/details/8271832 "view plain")[copy](http://blog.csdn.net/love__coder/article/details/8271832 "copy")

01. redis 127.0.0.1:6379&gt; info  #查看server版本内存使用连接等信息
02. redis 127.0.0.1:6379&gt; client list  #获取客户连接列表
03. redis 127.0.0.1:6379&gt; client kill 127.0.0.1:33441 #终止某个客户端连接
04. redis 127.0.0.1:6379&gt; dbsize #当前保存key的数量
05. redis 127.0.0.1:6379&gt; save #立即保存数据到硬盘
06. redis 127.0.0.1:6379&gt; bgsave #异步保存数据到硬盘
07. redis 127.0.0.1:6379&gt; flushdb #当前库中移除所有key
08. redis 127.0.0.1:6379&gt; flushall #移除所有key从所有库中
09. redis 127.0.0.1:6379&gt; lastsave #获取上次成功保存到硬盘的unix时间戳
10. redis 127.0.0.1:6379&gt; monitor #实时监测服务器接收到的请求
11. redis 127.0.0.1:6379&gt; slowlog len #查询慢查询日志条数
12. (integer) 3
13. redis 127.0.0.1:6379&gt; slowlog get #返回所有的慢查询日志，最大值取决于slowlog-max-len配置
14. redis 127.0.0.1:6379&gt; slowlog get 2 #打印两条慢查询日志
15. redis 127.0.0.1:6379&gt; slowlog reset #清空慢查询日志信息

通过以上操作，单台服务器基本跑起来了，不过后面的路还很长很长。。。。

 

 

参考文章：

http://redis.io/topics/introduction

http://timyang.net/data/mcdb-tt-redis/

http://redis.io/commands#server

http://code.google.com/p/redis/