引言
最近在学习 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.11.tar.gz
1.4, 进入到解压目录:
cd redis-4.0.11
1.5, 进入到解压目录: 执行 make 编译 Redis:
make MALLOC=libc
make 命令执行完成编译后, 会在 src 目录下生成 6 个可执行文件, 分别是
- redis-server,
- redis-cli,
- redis-benchmark,
- redis-check-aof,
- redis-check-rdb,
- 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 才会生效
,daemonize 如果需要在后台运行, 把该项改为 yes
,pidfile 配置多个 pid 的地址 默认在 / var/run/redis.pid
,bind 绑定 ip, 设置后只接受来自该 ip 的请求
,port 监听端口, 默认是 6379
,loglevel 分为 4 个等级: debug verbose notice warning
,logfile 用于配置 log 文件地址
,databases 设置数据库个数, 默认使用的数据库为 0
,save 设置 redis 进行数据库镜像的频率.
,rdbcompression 在进行镜像备份时, 是否进行压缩
,dbfilename 镜像备份文件的文件名
,Dir 数据库镜像备份的文件放置路径
,Slaveof 设置数据库为其他数据库的从数据库
,Masterauth 主数据库连接需要的密码验证
,Requriepass 设置 登陆时需要使用密码
,Maxclients 限制同时使用的客户数量
,Maxmemory 设置 redis 能够使用的最大内存
,Appendonly 开启 append only 模式
,Appendfsync 设置对 appendonly.aof 文件同步的频率 (对数据进行备份的第二种方式)
,vm-enabled 是否开启虚拟内存支持 (vm 开头的参数都是配置虚拟内存的)
,vm-swap-file 设置虚拟内存的交换文件路径
,vm-max-memory 设置 redis 使用的最大物理内存大小
,vm-page-size 设置虚拟内存的页大小
,vm-pages 设置交换文件的总的 page 数量
,vm-max-threads 设置 VM IO 同时使用的线程数量
,Glueoutputbuf 把小的输出缓存存放在一起
,hash-max-zipmap-entries 设置 hash 的临界值
,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 | 连接超时时间 & nbsp; 以 ms 为单位 |
password | null | 密码 |
来源: http://www.bubuko.com/infodetail-2719006.html