Redis入門

來源:互聯網
上載者:User

標籤:

一、安裝

目前,官方最新穩定版本為3.0.7

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

# cd /usr/local/

# tar xvf /root/redis-3.0.7.tar.gz

# cd redis-3.0.7/

# make

 

二、啟動

安裝完成後,在src目錄下會產生啟動執行程式,包括redis-server,redis-sentinel, redis-benchmark,redis-cli等

# src/redis-server 

該啟動方式是前台啟動,如果關閉當前終端,則redis會自動關閉

正如登入資訊開頭Warning所顯示的,這種方式啟動沒有使用設定檔,所以並不推薦。預設監聽6379連接埠

24649:C 03 Feb 16:32:30.242 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf24649:M 03 Feb 16:32:30.243 * Increased maximum number of open files to 10032 (it was originally set to 1024).                _._                                                             _.-``__ ‘‘-._                                                   _.-``    `.  `_.  ‘‘-._           Redis 3.0.7 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ‘‘-._                                    (    ‘      ,       .-`  | `,    )     Running in standalone mode |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6379 |    `-._   `._    /     _.-‘    |     PID: 24649  `-._    `-._  `-./  _.-‘    _.-‘                                    |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                   |    `-._`-._        _.-‘_.-‘    |           http://redis.io          `-._    `-._`-.__.-‘_.-‘    _.-‘                                    |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                   |    `-._`-._        _.-‘_.-‘    |                                    `-._    `-._`-.__.-‘_.-‘    _.-‘                                         `-._    `-.__.-‘    _.-‘                                                 `-._        _.-‘                                                         `-.__.-‘                                               24649:M 03 Feb 16:32:30.246 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.24649:M 03 Feb 16:32:30.246 # Server started, Redis version 3.0.724649:M 03 Feb 16:32:30.246 # 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.24649:M 03 Feb 16:32:30.246 # 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.24649:M 03 Feb 16:32:30.247 * The server is now ready to accept connections on port 6379

 

關於redis-server的更多用法,可通過redis-server -h查看

# src/redis-server -h

Usage: ./redis-server [/path/to/redis.conf] [options]       ./redis-server - (read config from stdin)       ./redis-server -v or --version       ./redis-server -h or --help       ./redis-server --test-memory <megabytes>Examples:       ./redis-server (run the server with default conf)       ./redis-server /etc/redis/6379.conf       ./redis-server --port 7777       ./redis-server --port 7777 --slaveof 127.0.0.1 8888       ./redis-server /etc/myredis.conf --loglevel verboseSentinel mode:       ./redis-server /etc/sentinel.conf --sentinel

 

設定檔中常用參數如下:

daemonize:是否以後台daemon方式運行,預設是前台方式運行,即預設值為no

pidfile:pid檔案位置,預設為:/run/redis.pid

port:監聽的連接埠號碼,預設為6379

bind 127.0.0.1 配置監聽網卡的ip,針對有多個網卡的情境

logfile:log檔案位置,預設值為stdout,使用“標準輸出”,預設後台模式會輸出到/dev/null

loglevel notice ,指定日誌記錄層級,Redis總共支援四個層級:debug,verbose,notice,warning,預設為notice

     Debug:記錄很多資訊,用於開發與測試

     Verbose:很多精簡的有用資訊,不像debug會記錄那麼多

     Notice:普通的verbose,常用於生產環境

     Warning:只有非常重要或者嚴重的資訊會記錄到日誌

 

三、設定開機自啟動

 將啟動指令碼複製到/etc/init.d目錄下

# cp /usr/local/redis-3.0.7/utils/redis_init_script /etc/init.d/redisd

編輯啟動指令碼

# vim /etc/init.d/redisd

#!/bin/sh# chkconfig:2345 90 10# Simple Redis init.d script conceived to work on Linux systems# as it does use of the /proc filesystem.REDISPORT=6379EXEC=/usr/local/redis-3.0.7/src/redis-server#EXEC=/usr/local/bin/redis-serverCLIEXEC=/usr/local/redis-3.0.7/src/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pidCONF="/etc/redis/${REDISPORT}.conf"

 

主要做了兩項修改,

一、添加了# chkconfig:2345 90 10

二、指定了redis-server和redis-cli的位置

EXEC=/usr/local/redis-3.0.7/src/redis-server

CLIEXEC=/usr/local/redis-3.0.7/src/redis-cli

注意:

PIDFILE=/var/run/redis_${REDISPORT}.pid指定了pid檔案的位置

CONF="/etc/redis/${REDISPORT}.conf"指定了設定檔的位置

 

建立設定檔

# cd /etc/

# mkdir redis

# cp /usr/local/redis-3.0.7/redis.conf redis/6379.conf

 

修改設定檔

主要是設定redis以後台進程運行和pid檔案的位置

daemonize yespidfile /var/run/redis_6379.pid

 

以服務方式啟動redis

# /etc/init.d/redisd start

Starting Redis server...

# ps -ef |grep redis

root      29836      1  0 18:23 ?        00:00:00 /usr/local/redis-3.0.7/src/redis-server *:6379root      29846   4110  0 18:23 pts/0    00:00:00 grep --color=auto redis

 

用戶端串連測試

# cd /usr/local/redis-3.0.7/src/

# ./redis-cli 

127.0.0.1:6379> set 123 helloOK127.0.0.1:6379> get 123"hello"

預設串連到localhost 6379,查看伺服器資訊,可通過info命令。

 

Redis入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.