Can not connect to mongodb
Closed this issue · 8 comments
On an ubuntu machine, I have modified config.js to
"Database": {
"Type": "MongoDB",
"Bolt": {
"Path": "gowebapp.db"
},
"MongoDB": {
"URL": "127.0.0.1",
"Database": "gowebappdb"
},
And I have created gowebappdb
. However I get this error at compile time:
database.go:98: MongoDB Driver Error write tcp 127.0.0.1:34161->127.0.0.1:27017: i/o timeout
register.go:88: Database is unavailable.
Appreciate your hints.
Are you able to access the Mongo instance using the mongo shell? https://docs.mongodb.org/manual/reference/mongo-shell/
Yes. I created the db from mongo shell.
It may worth mentioning that I have not this issue with Bolt, and the web server port is 8080, i.e. http://localhost:8080/register
Can you try connecting to mongo via the IP address?
This is the code used to connect to Mongo. Can you try increasing the socket timeout?
// Connect to MongoDB
if Mongo, err = mgo.DialWithTimeout(d.MongoDB.URL, 5); err != nil {
log.Println("MongoDB Driver Error", err)
return
}
// Prevents these errors: read tcp 127.0.0.1:27017: i/o timeout
Mongo.SetSocketTimeout(1 * time.Second)
// Check if is alive
if err = Mongo.Ping(); err != nil {
log.Println("Database Error", err)
}
Well I set the timeout to 1000 sec but still get this error:
database.go:98: MongoDB Driver Error write tcp 127.0.0.1:35979->127.0.0.1:27017: i/o timeout
Interestingly, I can connect to mongodb from the IP:
me@pc:~/go/src/github.com/josephspurrier/gowebapp$ mongo --host 127.0.0.1 --verbose
MongoDB shell version: 3.2.5
connecting to: 127.0.01:27017/test
2016-04-21T04:08:35.398+0200 D NETWORK [thread1] creating new connection to:127.0.01:27017
2016-04-21T04:08:35.398+0200 D COMMAND [ConnectBG] BackgroundJob starting: ConnectBG
2016-04-21T04:08:35.398+0200 D NETWORK [thread1] connected to server 127.0.01:27017 (127.0.0.1)
2016-04-21T04:08:35.398+0200 D NETWORK [thread1] connected connection!
Server has startup warnings:
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten]
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten]
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten]
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten]
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-04-21T04:05:21.351+0200 I CONTROL [initandlisten]
> show databases
gowebappdb 0.078GB
local 0.078GB
taskdb 0.078GB
But not from IP with port:
me@pc:~/go/src/github.com/josephspurrier/gowebapp$ mongo --host 127.0.0.1:8080 --verbose
MongoDB shell version: 3.2.5
connecting to: 127.0.0.1:8080/test
2016-04-21T04:11:03.025+0200 D NETWORK [thread1] creating new connection to:127.0.0.1:8080
2016-04-21T04:11:03.025+0200 D COMMAND [ConnectBG] BackgroundJob starting: ConnectBG
2016-04-21T04:11:03.025+0200 D NETWORK [thread1] connected to server 127.0.0.1:8080 (127.0.0.1)
Hanged here
Solved!
I changed
if Mongo, err = mgo.DialWithTimeout(d.MongoDB.URL, 5); err != nil {
log.Println("MongoDB Driver Error", err)
return
}
to
if Mongo, err = mgo.Dial("localhost"); err != nil {
log.Println("MongoDB Driver Error", err)
return
}
And now I can register users to the db.
Thanks you for the great starter MVC!
Ah, it looks like the DialWithTimeout value of 5 is not seconds, it is nanoseconds. It should be set to 5000. Thanks, I'll update the code!