nodejs connection redis
redis version 5.0.6
nodejs dependencies redis version 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 connection mongoDB
nodejs dependencies mongoose version 4.6.0; mongoomise version 0.0.8; bluebird version 3.4.6;
nodejs v12.13.0
mongoDB version 4.2.1
Run the following command to create a user mongoDB:
db.createUser({ user:'admin',pwd:'test123',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}]});
nodejs connection code as follows:
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:[email protected]: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
Error when connecting:
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! [email protected] start: `babel-node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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
Because the mall is connected with the mall db user, you need to execute the following command in mongoDB, the new mall users have read-write access to this mall db, passwordtest123
use mall;
db.createUser({ user:'mall',pwd:'test123',roles:[ { role:'readWrite', db: 'mall'}]});
Restart the modified project, the connection is successful.
reference
Nodejs study notes (nine) - and Redis interaction (mranney / node_redis) directory entry
About the Author
Author: a small bowl of soup, a love, a serious guy writing, currently maintaining the original number public: "My little bowl of soup," focus on writing golang, docker, kubernetes and other knowledge to enhance the hard power of articles, look forward to your attention . Reprinted Note: Be sure to indicate the source (note: from public number: My little bowl of soup, author: small bowl of soup)