josephspurrier / gowebapp

Basic MVC Web Application in Go
MIT License
1.14k stars 197 forks source link

Can not connect to mongodb #12

Closed k0fi closed 8 years ago

k0fi commented 8 years ago

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.

josephspurrier commented 8 years ago

Are you able to access the Mongo instance using the mongo shell? https://docs.mongodb.org/manual/reference/mongo-shell/

k0fi commented 8 years ago

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

josephspurrier commented 8 years ago

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)
        }
k0fi commented 8 years ago

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

k0fi commented 8 years ago

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

k0fi commented 8 years ago

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!

josephspurrier commented 8 years ago

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!

josephspurrier commented 8 years ago

https://github.com/josephspurrier/gowebapp/commit/53a57574ad3deec6a4d887a4d5ab6d29f21a6498