■RDM

※vMotionを使用してRDMファイル(マッピングファイル)を持つ仮想マシンを移行する場合は、参加するすべてのホストでRDMの一貫したLUN IDを維持する必要がある。

・仮想互換モード

仮想マシンとLUN間でやり取りされるSCSI機能をハイパーバイザで処理する。ハイパーバイザがSCSI処理を仲介するため、vSphere機能を使ったスナップショットやクローン機能を実行することができる。クローン機能を実行した場合、RDMディスクの中身は.vmdkファイルとして作成される。

物理環境と互換性があり、ゲストOSやアプリケーションが発行する(REPORT LUNコマンド以外の)SCSI命令を、物理ストレージにそのまま渡すモード。

・物理互換モード(パススルーモード)

仮想マシンとLUN間でやり取りされるSCSI機能にハイパーバイザが仲介しない。そのため、vSphere機能を使ったスナップショットやクローン機能などを実行できない。ゲストOSとファイルシステム間でやり取りされるSCSI機能にベンダ独自の実装などを使用することが可能。(たとえば、ゲストOS内のソフトウェアクラスタリング機能などを使用する場合)

 

参照:「VMware徹底入門第4版 vmware著 翔泳社」、「徹底攻略VCP6.5-DCV VMware vSphere教科書 インプレス社」

■HDLMの場合

HDLMユーザーガイド

・VMFS上のvmdkを各VMにアタッチする場合、HDLMはESXiにインストールする(当然)

HDLMの新規インストール

・RDMを各VMにアタッチする場合、

 

 

 

 

■DMマルチパスドライバの場合

 
 
 

前回のつづき

 

【お題】 DR環境にあるMongoDBに複数データベースのうちの一部だけをレプリケーションできるか?

【環境】

・レプリケーション元:東京 (192.168.101.1) 

とうきょう# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core)

・レプリケーション先:ロスアンジェルス (192.168.101.21)

ろす# cat /etc/centos-release
CentOS Linux release 7.8.2003 (Core)

※MongoDBのバージョンはどっちも前回と同じ4.4

 

■レプリケーション元(東京)

とうきょう# grep -A 7 storage /etc/mongod.conf
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
  directoryPerDB: true
#  engine:
#  wiredTiger:

とうきょう# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since 木 2020-10-01 11:10:02 JST; 1h 45min ago

----(略)----

とうきょう# lvs | grep mongo
  mongodata2    dst Vwi-a-tz-- 200.00g big_thinpool         0.07
  mongodata1    src -wi-ao---- 200.00g

とうきょう# mount | grep mongo
/dev/mapper/src-mongodata1 on /var/lib/mongo/test1 type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/mapper/dst-mongodata2 on /var/lib/mongo/test2 type xfs (rw,relatime,seclabel,attr2,inode64,sunit=1024,swidth=4096,noquota)

とうきょう# ll /var/lib/mongo
合計 140
-rw-------. 1 mongod mongod    47 10月  1 08:39 WiredTiger
-rw-------. 1 mongod mongod    21 10月  1 08:39 WiredTiger.lock
-rw-------. 1 mongod mongod  1258 10月  1 12:41 WiredTiger.turtle
-rw-------. 1 mongod mongod 61440 10月  1 12:41 WiredTiger.wt
-rw-------. 1 mongod mongod  4096 10月  1 11:10 WiredTigerHS.wt
-rw-------. 1 mongod mongod 20480 10月  1 11:10 _mdb_catalog.wt
drwx------. 2 mongod mongod    89 10月  1 08:39 admin
drwx------. 2 mongod mongod   128 10月  1 08:39 config
drwx------. 2 mongod mongod   239 10月  1 12:41 diagnostic.data
drwx------. 2 mongod mongod   110 10月  1 11:10 journal
drwx------. 2 mongod mongod    89 10月  1 08:39 local
-rw-------. 1 mongod mongod     5 10月  1 11:10 mongod.lock
-rw-------. 1 mongod mongod 36864 10月  1 11:12 sizeStorer.wt
-rw-------. 1 mongod mongod   114 10月  1 08:39 storage.bson
drwxr-xr-x. 2 mongod mongod     6 10月  1 08:37 test1
drwxr-xr-x. 2 mongod mongod     6 10月  1 08:59 test2

とうきょう# ll /var/lib/mongo/test1
合計 0
とうきょう# ll /var/lib/mongo/test2
合計 0

とうきょう# mongo
----(略)----
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

→まだ何もいない。

・test1データベースにtest1c1コレクションを作成してデータを書き込む

> use test1
switched to db test1
> db.createCollection( "test1c1" )
{ "ok" : 1 }
> show collections
test1c1
> db.test1c1.insertOne({ name:"ダギャー",age:100 })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f7551b3c56e0d3f3f8944c0")
}

> db.test1c1.find()
{ "_id" : ObjectId("5f7551b3c56e0d3f3f8944c0"), "name" : "ダギャー", "age" : 100 }

・test2データベースにtest2c1コレクションを作成してデータを書き込む

> use test2
switched to db test2
> db.createCollection( "test2c1" )
{ "ok" : 1 }
> show collections
test2c1
> db.test2c1.insertOne({ name:"ぺに助",age:200 })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f7552d1c56e0d3f3f8944c1")
}

> db.test2c1.find()
{ "_id" : ObjectId("5f7552d1c56e0d3f3f8944c1"), "name" : "ぺに助", "age" : 200 }

 

■レプリケーション先(ロスアンジェルス)

ろす#  grep -A 7 storage /etc/mongod.conf
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
  directoryPerDB: true
#  engine:
#  wiredTiger:


ろす# systemctl status mongod | head -3
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since 木 2020-10-01 13:02:08 JST; 10min ago

ろす# mount | grep test1
/dev/loop0 on /var/lib/mongo/test1 type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
ろす# ll /var/lib/mongo
合計 124
-rw-------. 1 mongod mongod    47 10月  1 13:02 WiredTiger
-rw-------. 1 mongod mongod    21 10月  1 13:02 WiredTiger.lock
-rw-------. 1 mongod mongod  1254 10月  1 13:12 WiredTiger.turtle
-rw-------. 1 mongod mongod 61440 10月  1 13:12 WiredTiger.wt
-rw-------. 1 mongod mongod  4096 10月  1 13:02 WiredTigerHS.wt
-rw-------. 1 mongod mongod 20480 10月  1 13:03 _mdb_catalog.wt
drwx------. 2 mongod mongod    87 10月  1 13:02 admin
drwx------. 2 mongod mongod   125 10月  1 13:02 config
drwx------. 2 mongod mongod    71 10月  1 13:12 diagnostic.data
drwx------. 2 mongod mongod   110 10月  1 13:02 journal
drwx------. 2 mongod mongod    87 10月  1 13:02 local
-rw-------. 1 mongod mongod     5 10月  1 13:02 mongod.lock
-rw-------. 1 mongod mongod 20480 10月  1 13:04 sizeStorer.wt
-rw-------. 1 mongod mongod   114 10月  1 13:02 storage.bson
drwxr-xr-x. 2 mongod mongod     6 10月  1 13:09 test1

ろす# ll /var/lib/mongo/test1
合計 0
ろす# mongo
----(略)----
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

→まだ何もいない。

 

■レプリケーション元(東京)からtest1データベースだけをレプリケーション先(ロスアンジェルス)にmongodumpで移行する

とうきょう# pwd
/tmp

とうきょう# mongodump -h 127.0.0.1:27017 -d test1 --out ./test1
2020-10-01T13:22:13.834+0900    writing test1.test1c1 to test1/test1/test1c1.bson
2020-10-01T13:22:13.835+0900    done dumping test1.test1c1 (1 document)

とうきょう# ll
合計 0
drwxr-xr-x. 3 root   root   19  9月 30 08:19 admin
drwxr-xr-x. 3 root   root   19  9月 30 08:20 local
srwx------. 1 mongod mongod  0 10月  1 11:10 mongodb-27017.sock
drwxr-xr-x. 3 root   root   19 10月  1 13:22 test1

とうきょう# ls -lR test1
test1:
合計 0
drwxr-xr-x. 2 root root 55 10月  1 13:22 test1

test1/test1:
合計 8
-rw-r--r--. 1 root root  64 10月  1 13:22 test1c1.bson
-rw-r--r--. 1 root root 154 10月  1 13:22 test1c1.metadata.json

とうきょう# tar cf ./test1.tar ./test1
とうきょう# scp test1.tar 192.168.101.21:/tmp
 

ろす# cd /tmp
ろす# ll test1.tar

-rw-r--r--. 1 root root 10240 10月  1 13:25 test1.tar

ろす# tar xf test1.tar

ろす# ls -lR test1
test1:
合計 0
drwxr-xr-x. 2 root root 55 10月  1 13:22 test1

test1/test1:
合計 8
-rw-r--r--. 1 root root  64 10月  1 13:22 test1c1.bson
-rw-r--r--. 1 root root 154 10月  1 13:22 test1c1.metadata.json

ろす# mongorestore -h 127.0.0.1:27017 --db test1 ./test1/test1
2020-10-01T13:32:38.897+0900    The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2020-10-01T13:32:38.897+0900    building a list of collections to restore from test1/test1 dir
2020-10-01T13:32:38.898+0900    reading metadata for test1.test1c1 from test1/test1/test1c1.metadata.json
2020-10-01T13:32:39.136+0900    restoring test1.test1c1 from test1/test1/test1c1.bson
2020-10-01T13:32:39.138+0900    no indexes to restore
2020-10-01T13:32:39.138+0900    finished restoring test1.test1c1 (1 document, 0 failures)
2020-10-01T13:32:39.138+0900    1 document(s) restored successfully. 0 document(s) failed to restore.

ろす# ll /var/lib/mongo/test1
合計 8
-rw-------. 1 mongod mongod 4096 10月  1 13:32 collection-7-7749723106953056859.wt
-rw-------. 1 mongod mongod 4096 10月  1 13:32 index-8-7749723106953056859.wt

ろす# mongo
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test1   0.000GB

> use test1
switched to db test1

> show collections
test1c1

> db.test1c1.find()
{ "_id" : ObjectId("5f7551b3c56e0d3f3f8944c0"), "name" : "ダギャー", "age" : 100 }     ←オブジェクトIDも保持されている

> exit
bye
 

・念のためレプリケーション先(ロスアンジェルス)のMongodをrestartしてみる。

ろす# systemctl restart mongod
ろす# systemctl status mongod | head -3

● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since 木 2020-10-01 13:37:12 JST; 11s ago

ろす# mongo
----(略)----
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test1   0.000GB

> use test1
switched to db test1
> db.test1c1.find()
{ "_id" : ObjectId("5f7551b3c56e0d3f3f8944c0"), "name" : "ダギャー", "age" : 100 }

 

→複数データベースをもつレプリケーション元からレプリケーション先に一部のデータベースだけをコピーしてもレプリケーション先のMongoDBは正常に動作した。

 

■レプリケーション元(東京)からtest1データベースにデータを追加してレプリケーション先(ロスアンジェルス)にmongodumpで移行する

とうきょう# mongo

----(略)----

> use test1
switched to db test1
> db.test1c1.insertOne({ name:"ぴょろりん",age:0 })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f757043e99a68426998497e")
}

> db.test1c1.find()
{ "_id" : ObjectId("5f7551b3c56e0d3f3f8944c0"), "name" : "ダギャー", "age" : 100 }
{ "_id" : ObjectId("5f757043e99a68426998497e"), "name" : "ぴょろりん", "age" : 0 }

> exit
bye

とうきょう# cd /tmp;pwd
/tmp
とうきょう# ll
合計 12
----(略)----
drwxr-xr-x. 3 root   root      19 10月  1 13:22 test1
-rw-r--r--. 1 root   root   10240 10月  1 13:24 test1.tar

・先ほどのデータを消す

とうきょう# rm -rf test1*

とうきょう# ll test1*
ls: test1* にアクセスできません: そのようなファイルやディレクトリはありません

とうきょう# mongodump -h 127.0.0.1:27017 -d test1 --out ./test1
2020-10-01T15:10:10.389+0900    writing test1.test1c1 to test1/test1/test1c1.bson
2020-10-01T15:10:10.390+0900    done dumping test1.test1c1 (2 documents)

とうきょう# ls -lR test1
test1:
合計 0
drwxr-xr-x. 2 root root 55 10月  1 15:10 test1

test1/test1:
合計 8
-rw-r--r--. 1 root root 125 10月  1 15:10 test1c1.bson          ←少し増えた
-rw-r--r--. 1 root root 154 10月  1 15:10 test1c1.metadata.json    ←さっきと同じサイズ?

→ブロック?単位でデータ量は増えてるかもしれないけどファイルサイズは増えてないのかもしれない?

とうきょう# tar cf ./test1.tar ./test1

とうきょう# scp test1.tar 192.168.101.21:/tmp

 

ろす# pwd
/tmp
ろす# ll
合計 3736
-rw-r--r--. 1 root   root   1048576000 10月  1 13:39 loop1.img
srwx------. 1 mongod mongod          0 10月  1 13:37 mongodb-27017.sock
drwx------. 3 root   root           17  7月 30 15:21 systemd-private-a54e06f5c3b94682b3f9d0fb1afe5255-chronyd.service-Uo7znW
drwxr-xr-x. 3 root   root           19 10月  1 13:22 test1
-rw-r--r--. 1 root   root        10240 10月  1 15:17 test1.tar

・先ほどの展開したデータを消す

ろす# rm -rf test1

ろす# tar xf test1.tar
ろす# ll

合計 3736
-rw-r--r--. 1 root   root   1048576000 10月  1 13:39 loop1.img
srwx------. 1 mongod mongod          0 10月  1 13:37 mongodb-27017.sock
drwx------. 3 root   root           17  7月 30 15:21 systemd-private-a54e06f5c3b94682b3f9d0fb1afe5255-chronyd.service-Uo7znW
drwxr-xr-x. 3 root   root           19 10月  1 15:10 test1
-rw-r--r--. 1 root   root        10240 10月  1 15:17 test1.tar

ろす# ls -lR test1
test1:
合計 0
drwxr-xr-x. 2 root root 55 10月  1 15:10 test1

test1/test1:
合計 8
-rw-r--r--. 1 root root 125 10月  1 15:10 test1c1.bson
-rw-r--r--. 1 root root 154 10月  1 15:10 test1c1.metadata.json

ろす# mongorestore -h 127.0.0.1:27017 --db test1 ./test1/test1
2020-10-01T15:20:40.735+0900    The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2020-10-01T15:20:40.736+0900    building a list of collections to restore from test1/test1 dir
2020-10-01T15:20:40.737+0900    restoring to existing collection test1.test1c1 without dropping
2020-10-01T15:20:40.737+0900    reading metadata for test1.test1c1 from test1/test1/test1c1.metadata.json
2020-10-01T15:20:40.737+0900    restoring test1.test1c1 from test1/test1/test1c1.bson
2020-10-01T15:20:40.741+0900    continuing through error: E11000 duplicate key error collection: test1.test1c1 index: _id_ dup key: { _id: ObjectId('5f7551b3c56e0d3f3f8944c0') }
2020-10-01T15:20:40.741+0900    no indexes to restore
2020-10-01T15:20:40.741+0900    finished restoring test1.test1c1 (1 document, 1 failure)
2020-10-01T15:20:40.741+0900    1 document(s) restored successfully. 1 document(s) failed to restore.

ろす# mongo
----(略)----

> use test1
switched to db test1
> db.test1c1.find()
{ "_id" : ObjectId("5f7551b3c56e0d3f3f8944c0"), "name" : "ダギャー", "age" : 100 }
{ "_id" : ObjectId("5f757043e99a68426998497e"), "name" : "ぴょろりん", "age" : 0 }

 

→オブジェクトIDもそのままの形でコピー成功。

 

■レプリケーション元(東京)からtest1データベースにデータを追加してレプリケーション先(ロスアンジェルス)にオンラインでコピーする

とうきょう# mongo
----(略)----
> use test1
switched to db test1

> db.test1c1.insertOne({ name:"ちんぴょろ",age:5 })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f7576b5a7c88197b00343d2")
}

> db.test1c1.find()
{ "_id" : ObjectId("5f7551b3c56e0d3f3f8944c0"), "name" : "ダギャー", "age" : 100 }
{ "_id" : ObjectId("5f757043e99a68426998497e"), "name" : "ぴょろりん", "age" : 0 }
{ "_id" : ObjectId("5f7576b5a7c88197b00343d2"), "name" : "ちんぴょろ", "age" : 5 }

> exit
bye

とうきょう# cd /var/lib/mongo
とうきょう# ls -la test1
合計 76
drwxr-xr-x. 2 mongod mongod    87 10月  1 12:47 .
drwxr-xr-x. 9 mongod mongod  4096 10月  1 15:28 ..
-rw-------. 1 mongod mongod 36864 10月  1 15:27 collection-0-1184516869936185047.wt
-rw-------. 1 mongod mongod 36864 10月  1 15:27 index-1-1184516869936185047.wt

・dbPath内のファイルを更新時刻順にソート

とうきょう# date;find . -type f -print0 | xargs -0 ls -alt
とうきょう# date;find . -type f -print0 | xargs -0 ls -alt
2020年 10月  1日 木曜日 15:33:32 JST
-rw-------. 1 mongod mongod     14892 10月  1 15:33 ./diagnostic.data/metrics.interim
-rw-------. 1 mongod mongod 104857600 10月  1 15:33 ./journal/WiredTigerLog.0000000005
-rw-------. 1 mongod mongod      1259 10月  1 15:33 ./WiredTiger.turtle
-rw-------. 1 mongod mongod     69632 10月  1 15:33 ./WiredTiger.wt
-rw-------. 1 mongod mongod     12288 10月  1 15:33 ./config/index-6--3250962306205148151.wt

-rw-------. 1 mongod mongod     12288 10月  1 15:31 ./config/index-5--3250962306205148151.wt
-rw-------. 1 mongod mongod    911157 10月  1 15:31 ./diagnostic.data/metrics.2020-10-01T02-10-02Z-00000
-rw-------. 1 mongod mongod     36864 10月  1 15:28 ./sizeStorer.wt
-rw-------. 1 mongod mongod     36864 10月  1 15:27 ./test1/collection-0-1184516869936185047.wt
-rw-------. 1 mongod mongod     36864 10月  1 15:27 ./test1/index-1-1184516869936185047.wt
-rw-------. 1 mongod mongod     12288 10月  1 15:06 ./config/collection-4--3250962306205148151.wt
-rw-------. 1 mongod mongod     36864 10月  1 12:54 ./_mdb_catalog.wt
-rw-------. 1 mongod mongod     20480 10月  1 12:54 ./test2/collection-2-1184516869936185047.wt
-rw-------. 1 mongod mongod     20480 10月  1 12:54 ./test2/index-3-1184516869936185047.wt
-rw-------. 1 mongod mongod     36864 10月  1 11:11 ./local/collection-2--3250962306205148151.wt
-rw-------. 1 mongod mongod     36864 10月  1 11:11 ./local/index-3--3250962306205148151.wt
-rw-------. 1 mongod mongod 104857600 10月  1 11:10 ./journal/WiredTigerPreplog.0000000002
-rw-------. 1 mongod mongod     20480 10月  1 11:10 ./admin/collection-0--3250962306205148151.wt
-rw-------. 1 mongod mongod     20480 10月  1 11:10 ./admin/index-1--3250962306205148151.wt
-rw-------. 1 mongod mongod 104857600 10月  1 11:10 ./journal/WiredTigerPreplog.0000000001
-rw-------. 1 mongod mongod         5 10月  1 11:10 ./mongod.lock
-rw-------. 1 mongod mongod      4096 10月  1 11:10 ./WiredTigerHS.wt
-rw-------. 1 mongod mongod     50041 10月  1 11:10 ./diagnostic.data/metrics.2020-10-01T02-08-34Z-00000
-rw-------. 1 mongod mongod    462975 10月  1 11:08 ./diagnostic.data/metrics.2020-10-01T00-00-14Z-00000
-rw-------. 1 mongod mongod    105717 10月  1 09:00 ./diagnostic.data/metrics.2020-09-30T23-40-14Z-00000
-rw-------. 1 mongod mongod     37966 10月  1 08:40 ./diagnostic.data/metrics.2020-09-30T23-39-07Z-00000
-rw-------. 1 mongod mongod       114 10月  1 08:39 ./storage.bson
-rw-------. 1 mongod mongod        47 10月  1 08:39 ./WiredTiger
-rw-------. 1 mongod mongod        21 10月  1 08:39 ./WiredTiger.lock

→$dbPath/test1配下に以外のファイルも更新されている!

とくじ赤字のファイルはユーザがデータを書き込んでないのに1分以内に更新されてる!?

ちなみに、さっきから前々さわってないし誰もログインしてないロスアンジェルスのMongoDBもこのとおり。

ろす# date;find /var/lib/mongo -type f -print0 | xargs -0 ls -alt
2020年 10月  1日 木曜日 15:37:05 JST
-rw-------. 1 mongod mongod     15520 10月  1 15:37 /var/lib/mongo/diagnostic.data/metrics.interim
-rw-------. 1 mongod mongod 104857600 10月  1 15:36 /var/lib/mongo/journal/WiredTigerLog.0000000002
-rw-------. 1 mongod mongod      1258 10月  1 15:36 /var/lib/mongo/WiredTiger.turtle
-rw-------. 1 mongod mongod     69632 10月  1 15:36 /var/lib/mongo/WiredTiger.wt

-rw-------. 1 mongod mongod    413436 10月  1 15:33 /var/lib/mongo/diagnostic.data/metrics.2020-10-01T04-37-12Z-00000
-rw-------. 1 mongod mongod     36864 10月  1 15:28 /var/lib/mongo/sizeStorer.wt
-rw-------. 1 mongod mongod     36864 10月  1 15:27 /var/lib/mongo/config/index-6-7749723106953056859.wt
-rw-------. 1 mongod mongod     36864 10月  1 15:27 /var/lib/mongo/config/collection-4-7749723106953056859.wt
-rw-------. 1 mongod mongod     36864 10月  1 15:27 /var/lib/mongo/config/index-5-7749723106953056859.wt
-rw-------. 1 mongod mongod     36864 10月  1 15:21 /var/lib/mongo/test1/collection-7-7749723106953056859.wt
-rw-------. 1 mongod mongod     36864 10月  1 15:21 /var/lib/mongo/test1/index-8-7749723106953056859.wt
-rw-------. 1 mongod mongod     36864 10月  1 13:38 /var/lib/mongo/local/collection-2-7749723106953056859.wt
-rw-------. 1 mongod mongod     36864 10月  1 13:38 /var/lib/mongo/local/index-3-7749723106953056859.wt
-rw-------. 1 mongod mongod 104857600 10月  1 13:37 /var/lib/mongo/journal/WiredTigerPreplog.0000000002
-rw-------. 1 mongod mongod     20480 10月  1 13:37 /var/lib/mongo/admin/collection-0-7749723106953056859.wt
-rw-------. 1 mongod mongod     20480 10月  1 13:37 /var/lib/mongo/admin/index-1-7749723106953056859.wt
-rw-------. 1 mongod mongod 104857600 10月  1 13:37 /var/lib/mongo/journal/WiredTigerPreplog.0000000001
-rw-------. 1 mongod mongod         5 10月  1 13:37 /var/lib/mongo/mongod.lock
-rw-------. 1 mongod mongod     36864 10月  1 13:37 /var/lib/mongo/_mdb_catalog.wt
-rw-------. 1 mongod mongod      4096 10月  1 13:37 /var/lib/mongo/WiredTigerHS.wt
-rw-------. 1 mongod mongod    154200 10月  1 13:37 /var/lib/mongo/diagnostic.data/metrics.2020-10-01T04-02-08Z-00000
-rw-------. 1 mongod mongod       114 10月  1 13:02 /var/lib/mongo/storage.bson
-rw-------. 1 mongod mongod        47 10月  1 13:02 /var/lib/mongo/WiredTiger
-rw-------. 1 mongod mongod        21 10月  1 13:02 /var/lib/mongo/WiredTiger.lock

 

まあいっか。とりあえず東京サーバのtest1データベースディレクトリ内のファイルだけオンラインでコピーしてロスアンジェルスに移行してみる。

 

とうきょう# scp /var/lib/mongo/test1/* 192.168.101.21:/var/lib/mongo/test1/
root@192.168.101.21's password:
collection-0-1184516869936185047.wt                                                                                100%   36KB  36.0KB/s   00:00
index-1-1184516869936185047.wt                                                                                      100%   36KB  36.0KB/s   00:00

ろす# ll /var/lib/mongo/test1
合計 144
-rw-------. 1 root   root   36864 10月  1 15:45 collection-0-1184516869936185047.wt
-rw-------. 1 mongod mongod 36864 10月  1 15:21 collection-7-7749723106953056859.wt
-rw-------. 1 root   root   36864 10月  1 15:45 index-1-1184516869936185047.wt
-rw-------. 1 mongod mongod 36864 10月  1 15:21 index-8-7749723106953056859.wt

ろす# rm /var/lib/mongo/test1/{collection-7-7749723106953056859.wt,index-8-7749723106953056859.wt}
rm: 通常ファイル `/var/lib/mongo/test1/collection-7-7749723106953056859.wt' を削除しますか? y
rm: 通常ファイル `/var/lib/mongo/test1/index-8-7749723106953056859.wt' を削除しますか? y

ろす# ll /var/lib/mongo/test1
合計 72
-rw-------. 1 root root 36864 10月  1 15:45 collection-0-1184516869936185047.wt
-rw-------. 1 root root 36864 10月  1 15:45 index-1-1184516869936185047.wt

ろす# chown mongod:mongod /var/lib/mongo/test1/*
ろす# ll /var/lib/mongo/test1

合計 72
-rw-------. 1 mongod mongod 36864 10月  1 15:45 collection-0-1184516869936185047.wt
-rw-------. 1 mongod mongod 36864 10月  1 15:45 index-1-1184516869936185047.wt

ろす# mongo
----(略)----

> use test1
switched to db test1

> db.test1c1.find()
{ "_id" : ObjectId("5f7551b3c56e0d3f3f8944c0"), "name" : "ダギャー", "age" : 100 }
{ "_id" : ObjectId("5f757043e99a68426998497e"), "name" : "ぴょろりん", "age" : 0 }

→やはり「ちんぴょろ」が居ない

甘くないなw

・mongodbのログ見てみる

ろす# cd /var/log/mongodb/
ろす# ls -ltr
合計 56
-rw-r-----. 1 mongod mongod 56066 10月  1 15:56 mongod.log

ろす# tail -100  /var/log/mongodb/mongod.log

----(略)----

{"t":{"$date":"2020-10-01T15:21:10.015+09:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn5","msg":"client metadata","attr":{"remote":"127.0.0.1:55912","client":"conn5","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.0"},"os":{"type":"Linux","name":"CentOS Linux release 7.8.2003 (Core)","architecture":"x86_64","version":"Kernel 3.10.0-1127.18.2.el7.x86_64"}}}}
{"t":{"$date":"2020-10-01T15:25:16.138+09:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn5","msg":"connection ended","attr":{"remote":"127.0.0.1:55912","connectionCount":0}}
{"t":{"$date":"2020-10-01T15:52:41.452+09:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"connection accepted","attr":{"remote":"127.0.0.1:55914","sessionId":6,"connectionCount":1}}
{"t":{"$date":"2020-10-01T15:52:41.452+09:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn6","msg":"client metadata","attr":{"remote":"127.0.0.1:55914","client":"conn6","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.0"},"os":{"type":"Linux","name":"CentOS Linux release 7.8.2003 (Core)","architecture":"x86_64","version":"Kernel 3.10.0-1127.18.2.el7.x86_64"}}}}
{"t":{"$date":"2020-10-01T15:56:27.136+09:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn6","msg":"connection ended","attr":{"remote":"127.0.0.1:55914","connectionCount":0}}

ろす# egrep -i 'err|warn'  /var/log/mongodb/mongod.log

----(略)----

{"t":{"$date":"2020-10-01T13:37:12.243+09:00"},"s":"W",  "c":"CONTROL",  "id":22178,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
{"t":{"$date":"2020-10-01T13:37:12.243+09:00"},"s":"W",  "c":"CONTROL",  "id":22181,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
{"t":{"$date":"2020-10-01T15:20:40.743+09:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn2","msg":"Interrupted operation as its client disconnected","attr":{"opId":77869}}

・ロサンジェルスのMongoDBをリスタートしてみる。

ろす# systemctl restart mongod
ろす# systemctl status mongod

● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since 木 2020-10-01 16:02:21 JST; 6s ago
     Docs: https://docs.mongodb.org/manual
  Process: 7949 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 7947 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 7945 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 7943 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 7952 (mongod)
    Tasks: 33
   Memory: 157.3M
   CGroup: /system.slice/mongod.service
           mq7952 /usr/bin/mongod -f /etc/mongod.conf

10月 01 16:02:20 pc2_centos7_k1 systemd[1]: Stopped MongoDB Database Server.
10月 01 16:02:20 pc2_centos7_k1 systemd[1]: Starting MongoDB Database Server...
10月 01 16:02:20 pc2_centos7_k1 mongod[7949]: about to fork child process, waiting until server is ready for connections.
10月 01 16:02:20 pc2_centos7_k1 mongod[7949]: forked process: 7952
10月 01 16:02:21 pc2_centos7_k1 systemd[1]: Started MongoDB Database Server.

→起動した。

ろす# mongo

MongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e9fce24e-a5e8-4e8e-9b14-627362678a17") }
MongoDB server version: 4.4.0
---
The server generated these startup warnings when booting:
        2020-10-01T16:02:20.310+09:00: ***** SERVER RESTARTED *****
        2020-10-01T16:02:21.443+09:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2020-10-01T16:02:21.444+09:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2020-10-01T16:02:21.444+09:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> use test1
switched to db test1
> db.test1c1.find()
Error: error doing query: failed: network error while attempting to run command 'find' on host '127.0.0.1:27017'
> show collections
Error: socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused] :
runClientFunctionWithRetries@src/mongo/shell/session.js:361:27
runCommand@src/mongo/shell/session.js:455:25
DB.prototype._runCommandImpl@src/mongo/shell/db.js:147:12
DB.prototype.runCommand@src/mongo/shell/db.js:162:16
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:805:17
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:855:16
shellHelper.show@src/mongo/shell/utils.js:919:9
shellHelper@src/mongo/shell/utils.js:814:15
@(shellhelp2):1:1

> exit
bye
{"t":{"$date":"2020-10-01T07:06:21.612Z"},"s":"I",  "c":"QUERY",    "id":22791,   "ctx":"js","msg":"Failed to end logical session","attr":{"lsid":{"id":{"$uuid":"e9fce24e-a5e8-4e8e-9b14-627362678a17"}},"error":{"code":9001,"codeName":"SocketException","errmsg":"socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused]"}}}

ろす# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 木 2020-10-01 16:03:18 JST; 3min 36s ago
     Docs: https://docs.mongodb.org/manual
  Process: 7949 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 7947 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 7945 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 7943 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 7952 (code=exited, status=14)

10月 01 16:02:20 pc2_centos7_k1 systemd[1]: Stopped MongoDB Database Server.
10月 01 16:02:20 pc2_centos7_k1 systemd[1]: Starting MongoDB Database Server...
10月 01 16:02:20 pc2_centos7_k1 mongod[7949]: about to fork child process, waiting until server is ready for connections.
10月 01 16:02:20 pc2_centos7_k1 mongod[7949]: forked process: 7952
10月 01 16:02:21 pc2_centos7_k1 systemd[1]: Started MongoDB Database Server.
10月 01 16:03:18 pc2_centos7_k1 systemd[1]: mongod.service: main process exited, code=exited, status=14/n/a
10月 01 16:03:18 pc2_centos7_k1 systemd[1]: Unit mongod.service entered failed state.
10月 01 16:03:18 pc2_centos7_k1 systemd[1]: mongod.service failed.

→いったん起動したけど、そのあと落ちた

 

なんか駄目臭いw

 

ろす# egrep -i 'err|warn'  /var/log/mongodb/mongod.log

----(略)----

{"t":{"$date":"2020-10-01T15:20:40.743+09:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn2","msg":"Interrupted operation as its client disconnected","attr":{"opId":77869}}
{"t":{"$date":"2020-10-01T16:02:20.072+09:00"},"s":"I",  "c":"CONTROL",  "id":23377,   "ctx":"SignalHandler","msg":"Received signal","attr":{"signal":15,"error":"Terminated"}}
{"t":{"$date":"2020-10-01T16:02:20.089+09:00"},"s":"I",  "c":"-",        "id":4695300, "ctx":"SignalHandler","msg":"Interrupted all currently running operations","attr":{"opsKilled":3}}
{"t":{"$date":"2020-10-01T16:02:20.310+09:00"},"s":"W",  "c":"CONTROL",  "id":20698,   "ctx":"main","msg":"***** SERVER RESTARTED *****","tags":["startupWarnings"]}
{"t":{"$date":"2020-10-01T16:02:21.443+09:00"},"s":"W",  "c":"CONTROL",  "id":22120,   "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
{"t":{"$date":"2020-10-01T16:02:21.444+09:00"},"s":"W",  "c":"CONTROL",  "id":22178,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
{"t":{"$date":"2020-10-01T16:02:21.444+09:00"},"s":"W",  "c":"CONTROL",  "id":22181,   "ctx":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'","tags":["startupWarnings"]}
{"t":{"$date":"2020-10-01T16:03:18.745+09:00"},"s":"E",  "c":"STORAGE",  "id":22435,   "ctx":"conn1","msg":"WiredTiger error","attr":{"error":2,"message":"[1601535798:745911][7952:0x7f4baa454700], file:test1/collection-7-7749723106953056859.wt, WT_SESSION.open_cursor: __posix_open_file, 806: /var/lib/mongo/test1/collection-7-7749723106953056859.wt: handle-open: open: No such file or directory"}}
{"t":{"$date":"2020-10-01T16:03:18.746+09:00"},"s":"F",  "c":"STORAGE",  "id":50883,   "ctx":"conn1","msg":"Cursor not found","attr":{"error":{"code":43,"codeName":"CursorNotFound","errmsg":"Failed to open a WiredTiger cursor. Reason: UnknownError: 2: No such file or directory, uri: table:test1/collection-7-7749723106953056859, config: "}}}

 

■レプリケーション元(東京)からレプリケーション先(ロスアンジェルス)にオフラインでコピーする

 

【検証パターン】

※データ転送時に東京とロスアンジェルスのmongodを停止しておく

①dbPath(デフォルトでは/var/lib/mongo)直下のdbPath/test1ディレクトリの中身だけを東京からロスアンジェルスへrsyncする。

 DbPath=/var/lib/mongo

 rsync -auv ${DbPath}/test1/* 192.168.101.21:${DbPath}/test1/

②dbPath直下の全ファイルからdbPath/test2ディレクトリを除いた分だけを東京からロスアンジェルスへrsyncする。

 DbPath=/var/lib/mongo

 rsync -auv --exclude='/var/lib/mongo/test2/' ${DbPath}/* 192.168.101.21:${DbPath}/

 

【結果】

①ロスアンジェルスでmongod起動失敗

②ロスアンジェルスでmongod起動成功。ロスアンジェルスでtest1データベース内データの参照成功

 

■レプリケーション元(東京)からレプリケーション先(ロスアンジェルス)にオンラインでコピーする

※データ転送時に東京とロスアンジェルスのmongodを起動しておく

 

【検証パターン】

③dbPath(デフォルトでは/var/lib/mongo)直下のdbPath/test1ディレクトリの中身だけを東京からロスアンジェルスへrsyncする。

④dbPath直下の全ファイルからdbPath/test2ディレクトリを除いた分だけを東京からロスアンジェルスへrsyncする。

 

【結果】

③ロスアンジェルスでmongod起動失敗

④ロスアンジェルスでmongod起動失敗

 

 

 

 

【お題】1インスタンスのMongoDBで、DB毎にデータファイルを格納するディレクトリを分ける。

【参照】

1.https://groups.google.com/forum/#!topic/mongodb-jp/ElKQd-Wlx2U

2.MongoDB公式マニュアル Storage Options

 

 

【環境】

# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core)

# mongod --version
db version v4.4.1
Build Info: {
    "version": "4.4.1",
    "gitVersion": "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1",
    "openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel70",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

# mongo --version
MongoDB shell version v4.4.1
Build Info: {
    "version": "4.4.1",
    "gitVersion": "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1",
    "openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel70",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

 

【準備】

・なんとなくこれをしちゃう

# setenforce 0

・下記のように2つのブロックデバイスを作成する。

# lvcreate -L 200g src/mongodata1
# lvcreate -V 200g -T dst/big_thinpool -n mongodata2
# lvs

  LV             VG  Attr         LSize    Pool          Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home          cl  -wi-ao---- 173.87g
  root           cl  -wi-ao----  50.00g
  swap           cl  -wi-ao----   7.81g
  big_thinpool   dst twi-aotz--    2.44t                        0.00   0.43
  mongodata2  dst Vwi-a-tz-- 200.00g big_thinpool          0.00
  testlv_bk      dst -wi-ao---- 200.00g
  mondodata1  src -wi-a----- 200.00g
  test_thinpool src twi-aotz--  400.00g                        18.09  26.40
  testlv         src -wi-ao---- 200.00g
  thinlv_1       src Vwi-aotz-- 200.00g test_thinpool         35.38

・せっかくだから、「/」にマウントしてる/dev/cl/rootのスナップショットをとっておいてロールバックできるようにしとくか

# lvreduce -L 100g cl/home

# lvcreate -s -L 50g -n root_snap cl/root

# lvs
  LV            VG   Attr         LSize    Pool     Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home          cl  -wi-ao---- 100.00g
  root           cl  owi-aos---  50.00g
  root_snap     cl  swi-a-s---  50.00g               root    0.00
  swap           cl  -wi-ao----   7.81g
  big_thinpool  dst  twi-aotz--    2.44t                        0.00   0.43
  mongodata2 dst  Vwi-a-tz-- 200.00g big_thinpool        0.00
  testlv_bk     dst  -wi-ao---- 200.00g
  mondodata  src  -wi-a----- 200.00g
  test_thinpool src twi-aotz--  400.00g                       18.09  26.40
  testlv        src -wi-ao----  200.00g
  thinlv_1      src Vwi-aotz--  200.00g test_thinpool       35.38

 

・MongoDBはインストールした直後でデータベースはまだ作ってない

# mongo

----(略)----

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

# cat /etc/mongod.conf
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
#

# ll /var/lib/mongo
合計 216
-rw-------. 1 mongod mongod    47  9月 29 17:29 WiredTiger
-rw-------. 1 mongod mongod    21  9月 29 17:29 WiredTiger.lock
-rw-------. 1 mongod mongod  1259  9月 30 06:50 WiredTiger.turtle
-rw-------. 1 mongod mongod 61440  9月 30 06:50 WiredTiger.wt
-rw-------. 1 mongod mongod  4096  9月 29 17:29 WiredTigerHS.wt
-rw-------. 1 mongod mongod 20480  9月 29 17:30 _mdb_catalog.wt
-rw-------. 1 mongod mongod 20480  9月 29 17:30 collection-0-4168443414840855323.wt
-rw-------. 1 mongod mongod 20480  9月 29 17:30 collection-2-4168443414840855323.wt
-rw-------. 1 mongod mongod  4096  9月 29 17:29 collection-4-4168443414840855323.wt

drwx------. 2 mongod mongod    71  9月 30 06:50 diagnostic.data
-rw-------. 1 mongod mongod 20480  9月 29 17:30 index-1-4168443414840855323.wt
-rw-------. 1 mongod mongod 20480  9月 29 17:30 index-3-4168443414840855323.wt
-rw-------. 1 mongod mongod  4096  9月 29 17:29 index-5-4168443414840855323.wt
-rw-------. 1 mongod mongod  4096  9月 29 17:29 index-6-4168443414840855323.wt

drwx------. 2 mongod mongod   110  9月 29 17:29 journal
-rw-------. 1 mongod mongod     5  9月 29 17:29 mongod.lock
-rw-------. 1 mongod mongod 20480  9月 29 17:31 sizeStorer.wt
-rw-------. 1 mongod mongod   114  9月 29 17:29 storage.bson

→初期状態から存在するデータベース「admin」、「config」、「local」内のコレクションやインデックスファイルが既に居る。

> use admin
> show collections

system.version
> use config
switched to db config
> show collections
system.sessions
> use local
switched to db local
> show collections
startup_log

→初期データベース「admin」、「config」、「local」内にそれぞれ1つずつコレクションが居る。

 

# cat /usr/lib/systemd/system/mongod.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target

 

【dbPathにデータファールを作る】

普通にdbPathにデータファイルを置くようなdbをつくる

> use test0
switched to db test0
> db. createCollection( "test0_0" )
{ "ok" : 1 }
> show collections
test0_0

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test0   0.000GB

# ll /var/lib/mongo
合計 372
-rw-------. 1 mongod mongod    47  9月 29 17:29 WiredTiger
-rw-------. 1 mongod mongod    21  9月 29 17:29 WiredTiger.lock
-rw-------. 1 mongod mongod  1259  9月 30 07:23 WiredTiger.turtle
-rw-------. 1 mongod mongod 69632  9月 30 07:23 WiredTiger.wt
-rw-------. 1 mongod mongod  4096  9月 29 17:29 WiredTigerHS.wt
-rw-------. 1 mongod mongod 36864  9月 30 07:22 _mdb_catalog.wt
-rw-------. 1 mongod mongod 20480  9月 29 17:30 collection-0-4168443414840855323.wt
-rw-------. 1 mongod mongod 20480  9月 29 17:30 collection-2-4168443414840855323.wt
-rw-------. 1 mongod mongod 20480  9月 30 07:20 collection-4-4168443414840855323.wt
-rw-------. 1 mongod mongod  4096  9月 30 07:22 collection-7-4168443414840855323.wt
drwx------. 2 mongod mongod    71  9月 30 07:24 diagnostic.data
-rw-------. 1 mongod mongod 20480  9月 29 17:30 index-1-4168443414840855323.wt
-rw-------. 1 mongod mongod 20480  9月 29 17:30 index-3-4168443414840855323.wt
-rw-------. 1 mongod mongod 20480  9月 30 07:20 index-5-4168443414840855323.wt
-rw-------. 1 mongod mongod 20480  9月 30 07:20 index-6-4168443414840855323.wt
-rw-------. 1 mongod mongod  4096  9月 30 07:22 index-8-4168443414840855323.wt
drwx------. 2 mongod mongod   110  9月 29 17:29 journal
-rw-------. 1 mongod mongod     5  9月 29 17:29 mongod.lock
-rw-------. 1 mongod mongod 36864  9月 30 07:23 sizeStorer.wt
-rw-------. 1 mongod mongod   114  9月 29 17:29 storage.bson

 

・なんとなくバックアップ

#pwd

/tmp

# mongodump -h 127.0.0.1:27017 -d test0 --out all
2020-09-30T07:32:21.531+0900    writing test0.test0_0 to all/test0/test0_0.bson
2020-09-30T07:32:21.532+0900    done dumping test0.test0_0 (0 documents)

# ll
合計 0
drwxr-xr-x. 3 root   root   19  9月 30 07:32 all
srwx------. 1 mongod mongod  0  9月 29 17:29 mongodb-27017.sock

# ll all
合計 0
drwxr-xr-x. 2 root root 55  9月 30 07:32 test0

# ll all/test0/
合計 4
-rw-r--r--. 1 root root   0  9月 30 07:32 test0_0.bson
-rw-r--r--. 1 root root 154  9月 30 07:32 test0_0.metadata.json

 

・上記の【参照】の1.に従って

To change the --directoryperdb option for existing deployments:

  • For standalone instances:
    1. Use mongodump on the existing mongod instance to generate a backup.
    2. Stop the mongod instance.
    3. Add the --directoryperdb value and configure a new data directory
    4. Restart the mongod instance.
    5. Use mongorestore to populate the new data directory.

のとおりやるぷに。

 

# systemctl stop mongod

# cp -p /etc/{mongod.conf,mongod.conf.org}

# diff /etc/mongod.conf.org /etc/mongod.conf
16a17
>   directoryPerDB: true

# systemctl start mongod

Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.

# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 水 2020-09-30 07:51:54 JST; 3min 12s ago
     Docs: https://docs.mongodb.org/manual
  Process: 5306 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=100)
  Process: 5302 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 5299 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 5296 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 4363 (code=exited, status=0/SUCCESS)

 9月 30 07:51:54 localhost.localdomain systemd[1]: Starting MongoDB Database Server...
 9月 30 07:51:54 localhost.localdomain mongod[5306]: about to fork child process, waiting until server is ready for connections.
 9月 30 07:51:54 localhost.localdomain mongod[5306]: forked process: 5308
 9月 30 07:51:54 localhost.localdomain mongod[5306]: ERROR: child process failed, exited with 100
 9月 30 07:51:54 localhost.localdomain mongod[5306]: To see additional information in this output, start without the "--fork" option.
 9月 30 07:51:54 localhost.localdomain systemd[1]: mongod.service: control process exited, code=exited status=100
 9月 30 07:51:54 localhost.localdomain systemd[1]: Failed to start MongoDB Database Server.
 9月 30 07:51:54 localhost.localdomain systemd[1]: Unit mongod.service entered failed state.
 9月 30 07:51:54 localhost.localdomain systemd[1]: mongod.service failed.

→てへぺろ

# cat /var/log/mongodb/mongod.log

----(略)----

{"t":{"$date":"2020-09-30T08:04:17.226+09:00"},"s":"E",  "c":"STORAGE",  "id":20557,   "ctx":"initandlisten","msg":"DBException in initAndListen, terminating","attr":{"error":"InvalidOptions: Requested option conflicts with current storage engine option for directoryPerDB; you requested true but the current server storage is already set to false and cannot be changed"}}

----(略)----

ぐぐったら、

インストール直後に生成されているdbPath内のファイルやディレクトリ構成が「storage.directoryPerDB: false」になっててmongod.confと矛盾してる的な感じ?

https://www.reddit.com/r/mongodb/comments/blct8a/startup_exception_requested_option_conflicts_with/

 

/var/lib/mongoの中身を全部削除してmongodを起動

下記のように、「admin」、「config」、「local」ディレクトリが自動的につくられた。

それぞれのディレクトリの中身は、それぞれのデータベースのcollectionとindexファイルとなっていた。

# ll /var/lib/mongo
-rw-------. 1 mongod mongod    47  9月 30 08:39 WiredTiger
-rw-------. 1 mongod mongod    21  9月 30 08:39 WiredTiger.lock
-rw-------. 1 mongod mongod  1261  9月 30 19:27 WiredTiger.turtle
-rw-------. 1 mongod mongod 86016  9月 30 19:27 WiredTiger.wt
-rw-------. 1 mongod mongod  4096  9月 30 09:37 WiredTigerHS.wt
-rw-------. 1 mongod mongod 36864  9月 30 09:38 _mdb_catalog.wt
drwx------. 2 mongod mongod    87  9月 30 08:39 admin
drwx------. 2 mongod mongod   125  9月 30 08:39 config
drwx------. 2 mongod mongod   155  9月 30 19:27 diagnostic.data
drwx------. 2 mongod mongod   110  9月 30 09:37 journal
drwx------. 2 mongod mongod    87  9月 30 08:39 local
-rw-------. 1 mongod mongod     5  9月 30 09:37 mongod.lock
-rw-------. 1 mongod mongod 36864  9月 30 10:13 sizeStorer.wt
-rw-------. 1 mongod mongod   114  9月 30 08:39 storage.bson

mongoを実行して、test1データベースを作成して、適当なコレクションを1つ作成

# ll /var/lib/mongo
-rw-------. 1 mongod mongod    47  9月 30 08:39 WiredTiger
-rw-------. 1 mongod mongod    21  9月 30 08:39 WiredTiger.lock
-rw-------. 1 mongod mongod  1261  9月 30 19:27 WiredTiger.turtle
-rw-------. 1 mongod mongod 86016  9月 30 19:27 WiredTiger.wt
-rw-------. 1 mongod mongod  4096  9月 30 09:37 WiredTigerHS.wt
-rw-------. 1 mongod mongod 36864  9月 30 09:38 _mdb_catalog.wt
drwx------. 2 mongod mongod    87  9月 30 08:39 admin
drwx------. 2 mongod mongod   125  9月 30 08:39 config
drwx------. 2 mongod mongod   155  9月 30 19:27 diagnostic.data
drwx------. 2 mongod mongod   110  9月 30 09:37 journal
drwx------. 2 mongod mongod    87  9月 30 08:39 local
-rw-------. 1 mongod mongod     5  9月 30 09:37 mongod.lock
-rw-------. 1 mongod mongod 36864  9月 30 10:13 sizeStorer.wt
-rw-------. 1 mongod mongod   114  9月 30 08:39 storage.bson
drwxr-xr-x. 2 mongod mongod   169  9月 30 09:24 test1

上記のようにtest1ディレクトリが勝手に作られた。中身はcollectionとindexファイルだった。

 

# mkdir /var/lib/mongo/test2

# chown mongod:mongod  /var/lib/mongo/test2

# mount /dev/dst/mongodata2 /var/lib/mongo/test2

を実行した後、mongoを実行して、test2データベースを作成し、適当なコレクションを作成したところ、test2ディレクトリ内にcollectionとindexファイルが作られた。

 

【わかったこと】

・mongod.confに、「storage.directoryPerDB: true」 を追記して、dbPath(/var/lib/mongo)を空にして、mongodを再起動すると、dbPathディレクトリのサブディレクトリにデータベース名と同じサブディレクトリを構成して、これらのディレクトリないにデータベースファイルが置かれる構成になる。

・dbPathディレクトリ内にデータベース名のサブディレクトリが居ない状態で、データベースを作成すると、dbPathにデータベース名と同じサブディレクトリが自動的に生成し、このサブディレクトリ内にデータベースファイルが作られる

・dbPathディレクトリ内にデータベース名のサブディレクトリをあらかじめ作成しておいてから、データベースを作成すると、データベース名と同じサブディレクトリ内にデータベースファイルが作られる

 ※つまり、データベースを作成する前に、dbPathディレクトリ内にデータベース名と同じサブディレクトリを作成して、このサブディレクトリをマウントポイントとしてブロックデバイスファイルをマウントするとこのデータベースファイルは指定したブロックデバイス内に保存されるようになる。