Mongodb Failed: error connecting to db server: no reachable servers

3.3K    Asked by Ankesh Kumar in Salesforce , Asked on May 12, 2021

I installed mongodb in Ubuntu14.04 server

I was not able to connect to mongodb via "mongoimport", "mongodump", "mongostat", etc. It always show "no reachable server"

mongoimport --db test --collection restaurants --drop --file dataset.json
2015-08-25T13:08:29.802+0800    [........................] test.restaurants 0.0 B/11.3 MB (0.0%)
2015-08-25T13:08:30.306+0800    Failed: error connecting to db server: no reachable servers
2015-08-25T13:08:30.306+0800    imported 0 documents
Somehow, I was able to connect with mongo shell
mongo --port 27017
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1:27017/test
At first, I doubt if it cause by my iptables, so I flush all iptables rules and create rules for ALL accept, but it still same
 sudo iptables -S
    -P INPUT ACCEPT
    -P FORWARD ACCEPT
    -P OUTPUT ACCEPT
    -A INPUT -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A FORWARD -j ACCEPT
    -A OUTPUT -j ACCEPT
    -A OUTPUT -o lo -j ACCEPT
Below are my mangodb config, would any one can help me to check? Thanks for your help
james@localhost:~$ cat /etc/mongod.conf 
# mongod.conf
# Where to store the data.
# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/var/lib/mongodb
#where to log
logpath=/var/log/mongodb/mongod.log
logappend=true
port = 27017
# Listen to local interface only. Comment out to listen on all interfaces. 
#bind_ip = 127.0.0.1
# Disables write-ahead journaling
# nojournal = true
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true
# Verbose logging output.
verbose = true
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true
# Enable db quota management
#quota = true
# Set oplogging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog = 0
# Ignore query hints
#nohints = true
# Enable the HTTP interface (Defaults to port 28017).
#httpinterface = true
# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting = true
# Turns off table scans.  Any query that would do a table scan fails.
#notablescan = true
# Disable data file preallocation.
#noprealloc = true
# Specify .ns file size for new databases.
# nssize =
# Replication Options
# in replicated mongo databases, specify the replica set name here
#replSet=setname
# maximum size in megabytes for replication operation log
#oplogSize=1024
# path to a key file storing authentication info for connections
# between replica set members
#keyFile=/path/to/keyfile

Answered by Carol Bower

You have to add syntax to the host param to your mongoimport call to solve the failed: error connecting to db server: no reachable servers. We are doing that because we want the mongo to know that the host is your personal machine. The syntax for performing is given below:

mongoimport --host=127.0.0.1

The full command in your state is as follows:

mongoimport --host=127.0.0.1  --db databaseName--collection collectionName --drop --file fileName with extension

Your Answer

Answer (1)

The error "Error connecting to DB server: No reachable servers" in MongoDB typically indicates that your application is unable to establish a connection with the MongoDB server. Here are some steps you can take to troubleshoot and resolve this issue:

Check MongoDB Server Status: Ensure that the MongoDB server is running and accessible. You can do this by logging into the server where MongoDB is installed and checking the status of the MongoDB service. For example, on Linux systems, you can use the command:

systemctl status mongod

On Windows, you can check the status of the MongoDB service in the Services Manager.

Verify MongoDB Configuration: Check the MongoDB configuration file (usually mongod.conf) to ensure that the server is listening on the correct IP address and port. You may need to configure MongoDB to listen on a specific IP address or network interface.

Check Network Connectivity: Ensure that there are no network issues preventing your application from reaching the MongoDB server. You can test network connectivity by trying to ping the MongoDB server from the machine where your application is running.

Firewall Settings: Check the firewall settings on both the MongoDB server and the client machine to ensure that the necessary ports (default is 27017) are open for communication.

Check MongoDB Log Files: Check the MongoDB log files for any error messages or warnings that may indicate why the server is not reachable. The log files are typically located in the MongoDB data directory.

Restart MongoDB Service: If everything seems to be configured correctly, try restarting the MongoDB service to see if that resolves the issue. Sometimes, a simple restart can fix connectivity problems.

Check Client Configuration: Double-check the connection string and other configuration settings in your application code to ensure that they are correct. Make sure that the hostname, port, and authentication credentials (if any) are properly configured.

Test with MongoDB Shell: If you're still unable to connect, try connecting to the MongoDB server using the MongoDB shell (mongo) from the command line on the client machine. This can help isolate whether the issue is with your application or with the MongoDB server itself.

By following these steps and troubleshooting the possible causes, you should be able to resolve the "No reachable servers" error and establish a successful connection to your MongoDB server.

6 Months

Interviews

Parent Categories