mysqlsqlsystem-tables

Recreate a `mysql.user` table in mysql


I found my mysql.user table is messed up and use drop table if exists mysql.user to drop it, how can I create this special table manually and add my new account to it.


Solution

  • NB I have never tried this so I have no idea if it will work at all. YOU HAVE BEEN WARNED!!

    Before you do anything, shut down the MySQL service and take a backup of the data folder (copy it elsewhere).

    Next, either try creating a new data directory and run mysql_install_db (from the MySQL install directory, scripts/mysql_install_db --datadir=<new data directory>), or use a virtual machine and do the same. Whatever you do, do NOT install into the original folder. Use the same version of MySQL to do this

    Whichever method you use, take the resultant files from <mysql data folder>/data (they should be called user.MYD, user MYI and user.frm) and copy them into your broken folder. Ensure that user rights are the same as the rest of the folder (owner and group should be read/write).

    Restart your MySQL service and see if you can get into the system (no login information will be required, but you will have to log in to MySQL as root. If you can, recreate the relevant users for application access. If not, you still have a backup of the data folder.

    In the event that it doesn't work, and assuming that you have recent backups, re-create the data folder completely using mysql_install_db (ensuring that you still have the copy of the folder you took before you started out), create the users, then restore your individual databases one by one from your last good backup. Do not restore the mysql database and tables - they will be built as you restore the other databases.

    Try all this out on a virtual machine first - it'll help you find any problems along the way

    Good luck