63

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

I search the stackOverflow and google and someone told marked off bind_ip or set bind_ip to 0.0.0.0, I tried all but still failed.

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 = <size>

# 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
James Chien
  • 997
  • 1
  • 7
  • 17
  • Your `mongod` process is clearly not running. Look at the log `/var/log/mongodb/mongod.log` for reasons why. Also make sure you followed all the instructions on [Install MongoDB on Ubuntu](http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/). Try to start the service, or restart the machine. Clean install if necessary. – Blakes Seven Aug 25 '15 at 05:23
  • Submitting this as an answer as I don't have the privilege to comment. I have a 3 member Replica Set (version 3.4) and faced the same issue with the below command. mongoimport --db test --drop rides.json Got the same error with the below command suggested by [whythecode](https://stackoverflow.com/users/5000167/whythecode),
    mongoimport --host=127.0.0.1 --db test rides.json The following command was able to import mongoimport --host=127.0.0.1:27018 --db test rides.json Note that the primary was running on 27018. Also, it wasn't necessary to reset the Replica Set or run it as standalone.
    – Van Peer Jun 30 '17 at 14:53

14 Answers14

92

A temporary workaround is to add the host param to your mongoimport call, letting mongo know that the host is your own machine (127.0.0.1):

mongoimport --host=127.0.0.1

The full command in your case is then:

mongoimport --host=127.0.0.1 \
  --db test --collection restaurants --drop --file dataset.json

(Source: ranjeetcao @ mongorestore Failed: no reachable servers)


Update:

Updating to Mongo >= 3.0.7 should solve the issue

(Source: Colin Marshall @ mongorestore Failed: no reachable servers)


Update 2:

It seems the Bug is still happening for some users.

There's also an open JIRA issue here.

(Source: James Chien @ this question thread)


Update 3:

In some cases, this can be caused by Mongo running as a ReplicaSet. To solve this, as far as I've seen, one can either disable the ReplicaSet functionality or reset it. See:

(Source: Maxim Yefremov @ mongorestore Failed: no reachable servers)

whythecode
  • 1,099
  • 7
  • 14
3

You need to provide host option try the following command :

mongoimport --host=127.0.0.1 -d dbName -c collectionName --file output.txt
Dere Sagar
  • 1,719
  • 1
  • 10
  • 7
Shemeer M Ali
  • 1,030
  • 15
  • 39
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/11097993) – cviejo Feb 04 '16 at 15:10
  • why not? The main answer only "mongoimport --host=127.0.0.1", so new guys will be confused about it and they can use full command. – Shemeer M Ali Feb 05 '16 at 06:18
  • 1
    @shemeermali you're right that it's better to stipulate the full command - the answer has been updated. thanks! – whythecode Apr 12 '16 at 09:19
3

To provide the options host and port

mongoimport --host X.X.X.X --port 27017 --db dbName --collection collectionName --file fileName.json --jsonArray
Dere Sagar
  • 1,719
  • 1
  • 10
  • 7
2

I find the answer by this links https://jira.mongodb.org/browse/TOOLS-620 It seem it is a mongodb tools bugs that doesn't resolved yet. Not sure if anyone know if the latest tools have fixed the issue?

BTW, I uninstall the mongodb-tools 3.x and reinstall to 2.6.0 that resolved the problem

sudo apt-get purge mongodb-org-tools
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install -y mongodb-org-tools=2.6.0

mongoimport --db test --collection restaurants --drop --file dataset.json
connected to: 127.0.0.1
2015-08-25T15:19:59.494+0800 dropping: test.restaurants
2015-08-25T15:20:00.089+0800 check 9 25359
2015-08-25T15:20:00.089+0800 imported 25359 objects
James Chien
  • 997
  • 1
  • 7
  • 17
2

If TLS is enabled with authentication then use the below format from the dump location

mongorestore --host=example.com --port=27017 --username=user_name  --authenticationDatabase=admin --ssl --sslCAFile /etc/ssl/rootCA.pem --sslPEMKeyFile /etc/ssl/mongodb.pem
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
1

Adding another answer for those who are struggling with the same issue while using MongoDB Atlas.

I tried to follow the offical guides like Seed with mongorestore and Load File with mongoimport but received the same no reachable servers error.

I tried to change the command arguments over and over - nothing changed.

But then I navigated inside my cluster to the "Command Line Tools" tab and saw the exact command I should run for mongorestore, mongodump, mongoimport,mongoexport etc':

enter image description here

Adding as inline the full command:

mongorestore --host 
<cluster-name>-shard-0/
<cluster-name>-shard-00-00-obe3i.mongodb.net:27017,
<cluster-name>-shard-00-01-obe3i.mongodb.net:27017,
<cluster-name>-shard-00-02-obe3i.mongodb.net:27017 
--ssl 
--username <User-Name> 
--password <PASSWORD> 
--authenticationDatabase admin 

The mongorestore comand can be replaced with mongodump,mongoimport,mongoexport etc'.

(*) Prefer copying the command directly from dashboard because the DNS of the replica-sets might change.

(**) MongoDB shell version v4.2.5.

Rot-man
  • 18,045
  • 12
  • 118
  • 124
1

For someone using Mongo Atlas and json file for import into collection:

mongoimport --uri mongodb+srv://<user>:<password>@your-cluster.xxxx.mongodb.net/<db> -c <collection> --file import.json --jsonArray
Alex Ershov
  • 21
  • 1
  • 4
1

For mongodb docker compose user the below command to seed the database

CMD mongoimport --uri mongodb://mongodb:27017/testdb --collection users --type json --file /init.json --jsonArray
0

For people still facing this issue with MongoDB Atlas, just update your mongoDB version to more than 4.0.0. and it will work.

Martin De Simone
  • 2,108
  • 3
  • 16
  • 30
0

I went ahead and downloaded MongoDB Compass and used the connection string from Atlas for For NodeJS version 2.2.12 or later

and that connection worked like a charm.

After that I went ahead and added the data using the Add Data option and uploaded the file which i want to mongoimport

kaushalop
  • 858
  • 9
  • 14
0

I used mongo atlas to import CSV file to the cluster, I faced the same issue I tried many different solutions mentioned here but no one worked, I get back to the documentation this one worked for me, go to the dashbord and click on the name of your cluster, in the extreme right there is tab called command-line tools Data Import and Export Tools you will find how to import to your cluster

mongoimport --uri mongodb+srv://admin:<PASSWORD>@<NAME>.mongodb.net/<DATABASE> --collection <COLLECTION> --type <FILETYPE> --file <FILENAME>

if you are uploading csv file add --headerline after type

0

Hi if you download latest version Mongo Database tool and it will work from the example website shows.

https://www.mongodb.com/docs/database-tools/installation/installation/

mongoimport | imports content from an Extended JSON, CSV, or TSV export

mongoimport --uri mongodb+srv://usernmae:<PASSWORD>@monghosturl-part.mongodb.net/<DATABASE> --collection <COLLECTION> --type <FILETYPE> --file <FILENAME>

mongoexport | produces a JSON or CSV export of data stored in a MongoDB instance

mongoexport --uri mongodb+srv://usernmae:<PASSWORD>@monghosturl-part.mongodb.net/<DATABASE> --collection <COLLECTION> --type <FILETYPE> --out <FILENAME>

CMD output

sendon1982
  • 9,982
  • 61
  • 44
-1

If you have installed mongodb through homebrew then you can simply start mongodb through

brew services start mongodb

Then access the shell by

mongo

You can shut down your db by

brew services stop mongodb

For more options

brew info mongodb
Fomovet
  • 313
  • 1
  • 5
  • 15
-2
mongorestore --db <thedb> --host=127.0.0.1 --port=<port> --drop path/db
Machavity
  • 30,841
  • 27
  • 92
  • 100
Jekayode
  • 1,552
  • 1
  • 12
  • 6