MySQL Fatal error: Can't open and lock privilege tables: Incorrect file format 'user'

亡梦爱人 提交于 2019-11-30 13:01:50

I managed so solve this problem with answer from ruby.b

You'll have to repair your host table. To do this, issue the following command to start your server bypassing the privilege system

In one terminal, run

$ sudo mysqld --skip-grant-tables

Open another terminal and execute these commands

$ mysql
mysql> use mysql
mysql> repair table host use_frm;
mysql> exit

And restart the mysql service

$ sudo service mysql restart

Thanks to John for putting me on the right track, I had a few other hoops to jump through on my system. Hope this helps someone.

This is a corrupt privilege table. Maybe caused by an upgrade or power failure. My system OpenSUSE 13.2, MySQL 5.6. Simple reinstall does not fix, must delete all traces of MySQL before reinstall OR…

Close down all instances of MySQL

$ systemctl stop mysql.service
$ pkill -9 mysqld

Start server bypassing privilege system

$ sudo mysqld_safe --user=root --skip-grant-tables

Start MySQL command line tool

$ mysql

If you receive

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

mysqld_safe is running with its socket somewhere else. Find it with.

$ sudo find / -type s

Mine was in /var/run/mysql/ Edit the socket line in my.cnf, making a note of your existing socket setting. My line became

socket=/var/run/mysql/mysql.sock

Return to 'Close down all instances of MySQL' (the top of these instructions). Follow them through to 'Start MySQL command line tool'. Hopefully you can open mysql successfully. From mysql command line.

mysql> use mysql
mysql> repair table user use_frm;
mysql> exit

Close down all instances of MySQL

$ systemctl stop mysql.service
$ pkill -9 mysqld

Re-edit my.cnf, returning the socket line to its original setting.

I had to reset permissions on 2 files in my mysql data directory.

$ chown mysql:mysql server2.err
$ chown mysql:mysql server2.pid

Start the MySQL server

$ systemctl start mysql.service

I then got the same original error with another table (db)

[ERROR] Fatal error: Can't open and lock privilege  tables: Incorrect file format 'db'

And had to repeat the above procedure multiple times altering the 'repair' command until all privilege tables were fixed.

mysql> repair table db use_frm;

Here is the setup for @John Linhart's answer, if you're running Docker:

First, start a new docker container with the appropriate tag from the mysql-container (the same you used to write the DB with).

$ docker run --rm -it -v <named_volume>:/var/lib/mysql mysql:<tag> /bin/bash

This will launch a new container with the correct named volume (or mounted volume) mounted in the container and drop you into a shell as root. The mysqld-Daemon will refuse to launch as root though, so we'll run it as the mysql-user:

$ whoami
root
$ which mysqld
/usr/sbin/mysqld
$ su mysql
$ whoami
mysql
$ /usr/sbin/mysqld --skip-grant-tables
....

Now to run the SQL commands, we'll connect to the running container from a new terminal:

$ docker ps 
CONTAINER ID [...]
abc123 [...]
$ docker exec -it abc123 /bin/bash
# We're on the container now!
$ whoami
root
$ mysql
...

And continue from there. When you're done, leave the container on the second terminal via exit. The terminal running mysqld will not respond to CMD+C, so we'll stop the container via Docker:

$ docker ps
CONTAINER ID [...]
abc123 [...]
$ docker stop abc123
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!