17

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?

2
  • Wouldn't it be better to explain your problems wit mysql. So people can try to help you fix those. Re installing is more a windows-way of solving problems. Dec 4, 2013 at 17:47
  • 1
    Being cautious, it's a good idea to backup your databases first. You can use mysqldump with the --all-databases option. dev.mysql.com/doc/refman/5.7/en/…
    – Progrock
    Dec 4, 2015 at 9:38

3 Answers 3

13

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).

2
  • How do I do it in a way to delete my files, if I wanted to?
    – Aditya M P
    Mar 6, 2014 at 6:57
  • 3
    is purge the correct command if I don't want to lose the data? Feb 13, 2018 at 21:01
3

Use sudo apt-get remove mysql-server.

Don't use sudo apt-get purge mysql-server, which will remove the files in /var/lib/mysql.

1
  • Just select <No> when it ask you to remove all MySQL databases.
    – mariozawa
    Nov 6, 2018 at 2:47
0

This is an old question, but nevertheless, here are my 5¢:

  1. Do a backup of the database first, e.g., using mysqldump.
  2. Now, do sudo apt-get purge mysql-server mysql-server-5.7. It will ask you whether you really want to delete the database files, click on Yes
  3. Reinstall mysql using sudo apt-get install mysql-server mysql-server-5.7
  4. Start the mysql database
  5. Recreate the database and its user using the mysql command line utility
  6. Import the dump from your backup

You may ask what this whole procedure is good for: There are cases where the mysql database has had many large transactions and occupies to much space on the disk. Destroying and rebuilding the database is a way of shrinking it again.

1
  • in some cases deleting db is not an option.. so this answer needs to be corrected with that context..
    – Siddharth
    Jun 16, 2019 at 3:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .