rpmdb: Thread died in Berkeley DB library

While trying to update a CentOS server I got this error:

[root@washingmashine ~]# yum update
error: rpmdb: BDB0113 Thread/process 47226/140411903506496 failed: BDB1507 Thread died in Berkeley DB library
error: db5 error(-30973) from dbenv->failchk: BDB0087 DB_RUNRECOVERY: Fatal error, run database recovery
error: cannot open Packages index using db5 -  (-30973)
error: cannot open Packages database in /var/lib/rpm
CRITICAL:yum.main:

Error: rpmdb open failed

The RPM database seemed corrupted.

First, I deleted the DB:

[root@washingmashine ~]# rm -rf /var/lib/rpm/__db*

Then I used the rpm --rebuilddb command to rebuild it.