ほげぇぇぇぇ。なぜかmysql.userが空になったままflush privilegesしちゃったよぉぉぉ〜

題名のとおりです。経緯? 知らん知らん。後輩君にMySQL5.1→5.6の移行を任せてたら
「あのぉ、kwyさん、select user, host from mysql.user;しても何も返ってこなくなっちゃったんですけど」
「どれどれ? う〜ん、flush privilegeやっちゃう?」
「えっ?」
「どうせ自体は好転しないしさ。えいっ」

flush privilege前

# mysql -uroot -pdbrootpass
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.6.19-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user, host from mysql.user ;
Empty set (0.00 sec)

mysql>
mysql>
mysql> show databases ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jakajaka           |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.01 sec)

mysql> exit
Bye

flush privilege後

# mysql -uroot -pdbrootpass
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

ファッ!
まて、こういう時はrootパスワード忘れた時の処理を…

# mysqld_safe --skip-grant-tables &
[1] 1520
# 150213 19:23:44 mysqld_safe Logging to '/var/log/mysql/server1-error.log'.
150213 19:23:44 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.19-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql>
mysql>
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=PASSWORD("dbrootpass") where user = 'root' ;
Query OK, 0 rows affected (0.03 sec)
Rows matched: 0  Changed: 0  Warnings: 0

0行…?さぁ雲行きが怪しくなってまいりました…。

mysql> grant all privileges on *.* to 'root'@'localhost' identified by 'dbrootpass' ;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option soit cannot execute this statement

マジかっ!? --skip-grant-tablesつけた時はgrantコマンド使えないのっ!?



どうする…どうする…。結局力技でinsertすることにしました
ところがここにもうひとつの罠が…

mysql> insert into mysql.user values('localhost',
    -> 'root',
    -> '' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> 'Y' ,
    -> '',
    -> '' ,
    -> '' ,
    -> ''  ,
    -> '0' ,
    -> '0' ,
    -> '0' ,
    -> '0' ,
    -> null ,
    -> null ,
    -> '');
ERROR 1265 (01000): Data truncated for column 'password_expired' at row 1

これ実は、最後のカラムが'password_expired'というのですが、これがデフォルトでNに設定されてまして、このカラムがNだとデータが自動的にtruncate喰らうという鬼仕様となってます。

ですので最終的にはこのカラムをYにした上でinsertを実行し、

mysql> update user set password=PASSWORD("dbrootpass") where user = 'root' ;

を実行することで解決(暫定ですけど)になります。その後、その他テーブルのMySQLテーブルのデータを持ってくるなどして解決しましょう