CentOS安裝Redis

1、安裝Redis

一、下載、解壓、編譯

shell> wget http://download.redis.io/releases/redis-5.0.8.tar.gz
shell> cd /usr/local
shell> tar -zxvf /path/to/redis-5.0.8.tar.gz
shell> cd redis-5.0.8
shell> make
複製代碼

二、啓動

shell> src/redis-server
26997:C 21 Mar 2020 12:37:20.174 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26997:C 21 Mar 2020 12:37:20.174 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=26997, just started
26997:C 21 Mar 2020 12:37:20.174 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 26997
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

26997:M 21 Mar 2020 12:37:20.175 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
26997:M 21 Mar 2020 12:37:20.175 # Server initialized
26997:M 21 Mar 2020 12:37:20.175 # 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.
26997:M 21 Mar 2020 12:37:20.175 # 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.
26997:M 21 Mar 2020 12:37:20.175 * Ready to accept connections
複製代碼

三、測試(使用內置的客戶端與Redis進行交互)

shell> src/redis-cli
127.0.0.1:6379> PING
PONG
複製代碼

以上就是官網的安裝步驟了,比較簡單,但實際應用中每每不止如此,下面就介紹下實戰中經常使用的一些配設置redis

2、添加到/user/local/bin

在解壓後的Redis目錄執行如下命令,能夠把redis命令安裝到/user/local/bin目錄,因爲/user/local/bin默認是在環境變量PATH中的,因此安裝後就無需在解壓後的redis源碼目錄中執行相關命令了,能夠在任何地方執行,比較方便shell

shell> make install
cd src && make install
make[1]: Entering directory `/usr/local/redis-5.0.8/src'
    CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis-5.0.8/src'
make[1]: Entering directory `/usr/local/redis-5.0.8/src'

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

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/redis-5.0.8/src'
shell> ll /usr/local/bin/
total 32772
-rwxr-xr-x 1 root root 4366808 Mar 21 19:21 redis-benchmark
-rwxr-xr-x 1 root root 8125000 Mar 21 19:21 redis-check-aof
-rwxr-xr-x 1 root root 8125000 Mar 21 19:21 redis-check-rdb
-rwxr-xr-x 1 root root 4807792 Mar 21 19:21 redis-cli
lrwxrwxrwx 1 root root      12 Mar 21 19:21 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8125000 Mar 21 19:21 redis-server
複製代碼

若是不想安裝到默認路徑/user/local/bin,能夠經過PREFIX選項指定其餘路徑vim

shell> make PREFIX=/some/other/directory instal
複製代碼

3、經常使用配置

官方文檔中的src/redis-server是直接使用默認配置啓動,咱們能夠經過如下命令指定配置文件bash

shell> src/redis-server /path/to/redis.conf
複製代碼

固然,也可使用命令行參數來指定配置項markdown

shell> src/redis-server /path/to/redis.conf --port 9999 --loglevel debug
複製代碼

Redis目錄中會有一個配置文件redis.conf,咱們能夠基於這個文件進行修改,啓動時只需指定該配置文件便可app

shell> src/redis-server redis.conf
複製代碼

更爲常見的一種作法多是把該配置文件拷貝至/etc目錄,如:ide

shell> mkdir /etc/redis
shell> cp redis.conf /etc/redis/6379.conf
shell> src/redis-server /etc/redis/6379.conf
複製代碼

下面開始編輯經常使用配置項測試

shell> vim /etc/redis/6379.conf
複製代碼

一、以守護進程模式運行(後臺運行)

daemonize no
複製代碼

修改成ui

daemonize yes
複製代碼

二、容許遠程訪問

關閉保護模式this

protected-mode yes
複製代碼

修改成

protected-mode no
複製代碼

取消主機綁定,註釋掉如下配置

# bind 127.0.0.1
複製代碼

三、設置密碼

# requirepass foobared
複製代碼

打開註釋,修改成本身的密碼

requirepass 123456
複製代碼

注意,若是是以服務方式啓動(下面有介紹),設置密碼後還須要修改腳本/etc/init.d/reds_6379,不然中止服務時會報「無權限」錯誤

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
# 新增下面一行
PASSWORD=123456
...

複製代碼

找到shutdown命令

$CLIEXEC -p $REDISPORT shutdown
複製代碼

修改成:

$CLIEXEC -a $PASSWORD -p $REDISPORT shutdown
複製代碼

4、設置開機啓動

一、設置爲服務

shell> mkdir /etc/redis
shell> cp /usr/local/redis-5.0.8/redis.conf /etc/redis/6379.conf
shell> cp /usr/local/redis-5.0.8/utils/redis_init_script /etc/init.d/redis_6379
複製代碼

二、啓動&中止

shell> systemctl start redis_6379.service
複製代碼
shell> systemctl stop redis_6379.service
複製代碼

CentOS 6:

shell> service redis_6379 start
複製代碼
shell> service redis_6379 stop
複製代碼

三、設置開機啓動

shell> chkconfig redis_6379 on
shell> chkconfig --list
redis              0:關    1:關    2:開    3:開    4:開    5:開    6:關
複製代碼

以上步驟還能夠經過一種更簡便的方式實現,在redis/utils目錄下有個install_server.sh腳本,執行下便可完成安裝服務、設置開機啓動以及配置文件位置等相關設置

shell> utils/install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
/var/run/redis_6379.pid exists, process is already running or crashed
Installation successful!
複製代碼

細心的小夥伴可能已經發現了,官方文檔的安裝教程雖然簡單,可是解壓後的Redis目錄中實際上是有個README.md幫助文檔的,基本的配置介紹的仍是很詳細的,上面的配置過程也主要是來自該文檔:

Building Redis

Redis can be compiled and used on Linux, OSX, OpenBSD, NetBSD, FreeBSD. We support big endian and little endian architectures, and both 32 bit and 64 bit systems.

It may compile on Solaris derived systems (for instance SmartOS) but our support for this platform is best effort and Redis is not guaranteed to work as well as in Linux, OSX, and *BSD there.

It is as simple as:

% make
複製代碼

You can run a 32 bit Redis binary using:

% make 32bit
複製代碼

After building Redis, it is a good idea to test it using:

% make test
複製代碼

Running Redis

To run Redis with the default configuration just type:

% cd src
% ./redis-server
複製代碼

If you want to provide your redis.conf, you have to run it using an additional parameter (the path of the configuration file):

% cd src
% ./redis-server /path/to/redis.conf
複製代碼

It is possible to alter the Redis configuration by passing parameters directly as options using the command line. Examples:

% ./redis-server --port 9999 --replicaof 127.0.0.1 6379
% ./redis-server /etc/redis/6379.conf --loglevel debug
複製代碼

All the options in redis.conf are also supported as options using the command line, with exactly the same name.

Installing Redis

In order to install Redis binaries into /usr/local/bin just use:

% make install
複製代碼

You can use make PREFIX=/some/other/directory install if you wish to use a different destination.

Make install will just install binaries in your system, but will not configure init scripts and configuration files in the appropriate place. This is not needed if you want just to play a bit with Redis, but if you are installing it the proper way for a production system, we have a script doing this for Ubuntu and Debian systems:

% cd utils
% ./install_server.sh
複製代碼

The script will ask you a few questions and will setup everything you need to run Redis properly as a background daemon that will start again on system reboots.

You'll be able to stop and start Redis using the script named /etc/init.d/redis_<portnumber>, for instance /etc/init.d/redis_6379.

相關文章
相關標籤/搜索