If I reinstall mysql do I lose my databases?

I have MySQL installed on my localhost. What happens if I have a problem with mysql and I want to re-install it again? Do I lose my existing databases?


No, reinstalling mysql-server will not delete you database files, only delete the package files of mysql-server. You will be able to access your files(database) after you re-install the server.

To purge and install mysql-server:

sudo apt-get purge mysql-server
sudo apt-get install mysql-server

If you want to delete the database too, you must delete it first before you remove mysql. To delete the database cleanly, on the mysql prompt type:

drop database <database_name>

I found another way to delete the database files, but I am not sure whether this is a clean way to delete the database, so take it with a pinch of salt:

From a question on stackoverflow:

It(The location of the database files) is installation specific, but if you've /var/lib/mysql , then:

  • MyISAM tables will be stored in individual files in /var/lib/mysql/databasename/
  • InnoDB resides in /var/lib/mysql/ibdata (unless you've used the innodb_per_table setting, in which case it's stored much like for MyISAM tables)

So check the location of your databases in these two locations and delete the directory of the databases(requires superuser privileges to read and delete).

  • See also: Similar question on Unix & Linux