You can use Data Management Service (DMS) or the mongo shell to connect to ApsaraDB for MongoDB. This topic covers common mongo shell connection errors and solutions.
"connection attempt failed" error
Symptom:
#mongo --host ali12345678.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u test -p xxx
MongoDB shell version: 3.2.3
DB Prefix:
connecting to: 10.1.2.8:3717/admin
2016-05-31T15:25:58.940+0800 W NETWORK Failed to connect to 10.*.*.8:3717 after 5000 milliseconds, giving up.
2016-05-31T15:25:58.943+0800 E QUERY Error: couldn't connect to server 10.*.*.8:3717 (10.1.2.8), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
| Possible cause | Solution |
| The ECS instance and the ApsaraDB for MongoDB instance are in different VPCs or use different network types. |
|
Use telnet to verify network connectivity. Example: telnet dds-ali123456789.mongodb.rds.aliyuncs.com 3717
root@mongotest:~# telnet xxx.mongodb.rds.aliyuncs.com 3717
Trying xxx...
Connected to xxx.mongodb.rds.aliyuncs.com.
Escape character is '^]'.
This output confirms that DNS resolution succeeds and port 3717 is reachable.
"Authentication failed" error
Symptom:
#mongo --host ali12345678.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u test -p xxx
MongoDB shell version: 3.2.3
connecting to: 10.1.2.8:3717/test
2016-05-31T15:50:18.623+0800 E QUERY Error: 18 Authentication failed.
at DB._authOrThrow (src/mongo/shell/db.js:1271:32)
at (auth):6:8
at (auth):7:2 at src/mongo/shell/db.js:1271
exception: login failed
| Possible cause | Solution |
| The database username is incorrect. | Log on to the database with the correct username. |
| The database password is incorrect. | Log on to the database with the correct password. |
| The account does not belong to the specified authentication database. | Specify the correct authentication database for the account. For example, the root account belongs to the admin database, so use --authenticationDatabase admin when connecting as root. |
| The client version is outdated. | Upgrade to mongo shell 3.0 or later. Install MongoDB. For driver version requirements, check the Driver compatibility documentation. |
Network error when running "isMaster"
Symptom:
#mongo --host ali12345678.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase test -u test -p xxxxxx
MongoDB shell version v3.4.10
connecting to: mongodb:ali1234567878.mongodb.rds.aliyuncs.com:3717/
2018-12-18T14:26:11.946+0800 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host 'ft12345678.mongodb.rds.aliyuncs.com:3717' :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed
| Possible cause | Solution |
| The ECS instance's IP address is not in the MongoDB instance's whitelist. | Add the ECS instance's IP address to the whitelist. Configure a whitelist for an ApsaraDB for MongoDB instance. |
"Timeout while receiving message" error
org.springframework.data.mongodb.UncategorizedMongoDbException: Timeout while receiving message; nested exception is com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
| Possible cause | Solution |
| Slow queries cause CPU utilization to spike. | Identify slow queries and add indexes. Analyze slow queries. |
| The application's connection pool is misconfigured (for example, incorrect timeout values). | Review your connection pool settings. Query and limit the number of connections. |
Common connection scenarios
- Connect to a MongoDB instance over the public network
- Connect an ECS instance to an ApsaraDB for MongoDB instance with a different network type over the internal network
- Connect an ECS instance to an ApsaraDB for MongoDB instance in a different region over the internal network
- Connect an ECS instance to a MongoDB instance in a different account over the internal network