wget http://download.redis.io/releases/redis-4.0.2.tar.gz tar xzf redis-4.0.2.tar.gz cd redis-4.0.2 make
後臺啓動redisnode
cd redis-4.0.2/ src/redis-server &
查詢redis進程redis
ps -ef | grep redis
能夠看到redis已經啓動了npm
root 19141 19065 0 12:50 pts/1 00:00:03 ./src/redis-server 0.0.0.0:6379 root 19238 19196 0 14:00 pts/0 00:00:00 grep --color=auto redis
結束進程安全
kill -9 pid
啓動redis客戶端bash
cd redis-4.0.2/ src/redis-cli 127.0.0.1:6379> set test 1 OK 127.0.0.1:6379> get test "1"
redis安裝成功了。服務器
默認配置只能是本地訪問,咱們修改redis-4.0.2/redis.conf配置文件
將app
bind 127.0.0.1
修改成測試
bind 0.0.0.0
你須要添加安全組規則,打開服務器防火牆上的6379端口。ui
默認配置開啓了保護模式this
protected-mode yes
這時你須要設置密碼才能夠遠程鏈接上redis,密碼設置很是簡單,只須要在requirepass字段上填寫你的密碼便可
requirepass 你的密碼
配置完畢,後臺啓動你的redis能夠了。
./redis-server /etc/redis/redis.conf
我用的是npm上的redis包,此時根據前面你的配置就能夠遠程鏈接服務器上的redis了。結合開發文檔,就能夠進行實際開發了。
const redis = require('redis'); const config = require('../config'); const logger = require('log4js').getLogger('app'); class RedisClient { constructor() { if (!RedisClient.instance) { this.client = redis.createClient({ host: config.redis.host, port: config.redis.port, password: config.redis.password, }); const client = this.client; RedisClient.instance = client; client.on("error", (err) => { logger.error('redis connect err: %s', err.toString()); }); client.on("connect", () => { logger.info('redis connect success'); }); } } } module.exports = new RedisClient().client;