Rebuilding a corrupted RPM database

A few days ago one of our customers reported that he couldn’t install any packages using yum command using CentOS Linux. After investigating I found the RPM database was corrupted. This is a very common issue on CentOS and RHEL servers.

CentOS and RHEL servers use rpm to manage package installation, remove and upgrades. If the rpm database is corrupted in some way, other system tools that rely on RPM like Yum/Dnf will stop working.

How can I rebuild the RPM database on CentOS or RHEL?

Install some requirements to ensure you can fix your RPM database:

yum install db4-utils yum-utils -y

Always make sure you have a backup of your current RPM database

rsync -avpr /var/lib/rpm /var/lib/rpm.bak

Rebuilding a corrupted RPM database: 2 solutions

1) Remove RPM database files and rebuild:

rm /var/lib/__db* -fv
rpm --rebuilddb

rpm –rebuilddb command rebuilds the RPM database based on the file /var/lib/rpm/Packages. The “Packages” files is required in order to rebuild your RPM database. On slow servers this command can take some time to execute.

Try using yum normall now, if that doesn’t fix your issue, move to step 2:

2) Reload RPM Packages

Another nice strategy to rebuild your corrupted RPM database is using db_dump and db_load tools. In the example below we will use db_dump to dump the Packages file, and then db_load will be used to reload the Packages file. This is an alternative way to fix RPM Db issues:

cd /var/lib/rpmdb
mv Packages Packages.bak
db_dump Packages.bak | db_load Packages

As an additional sanity check query all headers in the DB

rpm -qa

Finally, rebuild the RPM database:

rpm --rebuilddb

At this point you should have your RPM database fully working again. Please let us know if this two steps doesn’t work for you, or if you know other solutions to share with us.

Further reading

 

About the Author: Esteban Borges

Experienced Sr. Linux SysAdmin and Web Technologist, passionate about building tools, automating processes, fixing server issues, troubleshooting, securing and optimizing high traffic websites.

2 Comments

  1. and possibly add a slash to the source dir like:
    rsync -avpr /var/lib/rpm/ /var/lib/rpm.bak

    Thanks for a very useful tutorial

Leave a Reply

Your email address will not be published.