在centos7中安装redis,并通过node.js操作redis

1、cent OS7 下使用redis

  • 关闭防火墙
	systemctl stop firewalld.service #停止firewall
	systemctl disable firewalld.service #禁止firewall开机启动
    firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
复制代码
  • 配置编译环境:
    sudo yum install gcc-c++
复制代码
  • 下载源码:
    wget http://download.redis.io/releases/redis-4.0.11.tar.gz
复制代码
  • 解压源码:
tar -zxvf redis-4.0.11.tar.gz
复制代码
  • 进入到解压目录:
cd redis-4.0.11
复制代码
  • 进入到解压目录: 执行make编译Redis:
make MALLOC=libc
复制代码

注意:

make命令执行完成编译后,会在src目录下生成6个可执行文件,分别是

  1. redis-server、
  2. redis-cli、
  3. redis-benchmark、
  4. redis-check-aof、
  5. redis-check-rdb、
  6. redis-sentinel
  • 安装Redis:
make install 
复制代码
  • 配置Redis能随系统启动:
./utils/install_server.sh
复制代码

显示结果信息如下:

Welcome to the redis service installer
This script will help you easily set up a running redis server
复制代码

redis 配置

  • 关闭保护模式
    config set protected-mode no
复制代码
  • 设置密码
    // 获取密码
    config get requirepass
    
    // 设置密码 
    config set requirepass yourpassword
复制代码

2、nodejs中操作redis

安装redis

npm install redis --save
复制代码
//引入redis
var redis = require('redis')
// 连接redis服务器
// 连接redis数据库,createClient(port,host,options);
// 如果REDIS在本机,端口又是默认,直接写createClient()即可
client = redis.createClient(6379, '192.168.73.128', {
    password: 'lentoo'
});

//错误监听?
client.on("error", function (err) {
    console.log(err);
});
复制代码

2.1常用API

  • redis.print

通过redis回显

  • set 像redis中存入一个键值对
client.set('key','value')
// 设置过期时间 10s后过期
client.set('key','value','EX',10)
复制代码
  • get 获取在redis中存入的值
client.get('key') // value
复制代码
  • hset 通过hash key 存值
client.hset('hash key','key','value', redis.print)
复制代码
  • hget 通过hash key 获取值
client.hget('hash key','key', redis.print)
复制代码
  • hkeys 所有的"hash key"
// 遍历哈希表"hash key"
client.hkeys("hash key", function (err, replies) {
    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
    client.quit();
});
复制代码
  • hmset
client.hmset('hash 1', 'key', 'value111', 'key2', 'value222', 'key3', 'value3', redis.print)
复制代码
  • hmget
client.hmget('hash 1', 'key', 'key2', 'key3', redis.print)
复制代码
  • publish/subscribe 发布/订阅
const sub = redis.createClient() // 订阅者
const pub = redis.createClient() // 发布者
var msg_count = 0;

sub.on("subscribe", function (channel, count) {
    client.publish("a nice channel", "I am sending a message.");
    client.publish("a nice channel", "I am sending a second message.");
    client.publish("a nice channel", "I am sending my last message.");
});

sub.on("message", function (channel, message) {
    console.log("sub channel " + channel + ": " + message);
    msg_count += 1;
    if (msg_count === 3) {
        sub.unsubscribe();
        sub.quit();
        client.quit();
    }
});

复制代码
  • ready

redis客户端连接准备好后触发,在此前所有发送给redis服务器的命令会以队列的形式进行排队,会在ready事件触发后发送给redis服务器

client.on('ready',function(){
    console.log('ready');
})
复制代码
  • connct 客户端在连接到服务器后触发
client.on('connect',function(){
    console.log('connect');
})
复制代码
  • reconnecting 客户端在连接断开后重新连接服务器时触发
client.on('reconnecting ', function (resc) {
    console.log('reconnecting',resc);
})
复制代码
  • error 错误监听

client.on("error", function (err) { console.log(err); });

  • end 连接断开时触发

client.on('end',function(){ console.log('end')
})

  • createClient
redis.createClient([options])
redis.createClient(unix_socket[, options])
redis.createClient(redis_url[, options])
redis.createClient(port[, host][, options])
复制代码
options object properties
属性默认值描述
host 127.0.0.1redis服务器地址
port6379端口号
connect_timeout3600000连接超时时间 以ms为单位
passwordnull密码

公众号

欢迎关注我的公众号“码上开发”,每天分享最新技术资讯。关注获取最新资源

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值