Node工程使用云服务器中的redis镜像做数据库
Redis镜像安装
在云服务器中执行指令
1 | docker pull redis |
添加redis镜像实例的配置
1 2 3 4 5 6 7 | [root@VM-0-11-centos ~] # cd /home [root@VM-0-11-centos home] # ls mongotest [root@VM-0-11-centos home] # mkdir redistest [root@VM-0-11-centos home] # cd redistest/ [root@VM-0-11-centos redistest] # vi docker-compose.yml [root@VM-0-11-centos redistest] # docker-compose up -d |
使用docker-compose管家工具,编写docker-compose.yml文件进行启动配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | version: "3" services: redis- test : image: "redis" restart: always container_name: "redis-test" ports: # 宿主端口:容器内端口 - 15001:6379 volumes: #建立文件备份 宿主目录:容器目录 # redis的备份不是说一有数据就会备份的,它会按照一定的策略,自己定时持久话 - /home/redistest : /data #远程登录redis时,需要输入密码,保证redis的安全 command : [ "redis-server" , "--requirepass" , "123456" ] |
上面的配置和下面的命令是等价的
1 | docker run -itd --restart=always --name redis- test -p 15001:6379 - v /home/redistest : /data redis redis-server --requirepass 123456 |
通过下面的命令,可以查看镜像启动时的日志
1 | [root@VM-0-11-centos ~] # docker logs -f redis-test |
更新镜像配置
1 | docker-compose up -d |
Redis的命令行工具redis-cli
Redis常见的CLI命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // 切换到容器中的shell终端 [root@VM-0-11-centos ~] # docker exec -it redis-test /bin/bash root@fe27aff96dc9: /data # ls docker-compose.yml dump.rdb // 运行redis-cli命令行工具 root@fe27aff96dc9: /data # redis-cli // 输入redis设置的安全密码 127.0.0.1:6379> auth 123456 OK // 判断redis服务是否在运行 127.0.0.1:6379> ping PONG // 断开数据库 127.0.0.1:6379> quit // 退出容器 root@fe27aff96dc9: /data # exit exit |
redis中默认有16个“database”它们其的作用是隔离的作用,它们的区分是通过index来做
分别是1,2,3..号“数据库”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 27.0.0.1:6379> select 0 OK 127.0.0.1:6379> set name jack OK 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> set name lucy OK 127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> get name "jack" 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> get name "lucy" 127.0.0.1:6379[1]> |
基本操作//keys 后面接正则表达式,用于查询符合条件的所有key127.0.0.1:6379> keys *
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1) "test" 2) "name" 3) "test1" 4) "name1" 127.0.0.1:6379> keys test * 1) "test" 2) "test1" //exists 查询是否存在这个key 127.0.0.1:6379> exists test1 (integer) 1 //del 删除这个key 127.0.0.1:6379> del test1 (integer) 1 127.0.0.1:6379> exists test1 (integer) 0 127.0.0.1:6379> set num 1 OK //incr 递增 127.0.0.1:6379> incr num (integer) 2 //decr 递减 127.0.0.1:6379> decr num (integer) 1 |
hash表//hset 集合名称 key value, key value,
1 2 3 4 5 6 7 8 9 10 11 12 | 127.0.0.1:6379> hset city name hangzhou people 3000 (integer) 2 127.0.0.1:6379> hset city (error) ERR wrong number of arguments for 'hset' command 127.0.0.1:6379> hgetall city 1) "name" 2) "hangzhou" 3) "people" 4) "3000" 127.0.0.1:6379> hget city people "3000" 127.0.0.1:6379> |
新开一个终端,直接进入到redis数据库,并打开redis-cli工具
然后开启一个订阅
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@VM-0-11-centos ~] # docker exec -it redis-test redis-cli -h 127.0.0.1 -a 123456 127.0.0.1:6379> subscribe name name1 Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "name" 3) (integer) 1 1) "subscribe" 2) "name1" 3) (integer) 2 1) "message" 2) "name" 3) "hello a jack" |
从另一个端口发布一个通知
1 2 3 | 127.0.0.1:6379> publish name "hello a jack" (integer) 1 127.0.0.1:6379> |
服务器相关
flushdb, flushall 清空数据库127.0.0.1:6379[1]> flushdb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | OK 127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> keys * 1) "city" 2) "test" 3) "name" 4) "num" 5) "name1" 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> flushall OK 127.0.0.1:6379[1]> select 0 OK 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> |
Node工程建立Redis链接
首先安装redis第三方库
1 | npm i -S redis |
创建工具类RedisHelper.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | const redis = require( "redis" ); // // 存在用户名:密码时 // // format redis[s]: // [[username][:password]@][host][:port][ /db-number ]: // const client = redis.createClient({ // url: "redis://root:0059ssxxSS11@1.xxx.xxx.159:6379" , // }); // // // 不存在用户名时,password为鉴权密码 // const client = redis.createClient({ // url: "redis://:password1@192.168.0.1:6379" , // }); // 链接配置 const options = { url: 'redis://:123456@1.15.55.28:15001' }; const client = redis.createClient(options) // 监听错误事件 client.on( "error" , (err) => { console.log( "redis error" , err); }); (async ()=>{ // 建立链接 await client.connect(); // 写数据 await client. set ( "city" , 'hangzhou' +Date.now()) // 读数据 const value = await client.get( "city" ) console.log( "value: " +value) // 关闭链接 await client.quit() // 关闭连接 quit可以确保在连接销毁之前,挂起的命令被发送到redis // await client.disconnecting() // 强制关闭连接 挂起的命令可能没有被发送到redis })() |
执行
1 | node RedisHelper.js |
使用bluebird对redis的client的所有异步方法包裹一层Promise,方便开发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | const redis = require( "redis" ); const bluebird = require( 'bluebird' ) const {config} = require( './Config' ) // 链接配置 const options = { url: 'redis://:123456@' +config.URL+ ":" +config.Port }; // 使用bluebird将redis中的所有异步方法包括一层Asnyc const client = bluebird.promisifyAll(redis.createClient(options)) // 监听错误事件 client.on( "error" , (err) => { console.log( "redis error" , err); }); (async ()=>{ // 建立链接 await client.connect(); // 写数据 await client. set ( "city" , 'hangzhou' +Date.now()) // 读数据 const value = await client.get( "city" ) console.log( "value: " +value) // 关闭链接 // await client.quit() // 关闭连接 quit可以确保在连接销毁之前,挂起的命令被发送到redis // await client.disconnecting() // 强制关闭连接 挂起的命令可能没有被发送到redis })() // 添加导出方法 const getAsync = (key) => { return new Promise((resolve, reject) => { client.get(key) . then ((res) => { resolve(res); }) .catch((err)=>{ reject(err); }); }); } // const getValue = (key) => { // return getAsync(key) // } const getValue = (key) => { //bluebird 内部提供了getAsync方法,它是把get方法封了一层Promise return client.getAsync(key) } exports.getValue = getValue; |
Redis图形GUI工具
AnotherRedisDesktopManager
1 | https: //github .com /qishibo/AnotherRedisDesktopManager |
填写主机IP,授权密码,链接名点击OK进行登录
其中的redis-demo项目
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了