Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb & Go 项目部署 #6

Open
rainyear opened this issue Jun 22, 2015 · 0 comments
Open

Mongodb & Go 项目部署 #6

rainyear opened this issue Jun 22, 2015 · 0 comments
Assignees

Comments

@rainyear
Copy link
Owner

用Mongo作DB,基于Go语言的Gin框架的API,部署时需要添加MongoDB用户权限,同时部署Go项目用到godep,下面简单记录一些操作步骤。

MongoDB version 3.0.4
go version go1.4.2

MongoDB 安全设置

Mongo采用基于角色的访问控制(Role-Based Access Control),不同的用户角色拥有不同的权限。启动MongoDB服务默认是不需要权限的:

# start mongod
mongod --dbpath=/path/to/data

# login mongo shell
mongo

登入之后先创建用户管理账号,切换至系统管理员数据库,创建管理员账号admin,其角色为管理所有数据库用户

> use admin
> db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] 
  }
)

之后可以退出 Mongo shell,重新启动 mongod,加上访问权限的相关设定:

mongod --dbpath=/path/to/data --auth --nohttpinterface --bind_ip 127.0.0.1

重新登入 Mongo shell,并添加可操作数据库权限(readWrite)的用户角色:

mongo -u admin -p password --authenticationDatabase admin

> use your-db-name
> db.createUser(
  {
    user: "rainy",
    pwd: "year",
    roles: [ { role: "readWrite", db: "your-db-name" } ]
  }
)

添加读写操作用户权限之后,再次访问数据库内容,会被拒绝,需要先通过db.auth("rainy", "year")完成验证:

> show collections
> Error: listCollections failed: {
        "ok" : 0,
        "errmsg" : "not authorized on coodict to execute command { listCollections: 1.0 }",
        "code" : 13
}
> db.auth("rainy", "year")
1

Go 项目部署

主要是对依赖的包进行管理,godep可以对当前项目所依赖的包统计整理,部署时在新的环境中自动获取依赖的包:

go get github.com/tools/godep
cd path/to/your/golang/project
godep save #这一步需要你的项目放在`GOPATH/src`中,否则会出错,也可以在`GOPATH/src`创建软链

godep save在当前目录下的./Godeps/Godeps.json包含所有依赖关系:

{
        "ImportPath": "github.com/rainyear/coodict",
        "GoVersion": "go1.4.2",
        "Deps": [
                {
                        "ImportPath": "github.com/dgrijalva/jwt-go",
                        "Comment": "v2.2.0-28-g2e53eb6",
                        "Rev": "2e53eb673c4450614e3bca6f345197f9527dce71"
                },
                {
                        "ImportPath": "github.com/gin-gonic/gin",
                        "Comment": "v1.0rc1-104-g1a7ab6e",
                        "Rev": "1a7ab6e4d5fdc72d6df30ef562102ae6e0d18518"
                },
                {
                        "ImportPath": "github.com/manucorporat/sse",
                        "Rev": "c142f0f1baea5cef7f98a8a6c222f6134368c1f5"
                },
                {
                        "ImportPath": "github.com/mattn/go-colorable",
                        "Rev": "d67e0b7d1797975196499f79bcc322c08b9f218b"
                },
                {
                        "ImportPath": "golang.org/x/net/context",
                        "Rev": "d375fa34084fb5703fbe4ee98e64108f3d2235ca"
                },
                {
                        "ImportPath": "gopkg.in/bluesuncorp/validator.v5",
                        "Comment": "v5.8",
                        "Rev": "c06d47f593d786142436a43334f724d819093c04"
                },
                {
                        "ImportPath": "gopkg.in/mgo.v2",
                        "Comment": "r2015.01.24",
                        "Rev": "c6a7dce14133ccac2dcac3793f1d6e2ef048503a"
                }
        ]
}

在线上环境clone项目之后,执行godep restore自动下载依赖包,执行go build即可重新编译Go项目。

此外,采用gopkg.in/mgo.v2作为MongoDB驱动,通过如下方法连接:

var MOGODB_URI = "mongodb://rainy:year@127.0.0.1:27017/your-db-name"
sess, err := mgo.Dial(MOGODB_URI)

参考:

  1. MongoDB安全配置
  2. MongoDB Docs
@rainyear rainyear added the Note label Jul 3, 2015
@rainyear rainyear self-assigned this Jul 3, 2015
@rainyear rainyear added the Go label Jul 25, 2015
@rainyear rainyear mentioned this issue Jul 29, 2015
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant