我試圖連接使用貓鼬MLAB從我的系統,但它一直與下面的錯誤貓鼬連接錯誤連接到MLAB
name: 'MongoError',
message: 'failed to connect to server [server address here] on first connect' }
這失敗是我的代碼:
var mongoose = require('mongoose')
var Schema = mongoose.Schema;
var options = {
server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } }
};
mongoose.connect('mongodb://username:[email protected]:port/appname', options);
var conn = mongoose.connection
conn.on('error', console.error.bind(console, 'connection error:'));
var User = mongoose.model('User', new Schema({
name: String,
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
admin: Boolean,
location: String,
created_at: Date,
updated_at: Date
}));
var new_user = new User({
name: 'name',
username: 'username',
password: 'password'
});
conn.once('open', function() {
console.log('started')
new_user.save(function(err) {
if (err) throw err;
console.log('User created!');
});
});
請讓我知道我如何成功連接到mLAB。
你試過用mongo客戶端連接嗎?也許這是一個網絡問題? –
當我從mongo客戶端嘗試時,我得到這個錯誤0061因爲目標機器主動拒絕它,所以沒有連接。 – v1shnu
嘗試創建一個新用戶和密碼以防萬一,並確保用戶沒有讀取只能訪問並重試。似乎是一個授權問題,您提供的信息 – CodePhobia