MongoDB的数据恢复过程中的常见问题和解决方法

2023-04-15 00:00:00 解决方法 数据恢复 常见问题
  1. 数据库无法启动
    如果MongoDB的数据库无法启动,一般需要检查日志文件,查看错误信息,例如:
2021-01-01T10:11:12.345+0800 I STORAGE  [initandlisten] MongoDB starting
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten] db version v3.6.0
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten] git version: abf6cdd944559058edb806145506aaeec293d842
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2k  26 Jan 2017
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten] modules: none
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten] build environment:
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten]     distmod: ubuntu1604
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten]     distarch: x86_64
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2021-01-01T10:11:12.345+0800 I CONTROL  [initandlisten] options: {config: "/etc/mongod.conf", net: {bindIp: "127.0.0.1"}}
2021-01-01T10:11:12.345+0800 I STORAGE  [initandlisten] Detected data files in /var/lib/mongodb created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2021-01-01T10:11:12.345+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=8G,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2021-01-01T10:11:12.345+0800 E STORAGE  [initandlisten] WiredTiger error (2) [1609480272:345011][1:0x7f3db0bbf700], file:WiredTiger.wt, connection: __in_read_checkpoint, 272: /var/lib/mongodb/WiredTiger.turtle: handle-open: open: Permission denied
2021-01-01T10:11:12.346+0800 E STORAGE  [initandlisten] WiredTiger error (-31804) [1609480272:346408][1:0x7f3db0bbf700], file:WiredTiger.wt, connection: __in_read_checkpoint, 325: WiredTiger is unable to read the metadata checkpoint file: /var/lib/mongodb/WiredTiger.turtle: Permission denied
2021-01-01T10:11:12.346+0800 E STORAGE  [initandlisten] WiredTiger error (0) [1609480272:346465][1:0x7f3db0bbf700], file:WiredTiger.wt, connection: __checkpoint_verify, 647: the checkpoint file is more recent than the oldest uncheckpointed file. If you manually removed the metadata checkpoint file, please reinstall your installation to ensure proper recovery.
2021-01-01T10:11:12.346+0800 E STORAGE  [initandlisten] WiredTiger error (-31804) [1609480272:346515][1:0x7f3db0bbf700], file:WiredTiger.wt, connection: _checkpoint_open, 355: __checkpoint_verify fail
2021-01-01T10:11:12.346+0800 E STORAGE  [initandlisten] WiredTiger error (-2) [1609480272:346534][1:0x7f3db0bbf700], file:WiredTiger.wt, connection: __wt_log_base_credits, 196: the log file size must be less than or equal to the system limit (0 bytes) and greater than or equal to 20MB
2021-01-01T10:11:12.346+0800 E STORAGE  [initandlisten] WiredTiger error (0) [1609480272:346554][1:0x7f3db0bbf700], file:<fdopen>, connection: <access>, 548: file handle open failed
2021-01-01T10:11:12.346+0800 E STORAGE  [initandlisten] WiredTiger error (0) [1609480272:346578][1:0x7f3db0bbf700], file:journal_append, connection: _logging_recover, 346: Cannot write to "." directory (/var/lib/mongodb/journal) beside a file (/var/lib/mongodb/journal/j._0) for a journal file
2021-01-01T10:11:12.346+0800 I -        [initandlisten] Assertion: 28595:-31804: WT_ERROR: non-specific WiredTiger error at src/mongo/db/storage/wiredtiger/wiredtiger_util.cpp (on X86_64_GENERIC)
2021-01-01T10:11:12.346+0800 I STORAGE  [initandlisten] exception in initAndListen: Location28595: -31804: WT_ERROR: non-specific WiredTiger error, terminating
2021-01-01T10:11:12.346+0800 I CONTROL  [initandlisten] PC: 0x7f3dbce9723d __wt_abort+0
2021-01-01T10:11:12.346+0800 I CONTROL  [initandlisten] ***aborting after fassert() failure

上面的日志中,可以看到一些错误信息,例如:

  • permission denied,这时候一般是因为数据库文件的权限不正确,需要检查权限设置,例如:
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chmod -R 755 /var/lib/mongodb
  • file handle open failed,这时候一般是因为文件无法打开,可能是文件不存在或者文件被占用,需要检查文件是否存在以及文件是否正在被使用。
  • the log file size must be less than or equal to the system limit (0 bytes) and greater than or equal to 20MB,这时候一般是因为日志文件超过了系统限制,需要检查日志文件的大小以及系统限制,例如:
ulimit -n
  • Cannot write to "." directory,这时候一般是因为无法写入数据文件,需要检查文件系统的可用空间以及文件夹权限。
  1. 数据丢失或损坏
    如果MongoDB的数据丢失或损坏,可以通过以下步骤进行恢复:
  • 备份数据文件。可以通过mongodump命令进行备份,例如:
mongodump --db dbname --out backupdir
  • 停止数据库服务。
  • 删除数据文件夹。可以通过以下命令删除数据文件夹,例如:
sudo rm -rf /var/lib/mongodb/*
  • 恢复数据。可以通过mongorestore命令进行数据恢复,例如:
mongorestore --db dbname --dir backupdir/dbname
  • 启动数据库服务。
  1. 数据库性能问题
    如果MongoDB的性能出现问题,可以通过以下方法进行排查:
  • 检查日志文件。可以通过查看日志文件中的性能信息,例如查询响应时间、缓存命中率、磁盘写入速度等,来找出性能问题的原因。
  • 使用mongostat命令查看实时性能信息。可以通过mongostat命令查看数据库实时的性能信息,例如每个查询的响应时间、每秒写入的数据量等。
  • 修改配置文件。可以通过修改MongoDB的配置文件来调整性能,例如增加缓存大小、修改索引设置等。
  • 使用索引。可以通过建立索引来提升查询性能,例如在pidancode.com字段上建立索引:
db.collection.createIndex({"pidancode.com": 1})

上述是MongoDB数据恢复过程中的常见问题和解决方法。在实际操作中,可能会遇到更多的问题,需要针对具体情况进行排查。

相关文章