Bug #76906 mysql_upgrade tries to rebuild tables in the wrong database!
Submitted: 30 Apr 2015 16:55 Modified: 26 Aug 2015 13:31
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Command-line Clients Severity:S2 (Serious)
Version:5.7.7 OS:Any
Assigned to: CPU Architecture:Any

[30 Apr 2015 16:55] Shane Bester
Description:
Running mysql_upgrade on an instance where some tables need to be rebuilt
can result in errors about tables not existing.

For example the proxies_priv table on a clean 5.5->5.7 upgrade:

E:\mysql-5.7.7-rc-winx64\bin>mysql_upgrade --no-defaults --force --verbose -uroot
Checking server version.
Running queries to upgrade MySQL server.
Checking system database.
mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.engine_cost                                  OK
mysql.event                                        OK
mysql.func                                         OK
mysql.general_log                                  OK
mysql.gtid_executed                                OK
mysql.help_category                                OK
mysql.help_keyword                                 OK
mysql.help_relation                                OK
mysql.help_topic                                   OK
mysql.host                                         OK
mysql.innodb_index_stats                           OK
mysql.innodb_table_stats                           OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv
error    : Table upgrade required. Please do "REPAIR TABLE `proxies_priv`" or dump/reload to fix it!
mysql.server_cost                                  OK
mysql.servers                                      OK
mysql.slave_master_info                            OK
mysql.slave_relay_log_info                         OK
mysql.slave_worker_info                            OK
mysql.slow_log                                     OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.user                                         OK

Repairing tables
mysql.proxies_priv
Note     : TIME/TIMESTAMP/DATETIME columns of old format have been upgraded to the new format.
status   : OK
Upgrading the sys schema.
Checking databases.
sys.sys_config                                     OK

Repairing tables
test.proxies_priv   <--------------------------
Error    : Table 'test.proxies_priv' doesn't exist
status   : Operation failed
Upgrade process completed successfully.
Checking if update is needed.

How to repeat:
create some databases and tables on ancient version.
upgrade to 5.7.7 and check that it tries to repair tables in wrong database.

Suggested fix:
Construct all SQL queries using fully qualified database and tables names.
Don't rely on the connection's "current db" property
[26 Aug 2015 13:31] Erlend Dahl
Fixed under the heading of 

Bug#20868496 MYSQL_UPGRADE IN 5.7.7+ REPAIR LOOKS USER TABLES IN TEST WHEN LOAD FROM 50/51/55

[23 Jul 2015 18:50] Paul Dubois

Noted in 5.7.9, 5.8.0 changelogs.

mysql_upgrade could fail to look for checked tables in the wrong
database during the repair phase.