HowTo: Reinstall MySQL v5.x On Linux

My MySQL database installation has been corrupted. I would like to delete old mysql server. How do I reinstall MySQL database server version 5.x on Linux or Unix-like oses? A software upgrade broke my system. How can I uninstall and re-install mysql again to get fresh full mysql again?

Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
You can easily reinstall mysql server on Linux or Unix-like operating system. The steps are as follows:


  1. Backup database and all config files
  2. Erase/uninstall existing mysql server/client.
  3. Delete all files data directory.
  4. Delete all mysql config files.
  5. Completely reinstall mysql server.
  6. Restore config files and database.

Step #1: Backup backup backup

Make a backup – it cannot be stressed enough how important it is to make a backup of your system before you do this. You need to backup

  1. MySQL database data directory (e.g. /var/lib/mysql/.
  2. MySQL databases using mysqldump command.
  3. Mysql config files /etc/my.cnf, /etc/logrotate.d/mysqld, and other files.
  4. Mysql log files (e.g. /var/log/mysqld.log.

In this example, I am backing up all important files on CentOS/RHEL 6.x based server:
# mkdir /root/mysql-files/
# tar zcvf /root/mysql-files/mysql.config-files.dd-mm-yyyy.tar.gz /etc/logrotate.d/mysqld /var/log/mysqld.log /etc/my.cnf /root/my.cnf /var/lib/mysql/

In this example, I am backing up all databases using a shell script called

# Dump MySQL databases.
# Note: Test only on RHEL/CentOS/Debian/Ubuntu Linux.
# Author: nixCraft <> Under GPL v2.0+
# -----------------------------------------------------
NOW=$(date +"%d-%m-%Y")
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
if [ ! -d $BAK ]; then
  mkdir -p $BAK
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
echo -n "Dumping...${THISDB}..."
for db in $DBS
 echo -n "$db "
 FILE=$BAK/mysql-$db.$NOW-$(date +"%T").gz
echo -n  "...Done @ ${BAK} directory."
echo ""

Run it as follows
$ ./
Sample outputs: cyberciti mysql...Done @ /root/mysql-files/08-12-2013 directory.

Verify backups:
# ls -l /root/mysql-files/08-12-2013
Sample outputs:

-rw-r--r--. 1 root root  1836687 Dec  8 04:00 mysql-blog.08-12-2013-04:00:18.gz
-rw-r--r--. 1 root root  7152648 Dec  8 04:00 mysql-cyberciti.08-12-2013-04:00:25.gz
-rw-r--r--. 1 root root   135530 Dec  8 04:00 mysql-mysql.08-12-2013-04:00:41.gz

I suggest that you read our previous guide “HowTo: Migrate / Move MySQL Database And Users To New Server” for more information.

Step #2: Erase/Delete MySQL server

If you are using Debian/Ubuntu Linux type the following apt-get command:
$ sudo apt-get purge mysql-server mysql-common mysql-client
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package mysql-client is not installed, so not removed
The following packages were automatically installed and are no longer required:
  libnet-daemon-perl libdbi-perl libterm-readkey-perl mysql-server-core-5.5 mysql-client-core-5.5
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
  libdbd-mysql-perl* libmysqlclient18* mysql-client-5.5* mysql-common* mysql-server*
0 upgraded, 0 newly installed, 6 to remove and 2 not upgraded.
After this operation, 67.3 MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 82128 files and directories currently installed.)
Removing mysql-server ...
Removing mysql-server-5.5 ...
mysql stop/waiting
Purging configuration files for mysql-server-5.5 ...
Removing mysql-client-5.5 ...
Removing libdbd-mysql-perl ...
Removing libmysqlclient18 ...
Purging configuration files for libmysqlclient18 ...
Removing mysql-common ...
Purging configuration files for mysql-common ...
dpkg: warning: while removing mysql-common, directory '/etc/mysql' not empty so not removed.
Processing triggers for man-db ...
Processing triggers for ureadahead ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

Delete config/database/log files using rm command:
$ sudo rm -rvfi /var/lib/mysql /etc/mysql/ /var/log/mysql*

If you are using CentOS/RHEL/Fedora/Red Hat/Scientific Linux type the following yum command to uninstall mysql:
# yum remove mysql mysql-server
Sample outputs:

Loaded plugins: product-id, rhnplugin, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package mysql.x86_64 0:5.1.71-1.el6 will be erased
---> Package mysql-server.x86_64 0:5.1.71-1.el6 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
 Package                Arch             Version                Repository                       Size
 mysql                  x86_64           5.1.71-1.el6           @rhel-x86_64-server-6           2.4 M
 mysql-server           x86_64           5.1.71-1.el6           @rhel-x86_64-server-6            25 M
Transaction Summary
Remove        2 Package(s)
Installed size: 27 M
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Erasing    : mysql-server-5.1.71-1.el6.x86_64                                                   1/2 
  Erasing    : mysql-5.1.71-1.el6.x86_64                                                          2/2 
  Verifying  : mysql-server-5.1.71-1.el6.x86_64                                                   1/2 
  Verifying  : mysql-5.1.71-1.el6.x86_64                                                          2/2 
  mysql.x86_64 0:5.1.71-1.el6                    mysql-server.x86_64 0:5.1.71-1.el6                   

Delete config/database/log files using rm command:
[code]# rm -rfvi /etc/my.cnf /var/lib/mysql/ /var/log/mysqld.log[/code]

Step #3: Reinstall mysql database server

If you are using CentOS/RHEL/Fedora/Red Hat/Scientific Linux type the following yum command to install mysql:
# yum install mysql mysql-server

If you are using Debian/Ubuntu Linux type the following apt-get command:
$ sudo apt-get install mysql-client mysql-server mysql-common

Step #4: Restore config files and databases

Restore all config files and databases as demonstrated in step #1. See how to restore a backup of a mysql database:
$ gunzip mysql-blog.08-12-2013-04:00:18.gz
$ mysql -u root -p mysql -e 'CREATE DATABASE blog;'
$ mysql -u root -p blog < mysql-blog.08-12-2013-04\:00\:18

Restore mysql config file as follows:
# tar xvf /root/mysql-files/mysql.config-files.dd-mm-yyyy.tar.gz -C /root/backups/
# cp /root/backups/etc/my.cnf /etc
# service mysqld restart

This entry is 5 of 5 in the CentOS Linux MySQL Server Tutorial series. Keep reading the rest of the series:
  1. CentOS install Mysql database server
  2. CentOS install MySQL client only
  3. MySQL Create a user accounts
  4. MySQL Create a database & tables and data
  5. Reinstall MySQL On Linux
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

10 comments… add one
  • bob Dec 10, 2013 @ 8:26

    Thank you mate, just a simple thank you!

  • n.arshad Aug 25, 2014 @ 4:37

    nice explanation!!!!!!!!!!! thank u very much……..

  • Dexter Nov 26, 2014 @ 6:29

    insert mysqldomp option “-B” for add sql command “CREATE DATABASE ” in sql file

    then mysql -uroot -ppassword < dbl.sql

  • marc Jan 30, 2015 @ 16:42

    After reinstall I cannot start the mysqld. I get the errors
    /sbin/service mysqld start
    Cannot check for MySQL Daemon startup because of mysqladmin failure.
    Starting mysqld: [FAILED]

    command line statements like mysql -u root -p produces
    Bus error

  • 338A Aug 3, 2015 @ 3:22

    i like the tutorial. thanks for helping trouble with my vps

  • Neo Jan 21, 2016 @ 18:38

    After reinstalling, I cannot access with root password. Not sure how to get it.

  • nvirenque Mar 15, 2016 @ 20:37

    Many thanks,
    my install was totally broken and reinstall correct it.

  • krisna Jul 2, 2016 @ 22:12

    thanks!! it helps!

  • zero Jul 8, 2016 @ 19:15


  • Zeeshan Apr 6, 2017 @ 9:09

    You can use rm -rvf /var/lib/mysql /etc/mysql/ /var/log/mysql* instead of rm -rvfi /var/lib/mysql /etc/mysql/ /var/log/mysql* else it will keep asking for a confirmation before deleting each file, and there are many files to be deleted.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.