edit-icon download-icon

Connection issues with Mongo shell

Last Updated: Nov 13, 2017

You can allow ECS instances to connect to your ApsaraDB for MongoDB instance by using Data Management or Mongo shell. This article takes the scenarios using Mongo shell as examples.

A message indicating network time-out is displayed when the instance is connected

  1. # /u01/mongodb_current/bin/mongo --host dds-uf69ba5cf6e123442.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u root -p xxx
  2. MongoDB shell version: 3.2.3
  3. DB Prefix:
  4. connecting to: 10.1.2.8:3717/admin
  5. 2016-05-31T15:25:58.940+0800 W NETWORK Failed to connect to 10.1.2.8:3717 after 5000 milliseconds, giving up.
  6. 2016-05-31T15:25:58.943+0800 E QUERY Error: couldn't connect to server 10.1.2.8:3717 (10.1.2.8), connection attempt failed
  7. at connect (src/mongo/shell/mongo.js:181:14)
  8. at (connect):1:6 at src/mongo/shell/mongo.js:181
  9. exception: connect failed

Use Telnet (for example, telnet dds-uf69ba5cf6e123442.mongodb.rds.aliyuncs.com 3717) to check whether network time-out is caused by network disconnection.

Possible causes of network disconnection:

  • ECS and the MongoDB node are not in the same zone.
  • ECS and the MongoDB node are not in the same VPC environment.

A message indicating authentication failure is displayed when the instance is connected

  1. $mongo --host dds-uf69ba5cf6e123442.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u root -p xxx
  2. MongoDB shell version: 3.2.3
  3. connecting to: 10.1.2.8:3717/test
  4. 2016-05-31T15:50:18.623+0800 E QUERY Error: 18 Authentication failed.
  5. at DB._authOrThrow (src/mongo/shell/db.js:1271:32)
  6. at (auth):6:8
  7. at (auth):7:2 at src/mongo/shell/db.js:1271
  8. exception: login failed

Possible causes:

  • The password is incorrect. You can reset the root password on the console.
  • The connected user does not match the database. For example, the root user is a user under the admin database. When you use the root user for connection, the authentication database must be admin.
  • The version of the client is too early. The version of Mongo shell must be 3.0 or later. For version requirements for clients in other languages, see Driver compatibility document.

The slaveOk=false error message is displayed when the secondary node is connected for command execution

  1. $mongo --host dds-uf69ba5cf6e123442.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u root -p xxx
  2. MongoDB shell version: 3.2.3
  3. connecting to: 10.1.2.8:3717/test
  4. mongo-9551:SECONDARY> show dbs
  5. 2016-05-31T15:54:13.527+0800 E QUERY Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }
  6. at Error (<anonymous>)
  7. at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
  8. at shellHelper.show (src/mongo/shell/utils.js:630:33)
  9. at shellHelper (src/mongo/shell/utils.js:524:36)
  10. at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47

By default, all read and write requests of MongoDB are destined for the primary node, and the secondary node does not provide the reading service unless otherwise specified by the client explicitly (by setting readPreference to grant the read permission on a node).

When using Mongo shell to connect to the instance, you can run rs.slaveOk() to grant the read permission on the secondary node.

  1. $mongo --host dds-uf69ba5cf6e123442.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u system -p xxx
  2. MongoDB shell version: 3.2.3
  3. connecting to:10.1.2.8:3717/test
  4. mongo-9551:SECONDARY> rs.slaveOk()
  5. mongo-9551:SECONDARY> show dbs
  6. admin 0.000GB
  7. hello 0.000GB
  8. local 0.000GB
  9. test 0.000GB

To maintain Mongo shell connection to the primary node, run the following command:

  1. mongo --host replica set name/node 1, node 2 --authenticationDatabase admin -u system -p xxx
  2. Example
  3. mongo --host mg-100101/dds-uf69ba5cf6e123442.mongodb.rds.aliyuncs.com:3717,dds-uf69ba5cf6e123441.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u system -p xxx

Note: The primary and secondary nodes of MongoDB are unfixed during operation. Instance restart, instance upgrade, and node failure all may cause switchover between the primary and secondary nodes. Do not connect to the primary node directly in the production environment. For the correct method of connecting to the replica set, see How to implement read/write splitting for MongoDB.

Thank you! We've received your feedback.