暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Mongodb directoryPerDB

数据库笔记 2021-03-26
2995

最近遇到一个问题,mongodb可不可以针对每一个数据库指定数据目录,以便可以将多个大库分散到多个卷设备。

但是相关资料很少,由于我们配置,启动Mongodb时,会通过dbpath来指定Mongodb数据文件的存放路径,而dbpath并不能指定多个路径,在不考虑扩容或启用sharding模式的情况下,可以指定--directoryperdb或在配置文件中设置directoryperdb为true,来为每个db创建单独的目录。

--directoryperdb Uses a separate directory to store data for each database. The directories are under the --dbpath directory, and each subdirectory name corresponds to the database name.

https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-directoryperdb

如果上面的描述太过抽象,来看一下具体的例子:


未配置directoryperdb的dbpath:

可以看到,所有的数据库文件和索引文件都在dbpath下


配置了--directoryperdb的dbpath:

可以看到,数据文件和索引文件都规划到对应的数据库名的目录中


那么分盘方式目前可行的方式是通过符号链接将该路径链接到卷设备,或者直接作为一个文件系统挂载点。

由此会引申出一个问题,使用directoryperdb的方式,只能在数据库初始化的时候配置。对于运行一段时间的数据库,如果设置该参数起库,则必定启动失败:

那么想改用directoryperdb模式,必定需要数据迁移,如何实施,官方手册给出了方案:

    For standalone instances:
    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:
    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.

    --------------------------------------

    PS:最后支持一波新疆棉花

    文章转载自数据库笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论