安裝
首先下載redis源碼,並使用tar進行解壓縮 (redis官網https://redis.io/download)
wget http://download.redis.io/releases/redis-5.0.3.tar.gz
tar xvzf redis-5.0.3.tar.gz
cd redis-5.0.3
#直接make 編譯
make
#可使用root用戶執行`make install`,將可執行文件拷貝到/usr/local/bin目錄下。這樣就可以直接敲名字運行程序了。
make install
直接啓動
1. 啓動
#加上`&`號使redis以後臺程序方式運行
./redis-server &
2.檢測
#檢測後臺進程是否存在
ps -ef |grep redis
#檢測6379端口是否在監聽
netstat -lntp | grep 6379
#使用`redis-cli`客戶端檢測連接是否正常
./redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set key "hello world"
OK
127.0.0.1:6379> get key
"hello world"
3.停止
#使用客戶端
redis-cli shutdown
#因爲Redis可以妥善處理SIGTERM信號,所以直接kill -9也是可以的
kill -9 PID
通過指定配置文件啓動
可爲redis服務啓動指定配置文件,配置文件 redis.conf
在Redis根目錄下。
#修改daemonize爲yes,即默認以後臺程序方式運行(還記得前面手動使用&號強制後臺運行嗎)。
daemonize no
#可修改默認監聽端口
port 6379
#修改生成默認日誌文件位置
logfile "/home/futeng/logs/redis.log"
#配置持久化文件存放位置
dir /home/futeng/data/redisData
啓動時指定配置文件
#加上`&`號使redis以後臺程序方式運行
redis-server ./redis.conf &
#如果更改了端口,使用`redis-cli`客戶端連接時,也需要指定端口,例如:
redis-cli -p 6380
使用Redis啓動腳本設置開機自啓動
推薦在生產環境中使用啓動腳本方式啓動redis服務。啓動腳本 redis_init_script
位於位於Redis的 /utils/
目錄下。
#大致瀏覽下該啓動腳本,發現redis習慣性用監聽的端口名作爲配置文件等命名,我們後面也遵循這個約定。
#redis服務器監聽的端口
REDISPORT=6379
#服務端所處位置,在make install後默認存放與`/usr/local/bin/redis-server`,如果未make install則需要修改該路徑,下同。
#EXEC=/home/local/dyxx/pay/redis-6379/src/redis-server
EXEC=/usr/local/bin/redis-server
#客戶端位置
#CLIEXEC=/home/local/dyxx/pay/redis-6379/src/redis-cli
CLIEXEC=/usr/local/bin/redis-cli
#Redis的PID文件位置
PIDFILE=/var/run/redis_${REDISPORT}.pid
#配置文件位置,需要修改(注意1)
#CONF="/home/local/dyxx/pay/redis-6379/${REDISPORT}.conf"
CONF="/etc/redis/${REDISPORT}.conf"
配置環境
1. 根據啓動腳本要求(注意1),將修改好的配置文件以端口爲名複製一份到指定目錄。需使用root用戶。
#可以在redis根目錄把redis.conf文件複製一份重命名爲6379.comf就可以的
(這操作對應的是CONF配置爲(注意1):/home/local/dyxx/pay/redis-6379)
mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf
2. 將啓動腳本複製到/etc/init.d目錄下,本例將啓動腳本命名爲redisd(通常都以d結尾表示是後臺自啓動服務)。
cp redis_init_script /etc/init.d/redisd
3. 設置爲開機自啓動
此處直接配置開啓自啓動 chkconfig redisd on
將報錯誤: service redisd does not support chkconfig
參照 http://www.cnblogs.com/goodspeed/archive/2012/10/18/2729615.html
在啓動腳本開頭添加如下兩行註釋以修改其運行級別:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
#
再設置即可成功。
#設置爲開機自啓動服務器
chkconfig redisd on
#打開服務
service redisd start
#關閉服務
service redisd stop
查詢開機啓動列表
chkconfig --list
如果看到redisd的服務,並且3,4,5都是on的話則成功,如果是off,則鍵入
chkconfig --level 345 redisd on
重啓服務器
reboot