来自localhost的Mongodb数据库备份

时间:2017-03-22 13:28:40

标签: node.js mongodb mongoose

我正在使用" mongodb-backup"用于获取数据库备份的插件。

数据库连接网址链接:

var _dbConnectionURL = 'mongodb://uname:password@LocalIpAddress:port/databasename?authdb=admin';
mongoose.connect(_dbConnectionURL);

此代码用于连接nodejs和mongodb。它运作良好。

现在我尝试从nodejs

进行备份
var backup = require('mongodb-backup');
backup({
    uri: _dbConnectionURL,
    root: './Media/MongoDB_BK',
    tar: 'sample.tar',
    callback: function (err) {
        if (err) {
            console.error(err);
        } else {
            console.log('finish');
        }
    },
});

错误:

{ MongoError: Authentication failed.
at Function.MongoError.create (F:\Hardik_Working\Eviral\MongoDB\BackEnd\Evir
al_Node_API\node_modules\mongodb-backup\node_modules\mongodb-    core\lib\error.js:3
1:11)
at F:\Hardik_Working\Eviral\MongoDB\BackEnd\Eviral_Node_API\node_modules\mon
godb-backup\node_modules\mongodb-core\lib\connection\pool.js:483:72
at authenticateStragglers (F:\Hardik_Working\Eviral\MongoDB\BackEnd\Eviral_N
ode_API\node_modules\mongodb-backup\node_modules\mongodb-    core\lib\connection\poo
l.js:429:16)
at Connection.messageHandler (F:\Hardik_Working\Eviral\MongoDB\BackEnd\Evira
l_Node_API\node_modules\mongodb-backup\node_modules\mongodb-    core\lib\connection\
pool.js:463:5)
at Socket.<anonymous> (F:\Hardik_Working\Eviral\MongoDB\BackEnd\Eviral_Node_
API\node_modules\mongodb-backup\node_modules\mongodb-    core\lib\connection\connect
ion.js:317:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:543:20)
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
code: 18,
errmsg: 'Authentication failed.' }

1 个答案:

答案 0 :(得分:1)

尝试使用authsource而不是authdb

var _dbConnectionURL = 'mongodb://uname:password@LocalIpAddress:port/databasename?authsource=admin';

或尝试在options参数

中设置authdb
backup({
    uri: _dbConnectionURL,
    root: './Media/MongoDB_BK',
    tar: 'sample.tar',
    options: {
       authSource: 'admin'    
    }
});