求救我 我弄了一下午了 nodejs 链接 mongodb问题一直链接不上
发布于 1年前 作者 zhouaini528 861 次浏览

根据官方教程 有几种链接方式 但是就是不能访问 因为我设置了访问需要验证

但是我图形界面 是链接成功了的 验证 mongodb://root:123456@192.168.1.227:27017

nodejs 链接mongo 能链接成功 但是要访问我的 comedy 数据库中的 aa集合 却不能访问 错误提示是没有授权 我就知道是密码的问题了

根据nodejs mongodb API 我去设置密码 但是看文档没有找到设置的密码
如下2中方式 new Server("localhost", 27017) 只接受2个参数localhost 改为 mongodb://root:123456@192.168.1.227不行 我招了N久文档了 不知道那里设置密码

第一种 // Set up the connection to the local db var mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true});

      // Open the connection to the server
      mongoclient.open(function(err, mongoclient) {
    
        // Get the first db and do an update document on it
        var db = mongoclient.db("integration_tests");
        db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
          assert.equal(null, err);
          assert.equal(1, result);
    
          // Get another db and do an update document on it
          var db2 = mongoclient.db("integration_tests2");
          db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
            assert.equal(null, err);
            assert.equal(1, result);
    
            // Close the connection
            mongoclient.close();
          });
        });
      });

第二种 var db = new Db('test’, new Server('locahost’, 27017)); // Establish connection to db db.open(function(err, db) { assert.equal(null, err);

      db.on('close', test.done.bind(test));
      db.close();
    });
3 回复

我php远程连接数据库都成功了 就TMD nodejs不行 我操

php的链接 设置是
$m = new Mongo(‘mongodb://lwj:[email protected]:27017’);

那里有nodejs 的设置 我勒个去

{ [MongoError: not authorized for query on comedy.aa] name: ‘MongoError’ }这就是错误 没权限啊

回到顶部