Parcourir la source

新增mgo连接账号密码

maxiaoshan il y a 4 ans
Parent
commit
1c7942415f
3 fichiers modifiés avec 15 ajouts et 2 suppressions
  1. 4 2
      src/saveServer/config.json
  2. 9 0
      src/saveServer/tools/mgo.go
  3. 2 0
      src/saveServer/util.go

+ 4 - 2
src/saveServer/config.json

@@ -1,7 +1,9 @@
 {
-    "mongodbServers": "192.168.3.207:27092",
+    "mongodbServers": "192.168.3.205:27082",
     "mongodbPoolSize": "5",
-    "mongodbName": "spider",
+    "mongodbName": "qfw",
+    "username": "dataAnyWrite",
+    "password": "data@dataAnyWrite",
     "spider":{
     	"addr": "192.168.3.207:27092",
 		"db": "spider",

+ 9 - 0
src/saveServer/tools/mgo.go

@@ -127,6 +127,8 @@ type MongodbSim struct {
 	Ctx      context.Context
 	ShortCtx context.Context
 	pool     chan bool
+	UserName string
+	Password string
 }
 
 func (m *MongodbSim) GetMgoConn() *MgoSess {
@@ -147,6 +149,13 @@ func (m *MongodbSim) InitPool() {
 	opts.SetConnectTimeout(3 * time.Second)
 	opts.ApplyURI("mongodb://" + m.MongodbAddr)
 	opts.SetMaxPoolSize(uint64(m.Size))
+	if m.UserName != "" && m.Password != "" {
+		cre := options.Credential{
+			Username: m.UserName,
+			Password: m.Password,
+		}
+		opts.SetAuth(cre)
+	}
 	m.pool = make(chan bool, m.Size)
 	opts.SetMaxConnIdleTime(2 * time.Hour)
 	m.Ctx, _ = context.WithTimeout(context.Background(), 99999*time.Hour)

+ 2 - 0
src/saveServer/util.go

@@ -36,6 +36,8 @@ func InitMgo() {
 		MongodbAddr: qutil.ObjToString(config["mongodbServers"]),
 		Size:        qutil.IntAll(config["mongodbPoolSize"]),
 		DbName:      qutil.ObjToString(config["mongodbName"]),
+		UserName:    qutil.ObjToString(config["username"]),
+		Password:    qutil.ObjToString(config["password"]),
 	}
 	tools.Mgo.InitPool()
 	//spider