25

Due to a mishap I deleted the entire /var/lib/mysql directory. Since the database did not contain anything important, I do not want to go through the hassle of restoring it from an old backup, but instead create the directory structure from scratch. How do I do it without reinstalling MySQL?

1
  • (Wanted as comment, but I don't have enough reputation) This works for me too, but it deletes root access. To fix this see: . stackoverflow.com/questions/41645309/… A comment from a user whise answer may get deleted Commented Dec 7, 2022 at 10:21

3 Answers 3

24

You should be able to just run mysql_install_db from the command line

mysql_install_db initializes the MySQL data directory and creates the system tables that it contains, if they do not exist. It also initializes the system tablespace and related data structures needed to manage InnoDB tables. As of MySQL 5.6.8, mysql_install_db is a Perl script and can be used on any system with Perl installed. Before 5.6.8, it is a shell script and is available only on Unix platforms.

Please read the MySQL Documentation in full for mysql_install_db, then ...

GIVE IT A TRY !!!

If it does not work on the first try, do this

mkdir /var/lib/mysql
mkdir /var/lib/mysql/mysql
chown -R mysql:mysql /var/lib/mysql 

then run mysql_install_db again

4
  • It worked on the first try. Thx. The password is another issue. Commented Jun 9, 2015 at 20:37
  • See my post dba.stackexchange.com/questions/44645/… Commented Jun 9, 2015 at 20:39
  • you may also need to restorecon -r /var/lib/mysql if SELinux is enabled Commented Jan 20, 2019 at 18:22
  • 1
    Note: This is now depreciated! See "Cristian Deluxe" answer below for modern 5.7 MySQL
    – MadHatter
    Commented Sep 28, 2020 at 14:30
15

For MySQL 5.7 > just:

# Move or remove the original dir
mv /var/lib/mysql /tmp/mysql

# Create a new dir
mkdir /var/lib/mysql

# Change owner to mysql (user and group)
chown -R mysql:mysql /var/lib/mysql

# Create MySQL initial data
mysqld --initialize
3
  • 2
    the only answer that's worked for me
    – Bahaa Odeh
    Commented Nov 12, 2018 at 15:26
  • mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
    – Bahaa Odeh
    Commented Nov 12, 2018 at 15:26
  • Can confirm, this fixed a reinstall issue on Ubuntu 16.04 LTS Server.
    – MadHatter
    Commented Sep 28, 2020 at 14:29
1

(Wanted as comment, but I don't have enough reputation)

This works for me too, but it deletes root access. To fix this see: mysql-error-access-denied-for-user-rootlocalhost

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.