1. Home
  2. Linux
  3. CentOS
  4. RPM Rebuild Database in CentOS: A Complete Tutorial

RPM Rebuild Database in CentOS: A Complete Tutorial

How to Rebuild a Corrupted RPM Database in CentOS
How to Rebuild a Corrupted RPM Database in CentOS

Anxious about learning a New hack; let’s Learn  RPM Rebuild Database in CentOS.
The RPM Rebuild database is made up of files under the /var/lib/rpm/ directory in CentOS and other enterprise Linux distributions such as RHEL, openSUSE, Oracle Linux, and more.
Also, here are a few hand-picked guides that must read about CentOS;

  1. How to install LDAP on CentOS 7
  2. How To Open Port in CentOS 7 With Firewalld

RPM Database

If the RPM database is corrupted, RPM will not work correctly, and thus updates cannot be applied to your system. You will encounter errors while updating packages on your system via the YUM package manager. The worst case scenario is being unable to run any rpm or yum commands successfully.
There are a number of factors that can lead to RPM database corruption, including incomplete previous transactions, installation of certain third-party software, removing specific packages, and many more.
In this article, we will show you how to rebuild a corrupted RPM database; this way, you can recover from an RPM database corruption in CentOS. This requires root user privileges; otherwise, use the sudo command to gain those privileges.

Rebuild Corrupted RPM Database in CentOS

Start by backing up your current RPM database before proceeding (you might need it in the future) by using the following commands.

# mkdir /backups/
# tar -zcvf /backups/rpmdb-$(date +"%d%m%Y").tar.gz  /var/lib/rpm

Back up RPM Database

Next, verify the integrity of the master package metadata file /var/lib/rpm/Packages; this is the file that needs rebuilding. First, remove /var/lib/rpm/__db* files to prevent stale locks by using the following commands.

# rm -f /var/lib/rpm/__db*
# /usr/lib/rpm/rpmdb_verify /var/lib/rpm/Packages

Verify RPM Database

In case the above operation fails, meaning you still encounter errors, you should dump and load a new database. Also verify the integrity of the freshly loaded Packages file as follows:

# cd /var/lib/rpm/
# mv Packages Packages.back
# /usr/lib/rpm/rpmdb_dump Packages.back | /usr/lib/rpm/rpmdb_load Packages
# /usr/lib/rpm/rpmdb_verify Packages

Dump and Load RPM Database

Now, to check the database headers, query all installed packages using the -q and -a flags, and try to carefully observe any error(s) sent to the stderror file.

# rpm -qa >/dev/null      #output is discarded to enable printing of errors only

Last but not least, rebuild the RPM database using the following command (the -vv option displays lots of debugging information):

# rpm -vv --rebuilddb

Rebuild RPM Database

Use dcrpm Tool to Detect and Correct RPM Database
We have also discovered the dcrpm (detect and correct rpm) command line tool, used to identify and correct well-known issues with RPM database corruption. It is a simple and easy-to-use tool which you can run without any additional options necessary. For effective and reliable usage, you should run it regularly via cron.
You can install it from source; just download the source tree and install it using setup.py (which should grab the psutil dependency from pypi as well), as shown.

# git clone https://github.com/facebookincubator/dcrpm.git
# cd dcrpm
# python setup.py install

Once you have installed dcrpm, run it as shown.

# dcrpm

Finally, try to run your failed rpm or yum command again to see if everything is working fine.
dcrpm Github repository: https://github.com/facebookincubator/dcrpm
You can find more information on the RPM database recovery page.

Final Thoughts

That’s all! In this article, we have explained how to rebuild a corrupted RPM database in CentOS.
Do let us know if you want to add any specific VPS Hosting or WordPress hosting topics into this tutorial series.

Updated on December 23, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']