Fix “Error: rpmdb open failed” on CentOS or Amazon Linux 2

Yesterday, I have started to test some services on running Amazon Linux 2 powered by Amazon Web Services cloud. For some reason, my package manager yum is broken and when I started to update my yum database, I’ve got an error about rpmdb open failed. You can see my error below. If you are running on Centos7 or another Linux distro using the package manager as yum, you can also continue to read.

error: rpmdb: BDB0113 Thread/process 18275/140171317404864 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

I decided to move all rpm database packages to /tmp directory.

sudo mv /var/lib/rpm/__db* /tmp

After this movement, yum should be clean completely. I run “yum clean all” to clean my yum database.

sudo yum clean all

After the cleaning process, yum said to me, cleaning is done.

Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Repository elastic-7.x is listed more than once in the configuration
Bad id for repo: root@ip-11-12-30-149 yum.repos.d, byte = @ 4
Cleaning repos: amzn2-core amzn2extra-epel elastic-7.x epel treasuredata
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos

I crossed my fingers and tried to update my yum database with the latest versions of packages. For this; I run “sudo yum update” command.

sudo yum update

Now, yum is running perfectly again!

Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Repository elastic-7.x is listed more than once in the configuration
Bad id for repo: root@ip-11-12-30-149 yum.repos.d, byte = @ 4
amzn2-core                                                                      | 2.4 kB  00:00:00
amzn2extra-epel                                                                 | 1.3 kB  00:00:00
elastic-7.x                                                                     | 1.3 kB  00:00:00
epel/x86_64/metalink                                                            |  21 kB  00:00:00
epel                                                                            | 4.7 kB  00:00:00
treasuredata                                                                    | 2.9 kB  00:00:00
(1/9): amzn2-core/2/x86_64/group_gz                                             | 2.5 kB  00:00:00
(2/9): amzn2-core/2/x86_64/updateinfo                                           | 200 kB  00:00:00
(3/9): amzn2extra-epel/2/x86_64/primary_db                                      | 1.8 kB  00:00:00
(4/9): elastic-7.x/primary                                                      | 115 kB  00:00:00
(5/9): epel/x86_64/group_gz                                                     |  95 kB  00:00:00
(6/9): epel/x86_64/updateinfo                                                   | 1.0 MB  00:00:00
(7/9): epel/x86_64/primary_db                                                   | 6.8 MB  00:00:00
(8/9): amzn2-core/2/x86_64/primary_db                                           |  39 MB  00:00:00
(9/9): treasuredata/2/x86_64/primary_db                                         |  15 kB  00:00:00
elastic-7.x                                                                                315/315
192 packages excluded due to repository priority protections
No packages marked for update

If you having rpmdb issues with your yum, you can easily follow the same steps and fix your package manager in a minute.