nodejs连接redis
redis版本5.0.6nodejs依赖包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)入门目录
作者简介
作者:小碗汤,一位热爱、认真写作的小伙,目前维护原创公众号:『我的小碗汤』,专注于写golang、docker、kubernetes等知识等提升硬实力的文章,期待你的关注。 转载说明:务必注明来源(注明:来源于公众号:我的小碗汤, 作者:小碗汤)
作者简洁
作者:小碗汤,一位热爱、认真写作的小伙,目前维护原创公众号:『我的小碗汤』,专注于写go语言、docker、kubernetes、java等开发、运维知识等提升硬实力的文章,期待你的关注。转载说明:务必注明来源(注明:来源于公众号:我的小碗汤,作者:小碗汤)