51

I tried to restore mongo from dump but failed:

mongorestore --port 27133 dump
2015-05-07T09:39:11.760+0300    Failed: no reachable servers

Although I can connect to it without any problem:

$ mongo --port 27133
MongoDB shell version: 3.0.1
connecting to: 127.0.0.1:27133/test

In a log file there is nothing special:

2015-05-07T09:37:00.350+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:44901 #1 (1 connection now open)
2015-05-07T09:37:13.935+0300 I NETWORK  [conn1] end connection 127.0.0.1:44901 (0 connections now open)
2015-05-07T09:39:08.752+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:44906 #2 (1 connection now open)
2015-05-07T09:39:11.763+0300 I NETWORK  [conn2] end connection 127.0.0.1:44906 (0 connections now open)
2015-05-07T09:39:52.365+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:44907 #3 (1 connection now open)
2015-05-07T09:39:55.064+0300 I NETWORK  [conn3] end connection 127.0.0.1:44907 (0 connections now open)
2015-05-07T09:40:11.272+0300 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:44909 #4 (1 connection now open)
2015-05-07T09:40:14.281+0300 I NETWORK  [conn4] end connection 127.0.0.1:44909 (0 connections now open)

Update

Host 127.0.0.1 didn't help

$ mongorestore --host=127.0.0.1 --port=27132 dump
2015-12-16T18:52:33.270+0300    Failed: no reachable servers

Although I can still connect using mongo command:

$ mongo --host=127.0.0.1 --port=27133
MongoDB shell version: 3.2.0
connecting to: 127.0.0.1:27133/test
> ^C
bye

Host 0.0.0.0 didn't help as well:

$ mongorestore --host=0.0.0.0 --port=27133 dump

I have 3.2 version of MongoDb:

$ mongorestore --version
mongorestore version: 3.2.0-rc5
git version: 6186100ad0500c122a56f0a0e28ce1227ca4fc88
Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166

13 Answers13

52

try adding host param

mongorestore --host=127.0.0.1 --port=27133 dump
ranjeetcao
  • 1,743
  • 2
  • 20
  • 28
25

Do you run mongo in replica set, i.e., mongod --replSet rs0?

If yes, please remember to run in your mongo shell the command: rs.initiate()

Then you can use cmd mongorestore to restore your db.

Shi Jieming
  • 541
  • 6
  • 5
24

The problem occured because --replSet was enabled in configuration. But the node wasn't yet in any replica set.

After I removed --replSet from configuration, relaunched mongodb server, mongorestore started to work without any --host parameter.

Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166
  • 14
    can you please explain how to disable "--replSet"? – Behrouz Riahi Oct 16 '16 at 19:00
  • 7
    This is an insufficient answer and should not be accepted. – podcastfan88 Sep 05 '18 at 01:38
  • First, you have to check which DB instance is Primary by rs.status() command. Then get the host id and use it in mongorestore command. OR For remote DB 1- Making a tunnel using `ssh -L 13306:mongodb-ip:27017 -i path-to-pem-key.pem user@server-ip` 2- `mongo localhost:13306` 3- `rs.status()` 4- `mongorestore -v --host localhost:13306 --drop --db db-name path-to-dump --writeConcern '{w:0}'` – Ahmed Jehanzaib Sep 24 '18 at 08:05
17

I have MongoDB installed through Homebrew, and was having this issue. Here are the symptoms:

These failed:

mongorestore dump

mongorestore dump --host=localhost

The error I got: Failed: error connecting to db server: no reachable servers

These worked (as a workaround):

mongorestore dump --host=127.0.0.1

mongorestore dump --host=0.0.0.0

This fixed the problem for me:

brew update && brew upgrade

That updated MongoDB to 3.0.7 and now mongorestore dump works without the host flag.

Colin Marshall
  • 2,142
  • 2
  • 21
  • 31
5

I struggled with the same issue while using MongoDB Atlas for a few hours.

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
4

check your mongod configuration file, if you have enabled replication disable it, if enabled, key based authentication disable it basically disable every other thing and restart the mongod service and then try doing mongo restore.

And BTW, try binding mongod ipaddress to your machine address as well, if it is a local mongod instance try binding the ip address to 0.0.0.0, in mongod.conf file under bind_ip="your ip address", do let me know it does or does not.

Prashanth Kumar B
  • 566
  • 10
  • 25
2

I had difficulty with that question also.

mongorestore --host f8cab83e7294 --port 27601 gene_backup_final/gene_backup/s2/  --verbose

2016-12-05T13:48:15.333-0700 Failed: error connecting to db server: no reachable servers

I failed to initiate the replica set. That is why there was no reachable server

After initiating the RS, restore worked.

mongo localhost:27501
rs.initiate()
pckill
  • 3,709
  • 36
  • 48
Liz
  • 71
  • 1
  • 2
2

In my case (with Mongo 3.2.9), the server was configured to listen on the IPv4 localhost address (127.0.0.1) but mongorestore tried only the IPv6 localhost address (::1).

Using -h 127.0.0.1 worked. However, this shouldn't be necessary, IMHO, and is a bug in mongorestore. It should use the same connection logic as the mongo command-line client, which was working.

Note: replica sets were not enabled.

Neil Mayhew
  • 14,206
  • 3
  • 33
  • 25
2

For me the issue was in version of mongorestore

Earlier when I was using an old version mongorestore version: built-without-version-string it was giving me no reachable servers, even after adding all host and port options.

Switching to version mongorestore version: 100.6.0 - the same command worked

Nimish Agrawal
  • 511
  • 1
  • 5
  • 11
1

First run mongod

If you get exception exception in initAndListen: 29 Data directory /data/db not found., terminating, do sudo mkdir -p /data/db

If you get expection exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating, This probably you have the permission issue. If this is your local, you can run sudo chmod 7777 /data/db to fix the issue.

Now, mongod should work. Then, run mongorestore dump where your dump file is.

You should successfully restore the dump file.

0

I have the same problem. I tried to start mongod before restoring my dump, and it work for me. Hope this help this situation

Huy Truong
  • 11
  • 3
0

In my case, I am defining port two times.

mongorestore --noIndexRestore --host X.X.X.X:27017 -d XYZ --port 27017 foldername

Solve in my case,

mongorestore --noIndexRestore --host X.X.X.X -d XYZ --port 27017 foldername

Dere Sagar
  • 1,719
  • 1
  • 10
  • 7
0

If you're using a hosted service (like MongoDB Atlas), make sure you've set up the IP white-list so that it isn't blocking your IP address. You can set it to 0.0.0.0/0 to whitelist all IP addresses, but this is a bad idea in terms of security so it should only be used as a temporary measure.