How to recover/recreate mysql's default 'mysql' database

Solution 1:

If you are still able to log in (I assume you aren't since there's no user table) and have databases to save, dump them with

mysqldump --routines databasename > outfile.sql

The MySQL database can be recreated with the command

# Most MySQL versions
mysql_install_db

# MySQL 5.7 and later
mysqld --initialize

MySQL Documentation here

Solution 2:

read more at: https://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization.html

On Windows, use one of these commands:

C:\> bin\mysqld --initialize
C:\> bin\mysqld --initialize-insecure

Solution 3:

I recently installed mysql 8 and it seemed that there was a problem with mysql default databases. Anyway, this worked for me :

mysql_upgrade -u root -p

Solution 4:

I removed database mysql by accident and mysqld --initialize didnt helped me. No errors showed, even mysql service started OK, but folder mysql wasnt created and mysql wasnt usable - I wasnt able even to create new databases.

After spending a lot of time on trying different options I noticed, that I have deprecated option innodb_additional_mem_pool_size=20M, which crashed mysqld --initialize

After fixing mysql config - it worked well.