在CentOS下安装redis

关键字:Linux, CentOS, Redis server

1. 安装redis

1.1 下载redis

根据需要的版本下载redis,这里以4.0.6版本为例。执行以下命令

wget http://download.redis.io/releases/redis-4.0.6.tar.gz

这里我直接下载到了/usr/local中,因为我们即将在这个位置安装redis。

1.2 解压缩

执行以下命令

tar -zxvf redis-4.0.6.tar.gz

解压之后会生成一个redis-4.0.6的文件夹

1.3 源码安装

进入该目录后,执行以下命令进行编译

cd redis-4.0.6 && make MALLOC=libc

编译完成后,会自动建立一个src的目录,里面有所有用到的.c和.h文件以及生成的.o文件等

(venv) [root@localhost redis-4.0.6]# cd src
(venv) [root@localhost src]# ls
adlist.c     bio.c        crc64.c       evict.c           intset.c      memtest.o        pubsub.o           redis-benchmark.o  replication.c   setproctitle.o  testhelp.h    ziplist.h
adlist.h     bio.h        crc64.h       evict.o           intset.h      mkreleasehdr.sh  quicklist.c        redis-check-aof    replication.o   sha1.c          t_hash.c      ziplist.o
adlist.o     bio.o        crc64.o       expire.c          intset.o      module.c         quicklist.h        redis-check-aof.c  rio.c           sha1.h          t_hash.o      zipmap.c
ae.c         bitops.c     db.c          expire.o          latency.c     module.o         quicklist.o        redis-check-aof.o  rio.h           sha1.o          t_list.c      zipmap.h
ae_epoll.c   bitops.o     db.o          fmacros.h         latency.h     modules          rand.c             redis-check-rdb    rio.o           siphash.c       t_list.o      zipmap.o
ae_evport.c  blocked.c    debug.c       geo.c             latency.o     multi.c          rand.h             redis-check-rdb.c  scripting.c     siphash.o       t_set.c       zmalloc.c
ae.h         blocked.o    debugmacro.h  geo.h             lazyfree.c    multi.o          rand.o             redis-check-rdb.o  scripting.o     slowlog.c       t_set.o       zmalloc.h
ae_kqueue.c  childinfo.c  debug.o       geohash.c         lazyfree.o    networking.c     rax.c              redis-cli          sdsalloc.h      slowlog.h       t_string.c    zmalloc.o
ae.o         childinfo.o  defrag.c      geohash.h         lzf_c.c       networking.o     rax.h              redis-cli.c        sds.c           slowlog.o       t_string.o
ae_select.c  cluster.c    defrag.o      geohash_helper.c  lzf_c.o       notify.c         rax_malloc.h       redis-cli.o        sds.h           solarisfixes.h  t_zset.c
anet.c       cluster.h    dict.c        geohash_helper.h  lzf_d.c       notify.o         rax.o              redismodule.h      sds.o           sort.c          t_zset.o
anet.h       cluster.o    dict.h        geohash_helper.o  lzf_d.o       object.c         rdb.c              redis-sentinel     sentinel.c      sort.o          util.c
anet.o       config.c     dict.o        geohash.o         lzf.h         object.o         rdb.h              redis-server       sentinel.o      sparkline.c     util.h
aof.c        config.h     dump.rdb      geo.o             lzfP.h        pqsort.c         rdb.o              redis-trib.rb      server.c        sparkline.h     util.o
aof.o        config.o     endianconv.c  help.h            Makefile      pqsort.h         redisassert.h      release.c          server.h        sparkline.o     valgrind.sup
asciilogo.h  crc16.c      endianconv.h  hyperloglog.c     Makefile.dep  pqsort.o         redis-benchmark    release.h          server.o        syncio.c        version.h
atomicvar.h  crc16.o      endianconv.o  hyperloglog.o     memtest.c     pubsub.c         redis-benchmark.c  release.o          setproctitle.c  syncio.o        ziplist.c

接下来将/usr/local/redis-4.0.6/src目录下的文件加到/usr/local/bin下,执行命令

cd src && make install

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
(base) [root@localhost ~]# ll /usr/local/bin
总用量 9428
-rwxr-xr-x. 1 root root  290422 12月 30 17:03 redis-benchmark
-rwxr-xr-x. 1 root root 2977570 12月 30 17:03 redis-check-aof
-rwxr-xr-x. 1 root root 2977570 12月 30 17:03 redis-check-rdb
-rwxr-xr-x. 1 root root  428943 12月 30 17:03 redis-cli
lrwxrwxrwx. 1 root root      12 12月 30 17:03 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 2977570 12月 30 17:03 redis-server
(base) [root@localhost ~]# 

2. 运行redis server

2.1 直接启动

直接在/usr/local/redis-4.0.6/src目录下执行下面的命令

./redis-server

(venv) [root@localhost src]# ./redis-server
29150:C 30 Dec 17:05:30.652 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29150:C 30 Dec 17:05:30.652 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=29150, just started
29150:C 30 Dec 17:05:30.652 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
29150:M 30 Dec 17:05:30.654 * Increased maximum number of open files to 10032 (it was originally set to 1024).
               _._                                                  
          _.-``__ ''-._                                             
     _.-``    `.  `_.  ''-._           Redis 4.0.6 (00000000/0) 64 bit
 .-`` .-```.  ```\/    _.,_ ''-._                                   
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 29150
 `-._    `-._  `-./  _.-'    _.-'                                   
|`-._`-._    `-.__.-'    _.-'_.-'|                                  
|    `-._`-._        _.-'_.-'    |           http://redis.io        
 `-._    `-._`-.__.-'_.-'    _.-'                                   
|`-._`-._    `-.__.-'    _.-'_.-'|                                  
|    `-._`-._        _.-'_.-'    |                                  
 `-._    `-._`-.__.-'_.-'    _.-'                                   
     `-._    `-.__.-'    _.-'                                       
         `-._        _.-'                                           
             `-.__.-'                                               

29150:M 30 Dec 17:05:30.659 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
29150:M 30 Dec 17:05:30.659 # Server initialized
29150:M 30 Dec 17:05:30.659 # 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.
29150:M 30 Dec 17:05:30.659 # 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.
29150:M 30 Dec 17:05:30.659 * Ready to accept connections

2.2 设置为守护进程

出现这个界面就说明我们的redis server已经成功运行了,但是有时候我们希望redis作为一个后台进程来执行,这样我们还可以在这个终端做其他的工作,那么可以配置redis为守护进程。具体操作如下:
退回上一级目录,也就是/usr/local/redis-4.0.6,用vim打开该目录下的配置文件redis.conf

vi redis.conf

 133 ################################# GENERAL #####################################
 134 
 135 # By default Redis does not run as a daemon. Use 'yes' if you need it.
 136 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
 137 daemonize yes

找到并将daeminize这个参数设置为yes就可以了,默认是no
保存退出后再次运行,就可以看到变成这样:

(venv) [root@localhost src]# ./redis-server /usr/local/redis-4.0.6/redis.conf 
29528:C 30 Dec 18:39:25.065 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29528:C 30 Dec 18:39:25.065 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=29528, just started
29528:C 30 Dec 18:39:25.065 # Configuration loaded

上面返回了一个pid=29528,我们可以查一下redis进程

(venv) [root@localhost src]# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root     29529  0.2  0.2 140420  2060 ?        Ssl  18:39   0:00 ./redis-server 127.0.0.1:6379                   
root     29536  0.0  0.0 103336   868 pts/1    S+   18:39   0:00 grep --color=auto redis

可以看到实际上的pid是29529,这里很奇怪,查到的pid总是比redis返回的pid大1,ps命令的pid才是真正的pid,这点儿可以用kill命令测试就知道了

(venv) [root@localhost src]# kill 29529
(venv) [root@localhost src]# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root     29543  0.0  0.0 103336   868 pts/1    S+   18:40   0:00 grep --color=auto redis

2.3 设置为系统服务

为了方便我们使用,可以把redis作为系统服务随着系统自动启动,这样我们就不用每次手动启动了。

  1. 进入/etc目录并在该目录下创建一个名为redis的文件夹,然后将安装目录下的配置文件复制到该文件夹中并以6379.conf命名
(venv) [root@localhost etc]# mkdir redis
(venv) [root@localhost etc]# cd redis
(venv) [root@localhost redis]# cp /usr/local/redis-4.0.6/redis.conf /etc/redis/6379.conf
  1. 将redis的启动脚本复制一份到/etc/init.d下面并重命名为redisd
(venv) [root@localhost init.d]# cp /usr/local/redis-4.0.6/utils/redis_init_script /etc/init.d/redisd
  1. 将redis服务设置为开机启动
    如果直接使用chkconfig命令会有问题
(venv) [root@localhost init.d]# chkconfig redisd on
redisd 服务不支持 chkconfig

解决方法也很简单,只需要在刚刚复制的那个文件开头加上下面两行就可以了

 #!/bin/sh
 # chkconfig: 2345 90 10
 # discription:Redis is a persistent key-value database

注意这虽然是注释,但是也是有意义的,和上面的#!/bin/sh一样

  • chkconfig:redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10
  • discription:仅仅是一个描述信息

再次执行上面的指令就成功了。

接下来就可以使用service命令启动redis server了, 同样可以用ps指令看到已经在运行了。

(venv) [root@localhost init.d]# service redisd start
Starting Redis server...
29609:C 30 Dec 18:53:28.256 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29609:C 30 Dec 18:53:28.257 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=29609, just started
29609:C 30 Dec 18:53:28.257 # Configuration loaded
(venv) [root@localhost init.d]# ps -aux | grep redis
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root     29610  0.1  0.1  43560  2020 ?        Ssl  18:53   0:11 /usr/local/bin/redis-server 127.0.0.1:6379      
root     30361  0.0  0.0 103336   864 pts/1    S+   21:40   0:00 grep --color=auto redis

停止服务可以用下面的指令

(venv) [root@localhost init.d]# service redisd stop
Stopping ...
Redis stopped

当然也可以使用

redis-cli SHUTDOWN

来停止redis

参考文献

  1. http://blog.csdn.net/zc474235918/article/details/50974483
  2. http://blog.csdn.net/gxw19874/article/details/51992125
  3. http://blog.csdn.net/luozhonghua2014/article/details/54649295
  4. https://www.cnblogs.com/zuidongfeng/p/8032505.html
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容