I've seen this frequently now that the restore process is being killed for, from the first glance at it, no apparent reason. Checking journalctl, the problem becomes obvious:
Out of memory: Killed process $PID (mongorestore) ...
So the restore process requires huge amounts of memory in order to restore the Database. I've had the same problem with mongoimport and I found the solution to be either
- add more RAM to the machine
- add a large swap partition / file to the system
Since RAM is probably more 'expensive' I'd suggest going with the second option and just adding another disk if necessary.
In my case, the total size of the database was ~5G and after some experimenting I found out that I had to have atleast 18G of RAM available for the mongorestore / mongoimport to go through.
Let's add a disk to the server on the hypervisor
[root@hyv01 ~]# virsh vol-create-as --pool vm-data --name test.qcow2 --capacity 32G --format qcow2 --prealloc-metadata
[root@hyv01 ~]# virsh attach-disk --domain mongodb01.archyslife.lan --source /srv/kvm/vm-images/test.qcow2 --target vdb
Now that the disk has been added, format it as a swap. I'm using a volumegroup since I might extend the disk in the future.
root@mongodb01 ~]# pvcreate /dev/vdb
[root@mongodb01 ~]# vgcreate vg_mongodb
[root@mgonodb01 ~]# lvcreate -n lv_restoreswap -L 20G vg_mongodb
[root@mongodb01 ~]# mkswap /dev/vg_mongodb/lv_restoreswap
[root@mongodb01 ~]# swapon /dev/vg_mongodb/lv_restoreswap
[root@mongodb01 ~]# echo "/dev/mapper/vg_mongodb-lv_restoreswap swap swap defaults 0 0" >> /etc/fstab
Now, let's retry restoring again:
[root@mongodb01 ~]# mongorestore --host $(hostname -f) --port 27017 --username mydb --password Sup3r!s3cuI2e --authenticationDatabase mydb --db mydb --dir /var/backup/mydb
The restore should now go through as expected.
Feel free to comment and / or suggest a topic.
How works with Docker?
ReplyDeleteHi there,
DeleteI'm not sure since I've not yet run MongoDB in a container. Anyway this should be managed with cgroups and therefore you'd have to modify the cgroup for your container to contain enough ram in order to prevent oom.