阿里云部署 3.redis

redis

下载和编译

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
复制代码

node客户端链接

我用的是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;
复制代码

相关文章
相关标签/搜索