Redis4.0.0 安裝及配置 (Linux — Centos7)

Redis4.0.0 安裝及配置 (Linux — Centos7)

96 x113773 關注linux

2017.07.17 15:04* 字數 991 閱讀 3130評論 0喜歡 6git

本文中的兩個配置文件可在這裏找到github

操做系統:Linux
Linux發行版:Centos7redis

安裝

  1. 下載地址,點這裏Redis4.0.0.tar.gz
    或者使用命令:
    wget http://download.redis.io/releases/redis-4.0.0.tar.gz緩存

  2. 而後執行make編譯源碼:bash

$ tar xzf redis-4.0.0.tar.gz
$ cd redis-4.0.0
$ make
  1. 編譯完成後啓動
    $ src/redis-server服務器

  2. 測試效果:網絡

$ src/redis-cli ping
PONG
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

make命令執行完成後,會在src目錄下生成6個可執行文件,分別是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel工具

  • redis-server is the Redis Server itself.(Redis服務器自己)
  • redis-sentinel is the Redis Sentinel executable (monitoring and failover).(Redis集羣的管理工具)
  • redis-cli is the command line interface utility to talk with Redis.(與Redis進行交互的命令行客戶端)
  • redis-benchmark is used to check Redis performances.(Redis性能測試工具)
  • redis-check-aof and redis-check-dump are useful in the rare event of corrupted data files.(AOF文件修復工具和RDB文件檢查工具)

能夠使用以下命令,把redis-server和redis-cli拷貝到合適的位置(/usr/local/bin/):性能

sudo cp src/redis-server /usr/local/bin/
sudo cp src/redis-cli /usr/local/bin/

或者,使用 sudo make install能夠把6個文件都拷貝過去;

這樣的話,只要/usr/local/bin/在PATH環境變量裏,


image

image

咱們就能夠直接使用redis-server和redis-cli而不須要指定全路徑了。好比:

[root@localhost ~]# redis-server
12408:C 16 Jul 21:30:29.657 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12408:C 16 Jul 21:30:29.657 # Redis version=4.0.0, bits=64, commit=00000000, mod                                                                             ified=0, pid=12408, just started
12408:C 16 Jul 21:30:29.657 # Warning: no config file specified, using the defau                                                                             lt config. In order to specify a config file use redis-server /path/to/redis.con                                                                             f
12408:M 16 Jul 21:30:29.658 * Increased maximum number of open files to 10032 (i                                                                             t was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 4.0.0 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 12408
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

12408:M 16 Jul 21:30:29.663 # WARNING: The TCP backlog setting of 511 cannot be                                                                              enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
12408:M 16 Jul 21:30:29.663 # Server initialized
12408:M 16 Jul 21:30:29.663 # WARNING overcommit_memory is set to 0! Background                                                                              save may fail under low memory condition. To fix this issue add 'vm.overcommit_m                                                                             emory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.ove                                                                             rcommit_memory=1' for this to take effect.
12408:M 16 Jul 21:30:29.664 # WARNING you have Transparent Huge Pages (THP) supp                                                                             ort enabled in your kernel. This will create latency and memory usage issues wit                                                                             h Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transpar                                                                             ent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to reta                                                                             in the setting after a reboot. Redis must be restarted after THP is disabled.
12408:M 16 Jul 21:30:29.664 * Ready to accept connections
$ redis-cli                                                                
redis 127.0.0.1:6379> ping
PONG
redis 127.0.0.1:6379> set mykey somevalue
OK
redis 127.0.0.1:6379> get mykey
"somevalue"

關閉redis服務

使用以下命令能夠關閉redis服務

$ redis-cli shutdown

配置初始化腳本,以服務方式啓動redis

  1. 拷貝redis-4.0.0下的utils目錄下的初始化腳本到/etc/init.d目錄,並重命名文件爲:redis_+端口號
    sudo cp utils/redis_init_script /etc/init.d/redis_6379
    確保redis_6379文件內的REDISPORT變量是你使用的端口號

  2. 新建文件夾/etc/redis/ ,並拷貝redis-4.0.0下的 redis.conf 文件到到改目錄下,使用端口號做爲文件名

sudo mkdir /etc/redis
sudo cp redis.conf /etc/redis/6379.conf
  1. 建立用來存儲redis持久化文件的目錄(6379爲端口號)
    sudo mkdir -p /var/redis/6379

  2. 編輯6379.conf文件,修改以下幾個參數:

  • Set daemonize to yes (by default it is set to no).(設置daemonize 爲yes,默認爲no)
  • Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).(若是端口號不是6379,則須要修改)
  • Change the port accordingly. In our example it is not needed as the default port is already 6379.(若是端口號不是6379,則須要修改)
  • Set your preferred loglevel.(能夠設置日誌等級,註釋上有說明)
  • Set the logfile to /var/log/redis/redis_6379.log(設置日誌文件路徑。須要在log下新建redis文件夾)
  • Set the dir to /var/redis/6379 (very important step!)(設置工做目錄爲 /var/redis/6379)
  1. 這時候就能夠使用以下兩條命令(任選其一)來啓動redis,並測試
[root@localhost /]# service redis_6379 start
Starting Redis server...
[root@localhost /]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
[root@localhost /]# /etc/init.d/redis_6379 start
Starting Redis server...

設置開機自動啓動

  1. 編輯/etc/init.d/redis_6379文件,在#!/bin/bash 以後添加以下兩行。
# chkconfig: 2345 10 90 
# description: redis_6379 service manage...

其中2345是默認啓動級別,級別有0-6共7個級別。

等級0表示:表示關機   
  等級1表示:單用戶模式   
  等級2表示:無網絡鏈接的多用戶命令行模式   
  等級3表示:有網絡鏈接的多用戶命令行模式   
  等級4表示:不可用   
  等級5表示:帶圖形界面的多用戶模式   
  等級6表示:從新啓動

10是啓動優先級,90是中止優先級,優先級範圍是0-100,數字越大,優先級越低

  1. 將redis_6379放入linux啓動管理體系中
    chkconfig --add redis_6379
    查看redis_6379服務在各運行級狀態
    chkconfig --list redis_6379

    image

    image

  2. 重啓服務器測試效果:
    reboot
    重啓完成後,直接使用redis-cli鏈接redis,效果以下:

Using username "root".
Last login: Sun Jul 16 21:30:16 2017 from 192.168.10.1
[root@localhost ~]# redis-cli
127.0.0.1:6379>

配置成功,完!

參考:
Redis入門指南(第2版)(順便推薦下這本書,做爲redis入門書籍很是不錯)
Redis Quick Start

相關文章:
Spring Boot 1.5.4集成Redis
Spring Boot 使用Redis緩存

 隨筆

相關文章
相關標籤/搜索