引言

最近在学习node.js 连接redis的模块,所以尝试了一下在虚拟机中安装cent OS7,并安装redis,并使用node.js 操作redis。所以顺便做个笔记。

如有不对的地方,欢迎大家指正!

1、cent OS7 下使用redis

1.1、配置编译环境:

sudo yum install gcc-c++

1.2、下载源码:

wget http://download.redis.io/releases/redis-4.0.11.tar.gz

1.3、解压源码:

tar -zxvf redis-4.0..tar.gz

1.4、进入到解压目录:

cd redis-4.0.

1.5、进入到解压目录: 执行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

1.6、安装Redis:

make install 

1.7、配置Redis能随系统启动:

./utils/install_server.sh

1.8、关闭防火墙

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

2、redis配置

  如果redis客户端和服务器在同一台机子中,可以不配置,如果redis服务器在虚拟机中,客户端在本地系统中,则需要进行配置,否则可能会连接失败

  • 关闭保护模式

  如果不关闭,通过node.js连接时,会连接失败

 config set protected-mode no
  • 设置密码
// 获取密码
config get requirepass // 设置密码
config set requirepass yourpassword

2.1 、redis.conf 的配置信息

  redis.conf 是redis的的配置文件,修改配置文件后,需要重启redis才会生效

1、daemonize 如果需要在后台运行,把该项改为yes

2、pidfile 配置多个pid的地址 默认在/var/run/redis.pid

3、bind 绑定ip,设置后只接受来自该ip的请求

4、port 监听端口,默认是6379

5、loglevel 分为4个等级:debug verbose notice warning

6、logfile 用于配置log文件地址

7、databases 设置数据库个数,默认使用的数据库为0

8、save 设置redis进行数据库镜像的频率。

9、rdbcompression 在进行镜像备份时,是否进行压缩

10、dbfilename 镜像备份文件的文件名

11、Dir 数据库镜像备份的文件放置路径

12、Slaveof 设置数据库为其他数据库的从数据库

13、Masterauth 主数据库连接需要的密码验证

14、Requriepass 设置 登陆时需要使用密码

15、Maxclients 限制同时使用的客户数量

16、Maxmemory 设置redis能够使用的最大内存

17、Appendonly 开启append only模式

18、Appendfsync 设置对appendonly.aof文件同步的频率(对数据进行备份的第二种方式)

19、vm-enabled 是否开启虚拟内存支持 (vm开头的参数都是配置虚拟内存的)

20、vm-swap-file 设置虚拟内存的交换文件路径

21、vm-max-memory 设置redis使用的最大物理内存大小

22、vm-page-size 设置虚拟内存的页大小

23、vm-pages 设置交换文件的总的page数量

24、vm-max-threads 设置VM IO同时使用的线程数量

25、Glueoutputbuf 把小的输出缓存存放在一起

26、hash-max-zipmap-entries 设置hash的临界值

27、Activerehashing 重新hash

  

3、nodejs中操作redis

3.1、  安装redis

// github https://github.com/NodeRedis/node_redis
npm install redis --save

3.2、  简单使用

//引入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);
});
client.set('key','value')
client.get('key')

3.3、常用API

  • redis.print

    通过redis的打印api回显

  • 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

    可同时设置多个key,value

client.hmset('hash 1', 'key', 'value111', 'key2', 'value222', 'key3', 'value3', redis.print)
  • hmget

    可同时获取多个key

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])

3.4 options 对象属性

属性 默认值 描述
host  127.0.0.1 redis服务器地址
port 6379 端口号
connect_timeout 3600000 连接超时时间 以ms为单位
password null 密码

在centos7中安装redis,并通过node.js操作redis的更多相关文章

  1. centos7中安装、配置、验证、卸载redis

    本文介绍在centos7中安装.配置.验证.卸载redis等操作,以及在使用redis中的一些注意事项. 一 安装redis 1 创建redis的安装目录 利用以下命令,切换到/usr/local路径 ...

  2. [Node.js]操作redis

    摘要 在实际开发中,免不了要操作mysql,mongodb,redis等数据存储服务器.这里先简单介绍如何操作redis. 一个例子 关于redis服务端的安装这里不再介绍,重点不在这里.感兴趣的可以 ...

  3. [Node.js]31. Level 7: Redis coming for Node.js, Simple Redis Commands

    Let's start practicing using the redis key-value store from our node application. First require the  ...

  4. node js 操作redis promise

    连接 redis = require('redis') var client = redis.createClient('6379', '127.0.0.1'); client.on('connect ...

  5. [Node.js]操作mysql

    摘要 上篇文章介绍了node.js操作redis的简单实例,这里介绍如何操作mysql. 安装 安装mysql模块 cnpm install mysql 一个例子 新建一个mysql.js的文件,代码 ...

  6. 在centos7中安装Robot Framework

    安装前景介绍: 最初,我们是在Windows环境下搭建Robot Framework来对我们的服务进行接口测试的(想知道如何在Windows下安装Robot Framework,可以参考我同事的博客h ...

  7. centos7中安装mongodb3.6

    centos7中安装mongodb3.6 首先更新系统 yum -y update 1.安装Mongodb 编辑Mongodb安装源 vim /etc/yum.repos.d/mongodb-org- ...

  8. 在Centos7中安装elasticsearch5.5

    在Centos7中安装elasticsearch5.5 第一步:必须要有jre支持 elasticsearch是用Java实现的,跑elasticsearch必须要有jre支持,所以必须先安装jre ...

  9. centos7中安装mysql

    centos7中安装mysql网上已经很多资源了,我就不在赘述了.我这里只是记录下我安装的时候出现的一些问题. 原文:https://www.cnblogs.com/bigbrotherer/p/72 ...

随机推荐

  1. poj1182 食物链 带权并查集

    题目传送门 题目大意:大家都懂. 思路: 今天给实验室的学弟学妹们讲的带权并查集,本来不想细讲的,但是被学弟学妹们的态度感动了,所以写了一下这个博客,思想在今天白天已经讲过了,所以直接上代码. 首先, ...

  2. Notepad++ 代码格式化插件

    UniversalIndentGUI 是一个代码格式化工具合集,基于很多开源的代码格式化项目.有NPP的插件版也有独立的程序,支持常见代码格式. 支持的代码格式: C, C++, C#, Cobol, ...

  3. 什么是RFID? 射频识别技术的特点及工作原理!

    RFID即Radio Frequency Identifcation,就是射频识别技术,这篇给大家讲述的就是这个射频识别技术.这里就涉及到射频,电磁学等等知识.看完这篇,你应该会对这些知识有些了解,大 ...

  4. How to download Heavy Duty Diagnostic Caterpillar SIS 2018 software

    Maybe you find there are supplied Caterpillar SIS 2018 software free download in search engine, that ...

  5. 采用MQTT协议实现android消息推送(4)选fusesource-mqtt-client为客户端

    1.简介 一个java写的mqtt客户端.项目地址: https://github.com/fusesource/mqtt-client 2.引入fusesource-mqtt-client库 Fil ...

  6. Hash算法总结

    1. Hash是什么,它的作用 先举个例子.我们每个活在世上的人,为了能够参与各种社会活动,都需要一个用于识别自己的标志.也许你觉得名字或是身份证就足以代表你这个人,但是这种代表性非常脆弱,因为重名的 ...

  7. Ignite cahce 存储object类型数据和object类型数据序列化后string存储区别

    Ignite cache在存储时 object类型的数据和 序列化该object成string类型 两者存储时间差不多. 但是这两者在读取出来的时候,string类型比object类型快很多. 以下为 ...

  8. TeamCity 持续集成工具

    https://www.jetbrains.com/teamcity/ null

  9. mysql DCl语句

    DCl 语句主要书DBA用来管理系统中的对象权限使用 grant select,insert on sakila.* 'kingle'@'localhost' identified by '123'; ...

  10. pip install xxx -i https://pypi.tuna.tsinghua.edu.cn/simple

    pip install xxx -i https://pypi.tuna.tsinghua.edu.cn/simple 快速下载