how to connect compose mongodb with golang

huangapple go评论119阅读模式
英文:

how to connect compose mongodb with golang

问题

我尝试在Bluemix中使用Golang程序连接Compose MongoDB,但是我得到了不支持的连接URL选项ssl。这里我给出了从ComposeDB控制台获取的MongoDB连接字符串。如何连接到远程主机,需要连接的语法?

  1. session, err := mgo.Dial("mongodb://****:****@aws-us-east-1-portal.26.dblayer.com:20258/admin?ssl=true")
  2. if err != nil {
  3. panic(err)
  4. }
  5. defer session.Close()
  6. // 可选的。将会话切换到单调行为。
  7. session.SetMode(mgo.Monotonic, true)
  8. c := session.DB("test").C("people")
  9. err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
  10. &Person{"Cla", "+55 53 8402 8510"})
  11. if err != nil {
  12. log.Fatal(err)
  13. }

错误信息:

  1. panic: unsupported connection URL option: ssl=true
  2. goroutine 1 [running]:
  3. panic(0x2130a0, 0xc82000a840)
  4. /usr/local/go/src/runtime/panic.go:481 +0x3e6
  5. main.main()
  6. /Users/vit/gocode/src/mongmix/mmix.go:19 +0x9b
  7. exit status 2
英文:

I try to connect compose mongodb in bluemix with golang program but i get unsupported connection url option ssl. Here I give mongodb connection string got from composedb console. How to connect with remote host need syntax for connect?

  1. session, err := mgo.Dial("mongodb://****:****@aws-us-east-1-portal.26.dblayer.com:20258/admin?ssl=true")
  2. if err != nil {
  3. panic(err)
  4. }
  5. defer session.Close()
  6. // Optional. Switch the session to a monotonic behavior.
  7. session.SetMode(mgo.Monotonic, true)
  8. c := session.DB("test").C("people")
  9. err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
  10. &Person{"Cla", "+55 53 8402 8510"})
  11. if err != nil {
  12. log.Fatal(err)
  13. }

The error:

  1. panic: unsupported connection URL option: ssl=true
  2. goroutine 1 [running]:
  3. panic(0x2130a0, 0xc82000a840)
  4. /usr/local/go/src/runtime/panic.go:481 +0x3e6
  5. main.main()
  6. /Users/vit/gocode/src/mongmix/mmix.go:19 +0x9b
  7. exit status 2

答案1

得分: 1

  1. package main
  2. // 导入mongo gopkg
  3. import (
  4. "fmt"
  5. "gopkg.in/mgo.v2"
  6. "gopkg.in/mgo.v2/bson"
  7. )
  8. // 添加结构体
  9. type Person struct {
  10. Name string
  11. Phone string
  12. }
  13. // 主函数
  14. func main() {
  15. fmt.Println("Hello world")
  16. // 数据库连接
  17. session, err := mgo.Dial("localhost:9494")
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer session.Close()
  22. // 可选的,将会话切换为单调行为
  23. session.SetMode(mgo.Monotonic, true)
  24. // 数据库名称
  25. c := session.DB("sudheer").C("people")
  26. err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
  27. &Person{"Cla", "+55 53 8402 8510"})
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. result := Person{}
  32. err = c.Find(bson.M{"name": "Ale"}).One(&result)
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println("Phone:", result.Phone)
  37. }

这是一个Go语言的代码片段,它使用了gopkg.in/mgo.v2包来连接MongoDB数据库,并进行一些操作。代码中定义了一个Person结构体,包含姓名和电话字段。在main函数中,首先建立数据库连接,然后插入两个Person对象到名为sudheer的数据库的people集合中。接着通过姓名查询并打印出电话号码。

请注意,这段代码中的数据库连接地址是localhost:9494,你需要根据实际情况修改为正确的地址。另外,你还需要确保已经安装了gopkg.in/mgo.v2包。

英文:

> install "go get gopkg.in/mgo.v2"

  1. package main
  2. ///////////////import mongo gopkg//////////////
  3. import (
  4. "fmt"
  5. "gopkg.in/mgo.v2"
  6. "gopkg.in/mgo.v2/bson"
  7. )
  8. //////////// add struct//////////////////////////
  9. type Person struct {
  10. Name string
  11. Phone string
  12. }
  13. ///// main function///////////
  14. func main(){
  15. fmt.Println("Hello world");
  16. //////db connection ////////////
  17. session, err := mgo.Dial("localhost:9494")
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer session.Close()
  22. // Optional. Switch the session to a monotonic behavior.
  23. session.SetMode(mgo.Monotonic, true)
  24. ///////////////db name/////////////
  25. c := session.DB("sudheer").C("people")
  26. err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
  27. &Person{"Cla", "+55 53 8402 8510"})
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. result := Person{}
  32. err = c.Find(bson.M{"name": "Ale"}).One(&result)
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println("Phone:", result.Phone)
  37. }

答案2

得分: 0

IBM Bluemix Compose for Mongodb有一份在线文档,可以在这里找到:https://console.bluemix.net/docs/services/ComposeForMongoDB/index.html#getting-started-with-compose-for-mongodb

第2步讲述了如何连接到您的Compose MongoDB。第2步提供了一个Node.js示例代码。您需要检查在Golang中连接到MongoDB的连接字符串,即Golang中的语法。

我找到了一篇文章,解释了如何从Golang连接到IBM Compose MongoDB:https://www.compose.com/articles/connect-to-mongo-3-2-on-compose-from-golang/

希望这对您有所帮助。

英文:

IBM Bluemix Compose for Mongodb has a documentation available online here: https://console.bluemix.net/docs/services/ComposeForMongoDB/index.html#getting-started-with-compose-for-mongodb

Step 2 talks about connecting to your compose mongo db. There is a nodejs sample code provided in step 2. You will have to check connection string to mongodb in golang i.e. the syntax in golang.

I found out this article which explains connecting to IBM Compose mongodb from goland: https://www.compose.com/articles/connect-to-mongo-3-2-on-compose-from-golang/

hope this helps.

huangapple
  • 本文由 发表于 2017年6月15日 18:24:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/44565052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定