# GCPにGROWIを構築する 06 MongoDBのユーザ設定
MongoDBのDocker内に入ってユーザを追加する。 本当はdocker-compose設定に書くべきらしいのだが知識不足なので。
# やりたいこと
以下の2ユーザを作成する。
# | ユーザ名 | 説明 | 権限 | 作成するDB |
---|---|---|---|---|
1 | growiadmin | 管理者ユーザ。メンテナンス用途。人間が使用する。すべてのDBに対する権限を持つ。 | userAdminAnyDatabase | admin |
2 | growiuser | GROWIが内部的に使用する。GROWIが必要な最低限のアクセス権のみ付与する。 | readWrite | growi |
growiadmin
は管理者ユーザ。GROWI用MongoDB全てに対してアクセスできるよう、admin
DBにユーザを作る。
MongoDBはDBを複数持ち、DB毎にユーザを作ることができる。
ただしadmin
という名前のDBは特別で、ここに作ったユーザはすべてのDBに対してアクセス権を持つらしい。
バックアップバッチ(次の記事参照)と臨時のメンテナンスで使用する^hontoha。
growiuser
はGROWIシステムがMongoDBにアクセスするために使用する。
そのためgrowi
DBのみにアクセスできればよい。
# 管理者ユーザ追加
MongoDB Docker内に入る。
$ sudo docker-compose exec mongo bash
1
mongoコマンド。
root@082fcc0fb2f2:/# mongo
MongoDB shell version v3.4.19
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.19
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-03-03T13:23:40.961+0000 I STORAGE [initandlisten]
2019-03-03T13:23:40.961+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-03-03T13:23:40.961+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-03-03T13:23:43.045+0000 I CONTROL [initandlisten]
2019-03-03T13:23:43.049+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-03-03T13:23:43.049+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-03-03T13:23:43.049+0000 I CONTROL [initandlisten]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
adminテーブルに移動。
> use admin
switched to db admin
1
2
2
ユーザ検索して、表示されない(いない)ことを確認。
> db.system.users.find()
1
管理者用ユーザ「growadmin」追加。
> db.createUser({
... user:"growiadmin",
... pwd:"(管理者ユーザパスワード)",
... roles:[{ role:"userAdminAnyDatabase", db:"admin" }]
... })
Successfully added user: {
"user" : "growiadmin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
adminテーブルにユーザが作成されたことを確認
> db.system.users.find()
{ "_id" : "admin.growiadmin", "user" : "growiadmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "(秘密)", "storedKey" : "(秘密)", "serverKey" : "(秘密)" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
1
2
2
終了。
> exit
bye
1
2
2
MongoDB Dockerから抜ける。
# exit
exit
1
2
2
# GROWIから接続するユーザを追加
MongoDB Docker内に入る。
$ sudo docker-compose exec mongo bash
1
mongoコマンド。
# mongo
MongoDB shell version v3.4.19
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.19
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-03-03T13:31:42.941+0000 I STORAGE [initandlisten]
2019-03-03T13:31:42.941+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-03-03T13:31:42.941+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-03-03T13:31:46.542+0000 I CONTROL [initandlisten]
2019-03-03T13:31:46.544+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-03-03T13:31:46.544+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-03-03T13:31:46.544+0000 I CONTROL [initandlisten]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ユーザを作ったら、以降権限が必要な操作をする前に認証が必要になる。 管理者で認証。
> db.auth("growiadmin","(管理者ユーザパスワード)")
1
1
2
2
growiテーブルに移動。
> use growi
switched to db growi
1
2
2
GROWI接続用ユーザ「growiuser」作成。
> db.createUser(
... {
... user: "growiuser",
... pwd:"(GROWI接続用ユーザパスワード)",
... roles:[
... {role:"readWrite", db:"growi"}
... ]
... }
... )
Successfully added user: {
"user" : "growiuser",
"roles" : [
{
"role" : "readWrite",
"db" : "growi"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mongo終了。
> exit
bye
1
2
2
MongoDB Dockerを終了。
# exit
exit
1
2
2
# docker-compose.yml 編集
growiuserでつなぐよう、MONGO_URIを書き換える。
services:
app:
environment:
- MONGO_URI=mongodb://growiuser:(GROWI接続用ユーザパスワード)@mongo:27017/growi
1
2
3
4
2
3
4
パスワードベタ書きじゃなきゃならないのかな? わかる方、情報をください。
# Docker再起動
$ sudo docker-compose down
Stopping growi_https-portal_1 ... done
Stopping growi_app_1 ... done
Stopping growi_elasticsearch_1 ... done
Stopping growi_mongo_1 ... done
Removing growi_https-portal_1 ... done
Removing growi_app_1 ... done
Removing growi_elasticsearch_1 ... done
Removing growi_mongo_1 ... done
Removing network growi_default
$ sudo docker-compose up -d
Creating network "growi_default" with the default driver
Creating growi_mongo_1 ... done
Creating growi_elasticsearch_1 ... done
Creating growi_app_1 ... done
Creating growi_https-portal_1 ... done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16