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?
-
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.– Evenbit GmbHDec 4, 2013 at 17:47
-
1Being 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/…– ProgrockDec 4, 2015 at 9:38
3 Answers
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
-
-
3is
purge
the correct command if I don't want to lose the data? Feb 13, 2018 at 21:01
Use sudo apt-get remove mysql-server
.
Don't use , which will remove the files in sudo apt-get purge mysql-server
/var/lib/mysql
.
This is an old question, but nevertheless, here are my 5¢:
- Do a backup of the database first, e.g., using
mysqldump
. - 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 onYes
- Reinstall mysql using
sudo apt-get install mysql-server mysql-server-5.7
- Start the mysql database
- Recreate the database and its user using the
mysql
command line utility - 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.
-
in some cases deleting db is not an option.. so this answer needs to be corrected with that context.. Jun 16, 2019 at 3:42