Redis安装部署与维护详解

y37f 9年前

本文介绍Redis2.8的安装部署和维护方法。

Redis在linux上的安装

步骤1:

首先从官网下在redis正式版的压缩包redis-2.8.19.tar.gz

http://download.redis.io/releases/redis-2.8.19.tar.gz

步骤2:编译源程序:

tar zxvf redis-2.8.19.tar.gz

[neil@neilhost Downloads]$ tar zxvf redis-2.8.19.tar.gz
[neil@neilhost Downloads]$ cd redis-2.8.19/src

MongoDB解压之后就可以使用了,但Redis需要编译,但是没有配置。

[neil@neilhost src]$ make  .......此处是大量编译过程,省略。可能有一些警告,不去官它们.............      CC setproctitle.o      CC hyperloglog.o      CC latency.o      CC sparkline.o      LINK redis-server      INSTALL redis-sentinel      CC redis-cli.o      LINK redis-cli      CC redis-benchmark.o      LINK redis-benchmark      CC redis-check-dump.o      LINK redis-check-dump      CC redis-check-aof.o      LINK redis-check-aof    Hint: It's a good idea to run 'make test' ;)

进入src进行安装

[neil@neilhost src]$ sudo make install  [sudo] password for neil:       Hint: It's a good idea to run 'make test' ;)        INSTALL install      INSTALL install      INSTALL install      INSTALL install      INSTALL install

这时候,我们可以看看src下的文件:

[neil@neilhost src]$ ll

Redis安装部署与维护详解

可以看到此时,src文件夹下出现了一些绿色的文件,这些文件就是我们以后需要用到的命令文件。

步骤3:

移动文件,便于管理:(所有源代码安装的软件都安装在/usr/local下,如apache等)

创建两个文件夹,bin用于存放命令,etc拥有存放配置文件。

[neil@neilhost src]$ sudo mkdir -p /usr/local/redis/bin  [neil@neilhost src]$ sudo mkdir -p /usr/local/redis/etc

-p是递归创建。

接下来,将redis-2.8.19文件夹下的redis.conf复制到/usr/local/redis/etc/

并将src目录下的7个命令文件(绿色的),移动到/usr/local/redis/bin/

[neil@neilhost src]$ cd ..  [neil@neilhost redis-2.8.19]$ ls  00-RELEASENOTES  COPYING  Makefile   redis.conf        sentinel.conf  utils  BUGS             deps     MANIFESTO  runtest           src  CONTRIBUTING     INSTALL  README     runtest-sentinel  tests  [neil@neilhost redis-2.8.19]$ sudo mv ./redis.conf /usr/local/redis/etc/  [sudo] password for neil:   [neil@neilhost redis-2.8.19]$ cd src  [neil@neilhost src]$ sudo mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server /usr/local/redis/bin/

步骤4:启动Redis服务:

首先进入刚才安装redis的目录:

[neil@neilhost src]$ cd /usr/local/redis/bin  [neil@neilhost bin]$ ls  mkreleasehdr.sh  redis-check-aof   redis-cli       redis-server  redis-benchmark  redis-check-dump  redis-sentinel

之后我们启动redis服务。启动redis服务需要用到命令redis-server

[neil@neilhost bin]$ sudo ./redis-server  [sudo] password for neil:   [13019] 15 Mar 22:34:24.568 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf  [13019] 15 Mar 22:34:24.569 * Increased maximum number of open files to 10032 (it was originally set to 1024).                  _._                                                               _.-``__ ''-._                                                     _.-``    `.  `_.  ''-._           Redis 2.8.19 (00000000/0) 64 bit    .-`` .-```.  ```\/    _.,_ ''-._                                      (    '      ,       .-`  | `,    )     Running in stand alone mode   |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379   |    `-._   `._    /     _.-'    |     PID: 13019    `-._    `-._  `-./  _.-'    _.-'                                      |`-._`-._    `-.__.-'    _.-'_.-'|                                     |    `-._`-._        _.-'_.-'    |           http://redis.io            `-._    `-._`-.__.-'_.-'    _.-'                                      |`-._`-._    `-.__.-'    _.-'_.-'|                                     |    `-._`-._        _.-'_.-'    |                                      `-._    `-._`-.__.-'_.-'    _.-'                                           `-._    `-.__.-'    _.-'                                                   `-._        _.-'                                                           `-.__.-'                                                   [13019] 15 Mar 22:34:24.570 # Server started, Redis version 2.8.19  [13019] 15 Mar 22:34:24.570 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.  [13019] 15 Mar 22:34:24.570 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.  [13019] 15 Mar 22:34:24.570 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.  [13019] 15 Mar 22:34:24.570 * The server is now ready to accept connections on port 6379

但是,这样做的话,我们并没有使用etc的下的配置文件进行启动。

如果希望通过指定的配置文件启动,需要在启动时指定配置文件:

这里我们先用ctrl+C来终止服务,然后查看redis服务是否终止干净了,之后通过设置配置文件来启动服务:

^C[13019 | signal handler] (1426430158) Received SIGINT scheduling shutdown...  [13019] 15 Mar 22:35:58.502 # User requested shutdown...  [13019] 15 Mar 22:35:58.502 * Saving the final RDB snapshot before exiting.  [13019] 15 Mar 22:35:58.683 * DB saved on disk  [13019] 15 Mar 22:35:58.683 # Redis is now ready to exit, bye bye...  [neil@neilhost bin]$ pstree -p | grep redis  [neil@neilhost bin]$ sudo ./redis-server /usr/local/redis/etc/redis.conf  [13054] 15 Mar 22:36:47.951 * Increased maximum number of open files to 10032 (it was originally set to 1024).                  _._                                                               _.-``__ ''-._                                                     _.-``    `.  `_.  ''-._           Redis 2.8.19 (00000000/0) 64 bit    .-`` .-```.  ```\/    _.,_ ''-._                                      (    '      ,       .-`  | `,    )     Running in stand alone mode   |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379   |    `-._   `._    /     _.-'    |     PID: 13054    `-._    `-._  `-./  _.-'    _.-'                                      |`-._`-._    `-.__.-'    _.-'_.-'|                                     |    `-._`-._        _.-'_.-'    |           http://redis.io            `-._    `-._`-.__.-'_.-'    _.-'                                      |`-._`-._    `-.__.-'    _.-'_.-'|                                     |    `-._`-._        _.-'_.-'    |                                      `-._    `-._`-.__.-'_.-'    _.-'                                           `-._    `-.__.-'    _.-'                                                   `-._        _.-'                                                           `-.__.-'                                                   [13054] 15 Mar 22:36:47.952 # Server started, Redis version 2.8.19  [13054] 15 Mar 22:36:47.952 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.  [13054] 15 Mar 22:36:47.952 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.  [13054] 15 Mar 22:36:47.952 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.  [13054] 15 Mar 22:36:47.952 * DB loaded from disk: 0.000 seconds  [13054] 15 Mar 22:36:47.952 * The server is now ready to accept connections on port 6379

但是,现在redis仍然是在前台运行。

如果要后台启动该怎么办呢?

回想一下,后台启动mysql的方法是在后面加一个 &符号。如:

#mysql_safe --user=mysql &

后台启动mongodb,则在后面加上一个 --fork。具体方法请看我之前的MongDB入门系列(2)。

但是,这两种方法都不适用于redis。redis的后台启动并运行需要通过配置文件中的参数设置。

Redis的配置文件中有哪些配置呢?

daemonize 如果需要在后台运行,把该项改为yes

pidfile 配置多个pid的地址 默认在/var/run/redis.pid

bind 绑定ip,设置后只接受来自该ip的请求

port 监听端口,默认是6379

loglevel 分为4个等级:debug verbose notice warning

logfile 用于配置log文件地址

databases 设置数据库个数,默认使用的数据库为0

save 设置redis进行数据库镜像的频率。

rdbcompression 在进行镜像备份时,是否进行压缩

dbfilename 镜像备份文件的文件名

Dir 数据库镜像备份的文件放置路径

Slaveof 设置数据库为其他数据库的从数据库

Masterauth 主数据库连接需要的密码验证

Requriepass 设置 登陆时需要使用密码

Maxclients 限制同时使用的客户数量

Maxmemory 设置redis能够使用的最大内存

Appendonly 开启append only模式

Appendfsync 设置对appendonly.aof文件同步的频率(对数据进行备份的第二种方式)

vm-enabled 是否开启虚拟内存支持   (vm开头的参数都是配置虚拟内存的)

vm-swap-file 设置虚拟内存的交换文件路径

vm-max-memory 设置redis使用的最大物理内存大小

vm-page-size 设置虚拟内存的页大小

vm-pages 设置交换文件的总的page数量

vm-max-threads 设置VM IO同时使用的线程数量

Glueoutputbuf 把小的输出缓存存放在一起

hash-max-zipmap-entries 设置hash的临界值

Activerehashing 重新hash

本文是oschina博客用户happyBKs的文章,转载请声明出处。http://my.oschina.net/u/1156339/blog/387335

言归正传,如果需要redis后台运行需要将daemonize由no改为yes。

首先在超级权限下打开redis.conf。

[neil@neilhost bin]$ sudo gedit /usr/local/redis/etc/redis.conf

之后将daemonize由no改为yes。

Redis安装部署与维护详解

保存退出。

之后我们再次使用配置文件启动redis-server。

可以看到,redis是后台启动了,并且通过ps命令可以查看到redis正在运行。

[neil@neilhost bin]$ sudo ./redis-server /usr/local/redis/etc/redis.conf  [neil@neilhost bin]$ ps -ef | grep redis  root     13154     1  0 22:53 ?        00:00:00 ./redis-server *:6379  neil     13162  8143  0 22:54 pts/0    00:00:00 grep --color=auto redis  [neil@neilhost bin]$ pstree -p | grep redis             |-redis-server(13154)-+-{redis-server}(13156)             |                     `-{redis-server}(13157)


Redis服务端默认连接端口是6379.

mysql 服务端默认连接端口是3306

Mogodb 服务端默认连接端口是27017,还有28017。

在平时,我们往往需要查看6379端口是否被占用。可以用以下命令:

[neil@neilhost bin]$ netstat -tunpl | grep 6379  (Not all processes could be identified, non-owned process info   will not be shown, you would have to be root to see it all.)  tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      -                     tcp6       0      0 :::6379                 :::*                    LISTEN      -                     [neil@neilhost bin]$ sudo netstat -tunpl | grep 6379  [sudo] password for neil:   tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      13154/./redis-serve   tcp6       0      0 :::6379                 :::*                    LISTEN      13154/./redis-serve

注意,redis服务需要su权限才能查看,不然只能检查到6379被某个进程占用,但是看不到进程名称。

至此,redis服务已经按照配置文件启动成功!!

客户端登陆

步骤5:

客户端连接

[neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli   127.0.0.1:6379>


关闭Redis服务

步骤6:

停止Redis实例

我们可以使用pkill redis-server

[neil@neilhost bin]$ sudo pkill redis-server  [neil@neilhost bin]$ sudo netstat -tunpl | grep 6379  [neil@neilhost bin]$   [neil@neilhost bin]$ pstree -p | grep redis  [neil@neilhost bin]$   [neil@neilhost bin]$   [neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli   Could not connect to Redis at 127.0.0.1:6379: Connection refused  not connected>   not connected> exit


关闭之后,发现6379就不再被占用了,redis的进程也都没有了。

客户登陆也无法成功了。


也可以使用/usr/local/redis/bin/redis-cli shutdown,这种方法使用客户端命令redis-cli 

[neil@neilhost bin]$ sudo ./redis-server /usr/local/redis/etc/redis.conf  [neil@neilhost bin]$ pstree -p | grep redis             |-redis-server(13509)-+-{redis-server}(13511)             |                     `-{redis-server}(13512)  [neil@neilhost bin]$ sudo /usr/local/redis/bin/redis-cli shutdown  [neil@neilhost bin]$ pstree -p | grep redis  [neil@neilhost bin]$   [neil@neilhost bin]$ sudo netstat -tunpl | grep 6379  [neil@neilhost bin]$   [neil@neilhost bin]$

还可以使用命令killall和kill -9,可以仿照我在之前博客中的关闭Mongdb的方法。