阿里云部署-3redis

7次阅读

共计 1280 个字符,预计需要花费 4 分钟才能阅读完成。

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

启动服务

后台启动 redis

cd redis-4.0.2/
src/redis-server &

查询 redis 进程

ps -ef | grep redis

可以看到 redis 已经启动了

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 客户端

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 配置文件

bind 127.0.0.1

修改为

bind 0.0.0.0 

防火墙

你需要添加安全组规则,打开服务器防火墙上的 6379 端口。

设置远程连接密码

默认配置开启了保护模式

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;

正文完
 0