Alibaba cloud deployment 3.redis

redis

Download and compile

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

Startup service

Start redis in the background

cd redis-4.0.2/
src/redis-server &

Query redis process

ps -ef | grep redis

You can see that redis has been started.

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

End process

kill -9 pid

Preliminary test

Start redis client

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 installation succeeded.

Configure server remote connections

The default configuration can only be local access. We modify the redis-4.0.2/redis.conf configuration file.
take

bind 127.0.0.1

Modified to

bind 0.0.0.0 

firewall

You need to add security group rules and open port 6379 on the server firewall.

Set remote connection password

Protection mode is on by default

protected-mode yes

At this time, you need to set a password to connect to redis remotely. The password setting is very simple. Just fill in your password in the required pass field.

requirepass your password

After configuration, start your redis in the background.

./redis-server /etc/redis/redis.conf

node client connection

I use the redis package on npm. At this time, you can connect to redis on the server remotely according to the previous configuration. Combination Developing documents , you can actually develop it.

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;

Keywords: Javascript Redis firewall npm

Added by rocco2004 on Wed, 23 Oct 2019 20:00:19 +0300