-
Notifications
You must be signed in to change notification settings - Fork 3
mongodb
cym edited this page Jan 10, 2018
·
1 revision
-
关闭 mongod
$ mongo > db.shutdownServer(); -
启动 mongod
$ ./mongod -config mongodb.conf -
mongodb.conf
$ vim mongodb.conf systemLog: destination: file path: /the/path/to/log/mongodb.log logAppend: true storage: dbPath: /the/path/to/data/ net: bindIp: 127.0.0.1 processManagement: fork: true security: authorization: enabled # enabled/disabled #开启客户端认证
4. 认证
```
> use [db]
> db.auth("[user]","[password]")
```
5. mongodb golang 驱动 mgo 简单使用
```golang
package main
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type People struct {
Name string `bson:"name"`
}
func main() {
session, err := mgo.Dial("mongodb://serverParkSocket:serverParkSocketAdmin123@localhost:27017/parkSocket") //传入数据库的地址,可以传入多个,具体请看接口文档
if err != nil {
panic(err)
}
defer session.Close() //用完记得关闭
c := session.DB("parkSocket").C("people1")
name := "b"
people := &People{
Name: name,
}
// 插入数据
err = c.Insert(people)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("insert "+people.Name+ " ok")
}
// 查询
result := People{}
err = c.Find(bson.M{"name": "b"}).One(&result)
if err!=nil{
fmt.Println(err)
} else {
fmt.Println(result)
}
}
```