mongo.go 551 B

1234567891011121314151617181920212223242526
  1. package clinit
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. "os"
  6. "strings"
  7. "time"
  8. )
  9. var MongoSession *mgo.Session
  10. func InitMongo(address, username, password string) {
  11. addressList := strings.Split(address, ",")
  12. dialInfo := &mgo.DialInfo{Addrs: addressList, Username: username, Password: password, Timeout: time.Second * 60}
  13. session, err := mgo.DialWithInfo(dialInfo)
  14. if err != nil {
  15. fmt.Printf("new mongo client failed. error:%s\n", err)
  16. os.Exit(1)
  17. }
  18. MongoSession = session
  19. }
  20. func GetMongoSession() *mgo.Session {
  21. return MongoSession.Clone()
  22. }