nodejs连接有密码的redis、连接有密码的mongoDB

nodejs连接redis

redis版本5.0.6

nodejs依赖包redis版本2.6.2

import redis from 'redis'

const redisLink = 'redis://127.0.0.1:6379'
const pwd = '289181'
const opts = {
	auth_pass: pwd,
}
const redisClient = redis.createClient(redisLink, opts)

redisClient
	.on('error', err => console.log('------ Redis connection failed ------' + err))
	.on('connect', () => console.log('------ Redis connection succeed ------'))

export default {
	redis: redis, 
	redisClient: redisClient, 
}

nodejs连接mongoDB

nodejs依赖包mongoose版本4.6.0 ; mongoomise版本0.0.8;bluebird版本3.4.6;
nodejs v12.13.0
mongoDB 版本4.2.1

在mongoDB中执行以下命令创建用户:

db.createUser({ user:'admin',pwd:'test123',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}]});

 nodejs连接代码如下:

import bluebird from 'bluebird'
import mongoose from 'mongoose'
import mongoomise from 'mongoomise'

class Mongo{
	constructor(app, config) {
		Object.assign(this, {
			app, 
			config, 
		})

		this.init()
	}

	init() {
		const opts = {
			useMongoClient: true,
			server: {
				socketOptions: { 
					keepAlive: 1 
				}
			}
		}

		mongoose
			.connect('mongodb://mall:test123@127.0.0.1:27017/mall', opts)
			.connection
			.on('error', err => console.log('------ Mongodb connection failed ------' + err))
			.on('open', () => console.log('------ Mongodb connection succeed ------'))

		mongoose.Promise = global.Promise
			
		mongoomise.promisifyAll(mongoose, bluebird)
	}
}

export default Mongo

连接时报错:

D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb\lib\utils.js:98                                                         
    process.nextTick(function() { throw err; });                                                                                                                        
                                  ^                                                                                                                                     
Error [MongoError]: Authentication failed.                                                                                                                              
    at Function.MongoError.create (D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\error.js:31:11)             
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\connection\pool.js:462:72                               
    at authenticateStragglers (D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\connection\pool.js:410:16)      
    at Connection.messageHandler (D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\connection\pool.js:444:5)    
    at Socket.<anonymous> (D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\connection\connection.js:306:22)    
    at Socket.emit (events.js:210:5)                                                                                                                                    
    at Socket.EventEmitter.emit (domain.js:476:20)                                                                                                                      
    at addChunk (_stream_readable.js:308:12)                                                                                                                            
    at readableAddChunk (_stream_readable.js:289:11)                                                                                                                    
    at Socket.Readable.push (_stream_readable.js:223:10)                                                                                                                
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23)                                                                                                        
Emitted 'error' event on NativeConnection instance at:                                                                                                                  
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongoose\lib\connection.js:288:17                                        
    at NativeConnection.Connection.error (D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongoose\lib\connection.js:451:12)    
    at open (D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongoose\lib\connection.js:503:13)                                 
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongoose\lib\connection.js:524:7                                         
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb\lib\db.js:1545:20                                                
    at handleCallback (D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb\lib\utils.js:96:12)                              
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb\lib\db.js:1504:22                                                
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\connection\pool.js:701:7                                
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\connection\pool.js:677:20                               
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\auth\scram.js:190:18                                    
    at D:\SoftwareAndProgram\program\weixin\miniprogram\mini-mall\mini-mall-admin\node_modules\mongodb-core\lib\connection\pool.js:436:18                               
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {                                                                                              
  name: 'MongoError',                                                                                                                                                   
  message: 'Authentication failed.',                                                                                                                                    
  ok: 0,                                                                                                                                                                
  errmsg: 'Authentication failed.',                                                                                                                                     
  code: 18,                                                                                                                                                             
  codeName: 'AuthenticationFailed'                                                                                                                                      
}                                                                                                                                                                       
npm ERR! code ELIFECYCLE                                                                                                                                                
npm ERR! errno 1                                                                                                                                                        
npm ERR! m-mall-admin@1.0.0 start: `babel-node ./bin/www`                                                                                                               
npm ERR! Exit status 1                                                                                                                                                  
npm ERR!                                                                                                                                                                
npm ERR! Failed at the m-mall-admin@1.0.0 start script.                                                                                                                 
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.                                                                      
                                                                                                                                                                        
npm ERR! A complete log of this run can be found in:                                                                                                                    
npm ERR!     C:\Users\lenovo\AppData\Roaming\npm-cache\_logs\2019-11-29T16_19_51_799Z-debug.log                                   
因为用mall用户连接mall这个db,所以需要在mongoDB中执行以下命令,新建mall用户,对mall这个db有读写权限,密码为test123

use mall;
db.createUser({ user:'mall',pwd:'test123',roles:[ { role:'readWrite', db: 'mall'}]});

修改后重新启动项目,连接成功。

参考

Nodejs学习笔记(九)— 与Redis的交互(mranney/node_redis)入门目录

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker是一种容器化解决方案,Docker Compose可以简化多个容器的管理和部署流程。Nacos是一个用于服务发现、配置管理和动态DNS服务的开源平台。Node.js是一个基于JavaScript的开源、跨平台的运行时环境,用于构建可扩展的网络应用程序。MongoDB是一个高性能、文档型NoSQL数据库,适用于处理大量的结构化和非结构化数据。MySQL是一个开源的关系型数据库管理系统,用于存储和管理结构化数据。Redis是一个基于内存的高性能键值存储系统,用于缓存和数据持久化。Seata是一个开源的分布式事务解决方案,用于保证分布式系统中的数据一致性。 通过Docker Compose,我们可以轻松地将这些不同的组件和服务以容器化的方式部署在一台或多台服务器上。我们可以使用Docker Compose的配置文件定义每个服务的镜像、端口映射、环境变量等设置。在这个场景中,我们可以将Nacos、Node.js、MongoDB、MySQL、Redis和Seata分别作为独立的服务进行定义。 使用Docker Compose可以简化部署过程,只需运行一个命令即可启动整个应用程序的容器群组。Docker会自动拉取和部署所需的镜像,启动容器,并通过网络连接各个服务。Nacos可以作为服务发现和配置中心,用于管理和注册各个服务的地址和配置信息。Node.js可以作为应用程序的后端逻辑进行开发,通过Nacos来发现和调用各个后端服务。MongoDB作为主要的数据存储,MySQL和Redis可以作为辅助数据存储和缓存。Seata可以用于管理和控制分布式事务,确保数据一致性。 总之,使用Docker Compose可以方便地将Nacos、Node.js、MongoDB、MySQL、Redis和Seata等组件集成在一起,并通过容器化的方式进行部署。这样做可以极大地简化应用程序的开发和部署过程,并提供高度可扩展的架构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值