签到成功

知道了

CNDBA社区CNDBA社区

MongoDB 配置 每个DB 使用独立的目录结构

2022-05-07 14:44 1731 0 原创 MongoDB
作者: dave

1 配置说明


在之前的博客,我们了解了MongoDB的目录结构:

http://www.cndba.cn/cndba/dave/article/107980

Mongodb 目录结构 和 数据文件 说明
https://www.cndba.cn/dave/article/107955

默认情况下,所有数据库的数据文件都存放在dbPath指定的目录下:http://www.cndba.cn/cndba/dave/article/107980

[dave@www.cndba.cn_4 etc]# cat mongo.conf
……
storage:
  journal:
    enabled: true
  dbPath: "/data/mongodb/data"
……

[dave@www.cndba.cn_4 etc]# tree /data/mongodb/data
/data/mongodb/data
├── collection-0--5156552241969425662.wt
……
├── collection-8--5156552241969425662.wt
├── collection-8--8751185035608474843.wt
├── diagnostic.data
│   ├── metrics.2022-05-01T03-51-01Z-00000
……
├── WiredTigerHS.wt
├── WiredTiger.lock
├── WiredTiger.turtle
└── WiredTiger.wt

2 directories, 65 files
[dave@www.cndba.cn_4 etc]#

当实例中数据库较多的时候,目录就会比较乱。 所以在生产环境,建议配置每个DB使用独立的目录。

配置参数是:storage.directoryPerDB,该值默认为false。当设置为true后,会在storage.dbPath 参数指定的目录下,为每个库使用独立的子目录。

http://www.cndba.cn/cndba/dave/article/107980

因为这里涉及到数据文件路径改变的问题,所以建议在环境上线之前进行修改。 如果是对现网的环境进行变更,对两种不同的环境,操作方法也有所不同。

For standalone instanceshttp://www.cndba.cn/cndba/dave/article/107980http://www.cndba.cn/cndba/dave/article/107980

  1. Use mongodump on the existing mongod instance to generate a backup.
  2. Stop the mongod instance.
  3. Add the storage.directoryPerDB value and configure a new data directory
  4. Restart the mongod instance.
  5. Use mongorestore to populate the new data directory.

For replica sets:http://www.cndba.cn/cndba/dave/article/107980http://www.cndba.cn/cndba/dave/article/107980

  1. Stop a secondary member.
  2. Add the storage.directoryPerDB value and configure a new data directory to that secondary member.
  3. Restart that secondary.
  4. Use initial sync to populate the new data directory.
  5. Update remaining secondaries in the same fashion.
  6. Step down the primary, and update the stepped-down member in the same fashion.

注意这里,第二步,需要指定新的路径,或者对原路径进行重命名备份,否则当参数改成true以后,实例无法启动,会报如下错误:

{“t”:{“$date”:”2022-05-07T14:27:13.884+08: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”}}http://www.cndba.cn/cndba/dave/article/107980

2 操作示例


我们这里在3节点的副本集环境进行操作演示。

2.1 修改之前的目录结构

[dave@www.cndba.cn_1 shard1]# ll
total 10584
-rw------- 1 root root   45056 May  7 11:57 collection-0-3182346862009094560.wt
-rw------- 1 root root   20480 May  7 11:56 collection-0--3819260641954839710.wt
-rw------- 1 root root 7286784 May  7 14:16 collection-10-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:56 collection-11-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:56 collection-13-3182346862009094560.wt
-rw------- 1 root root   20480 May  7 11:56 collection-15-3182346862009094560.wt
-rw------- 1 root root   24576 May  7 11:56 collection-17-3182346862009094560.wt
-rw------- 1 root root   12288 May  5 23:46 collection-21-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 14:16 collection-2-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:56 collection-23-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:57 collection-26-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 14:15 collection-30-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:56 collection-32-3182346862009094560.wt
-rw------- 1 root root  102400 May  7 11:56 collection-35-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 14:16 collection-4-3182346862009094560.wt
-rw------- 1 root root   20480 May  7 10:21 collection-45-3182346862009094560.wt
-rw------- 1 root root  479232 May  5 23:46 collection-59--3819260641954839710.wt
-rw------- 1 root root    4096 May  7 10:21 collection-61--3819260641954839710.wt
-rw------- 1 root root   36864 May  7 11:56 collection-6-3182346862009094560.wt
-rw------- 1 root root    4096 May  7 10:21 collection-63--3819260641954839710.wt
-rw------- 1 root root    4096 May  5 23:46 collection-65--3819260641954839710.wt
-rw------- 1 root root   36864 May  7 11:56 collection-8-3182346862009094560.wt
drwx------ 2 root root    4096 May  7 14:17 diagnostic.data
-rw------- 1 root root   20480 May  4 15:39 index-12-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:57 index-1-3182346862009094560.wt
-rw------- 1 root root   20480 May  5 23:46 index-1--3819260641954839710.wt
-rw------- 1 root root   36864 May  7 11:56 index-14-3182346862009094560.wt
-rw------- 1 root root   20480 May  4 15:39 index-16-3182346862009094560.wt
-rw------- 1 root root   12288 May  7 10:21 index-18-3182346862009094560.wt
-rw------- 1 root root   24576 May  5 23:46 index-22-3182346862009094560.wt
-rw------- 1 root root   20480 May  5 23:46 index-2--3819260641954839710.wt
-rw------- 1 root root   36864 May  5 23:46 index-24-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:56 index-25-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:56 index-28-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 12:02 index-31-3182346862009094560.wt
-rw------- 1 root root   20480 May  7 11:56 index-3-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 11:56 index-33-3182346862009094560.wt
-rw------- 1 root root   36864 May  7 14:15 index-34-3182346862009094560.wt
-rw------- 1 root root   86016 May  4 15:39 index-36-3182346862009094560.wt
-rw------- 1 root root   53248 May  7 11:56 index-37-3182346862009094560.wt
-rw------- 1 root root   20480 May  4 15:39 index-46-3182346862009094560.wt
-rw------- 1 root root   20480 May  7 10:21 index-47-3182346862009094560.wt
-rw------- 1 root root   20480 May  4 15:39 index-5-3182346862009094560.wt
-rw------- 1 root root  716800 May  5 23:46 index-60--3819260641954839710.wt
-rw------- 1 root root    4096 May  5 23:46 index-62--3819260641954839710.wt
-rw------- 1 root root    4096 May  5 23:46 index-64--3819260641954839710.wt
-rw------- 1 root root    4096 May  5 23:46 index-66--3819260641954839710.wt
-rw------- 1 root root  618496 May  5 10:47 index-67--3819260641954839710.wt
-rw------- 1 root root   20480 May  4 15:39 index-7-3182346862009094560.wt
-rw------- 1 root root   20480 May  7 11:56 index-9-3182346862009094560.wt
drwx------ 2 root root     110 May  7 11:57 journal
-rw------- 1 root root   36864 May  7 11:56 _mdb_catalog.wt
-rw------- 1 root root       6 May  7 11:56 mongod.lock
-rw------- 1 root root   36864 May  7 14:16 sizeStorer.wt
-rw------- 1 root root     114 May  3 23:04 storage.bson
-rw-r--r-- 1 root root   10854 May  4 16:24 super.log
-rw------- 1 root root      50 May  3 23:04 WiredTiger
-rw------- 1 root root   36864 May  7 14:16 WiredTigerHS.wt
-rw------- 1 root root      21 May  3 23:04 WiredTiger.lock
-rw------- 1 root root    1489 May  7 14:16 WiredTiger.turtle
-rw------- 1 root root  274432 May  7 14:16 WiredTiger.wt
[dave@www.cndba.cn_1 shard1]#

2.2 修改参数

关闭shard1 库,并修改参数。

[dave@www.cndba.cn_1 shard1]# supervisorctl stop shard1
shard1: stopped

备份旧目录:

[dave@www.cndba.cn_1 data]# mv shard1 shard1_bak
[dave@www.cndba.cn_1 data]# mkdir shard1

[dave@www.cndba.cn_1 etc]# cat shard1.conf
……
storage:
  journal:
    enabled: true
  dbPath: "/data/mongodb/data/shard1"
  oplogMinRetentionHours: 150
  directoryPerDB: true
……
[dave@www.cndba.cn_1 etc]#

[dave@www.cndba.cn_1 data]# supervisorctl start shard1
shard1: started
[dave@www.cndba.cn_1 data]#

2.3 同步数据

Resync a Member of a Replica Set
https://www.mongodb.com/docs/manual/tutorial/resync-replica-set-member/

http://www.cndba.cn/cndba/dave/article/107980

MongoDB 启动会自动同步,如果库比较大,同步时间会比较长,因为需要调整oplog的大小,保证oplog 保留数据的时间大于同步需要的时间。

MongoDB 4.4 中 journal 和 oplog 日志 说明
https://www.cndba.cn/dave/article/107979

我们库小, 同步之后新的目录机构如下:

[dave@www.cndba.cn_1 shard1]# pwd
/data/mongodb/data/shard1
[dave@www.cndba.cn_1 shard1]# ll
total 584
drwx------ 2 root root    333 May  7 14:31 admin
drwx------ 2 root root    128 May  7 14:31 cndba
drwx------ 2 root root   4096 May  7 14:31 config
drwx------ 2 root root     71 May  7 14:38 diagnostic.data
drwx------ 2 root root    110 May  7 14:31 journal
drwx------ 2 root root   4096 May  7 14:31 local
-rw------- 1 root root  36864 May  7 14:31 _mdb_catalog.wt
-rw------- 1 root root      6 May  7 14:31 mongod.lock
-rw------- 1 root root  36864 May  7 14:37 sizeStorer.wt
-rw------- 1 root root    114 May  7 14:31 storage.bson
-rw-r--r-- 1 root root      0 May  7 14:31 super.log
-rw------- 1 root root     50 May  7 14:31 WiredTiger
-rw------- 1 root root  36864 May  7 14:37 WiredTigerHS.wt
-rw------- 1 root root     21 May  7 14:31 WiredTiger.lock
-rw------- 1 root root   1474 May  7 14:37 WiredTiger.turtle
-rw------- 1 root root 241664 May  7 14:37 WiredTiger.wt
[dave@www.cndba.cn_1 shard1]#

然后逐个在其他secondary节点上进行操作,Primary 节点要先进行step-down 降级之后再处理。

生产环境在线变更还是有一定影响,最好的方法就是上线之前就配置好参数。 http://www.cndba.cn/cndba/dave/article/107980

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2283
    原创
  • 3
    翻译
  • 579
    转载
  • 196
    评论
  • 访问:8166677次
  • 积分:4428
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ