Bug #84214 mysqldump events/routines error if database name include ' character
Submitted: 15 Dec 2016 10:48 Modified: 15 Dec 2016 15:12
Reporter: edge yang Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: mysqldump Command-line Client Severity:S3 (Non-critical)
Version:all OS:Any
Assigned to: CPU Architecture:Any

[15 Dec 2016 10:48] edge yang
Description:
mysqldump events and routines will be error when database name include ' character.
mysqld reports like this: Couldn't execute 'use `\'test`': Unknown database '\'test' (1049)
because function mysql_real_escape_string add '\\' before '\''character.

How to repeat:
mysql> create database `'test`;
Query OK, 1 row affected (0.00 sec)

mysql> create table `'test`.t1(id int);
Query OK, 0 rows affected (0.00 sec)

mysql> use `'test`
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> delimiter //
mysql> create procedure pload()
    -> begin
    -> select * from t1;
    -> end
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

shell# bin/mysqldump -S /tmp/mysql_3306.sock -uroot -R "'test"
or
shell# bin/mysqldump -S /tmp/mysql_3306.sock -uroot -R --all-databases

--
-- Dumping routines for database ''test'
--
mysqldump: Couldn't execute 'use `\'test`': Unknown database '\'test' (1049)

Suggested fix:
--- a/client/mysqldump.c
+++ b/client/mysqldump.c
@@ -2344,7 +2345,7 @@ static uint dump_events_for_db(char *db)

       /* Get database collation. */

-      if (fetch_db_collation(db_name_buff, db_cl_name, sizeof (db_cl_name)))
+      if (fetch_db_collation(db, db_cl_name, sizeof (db_cl_name)))
         DBUG_RETURN(1);
     }

@@ -2549,7 +2550,7 @@ static uint dump_routines_for_db(char *db)

   /* Get database collation. */

-  if (fetch_db_collation(db_name_buff, db_cl_name, sizeof (db_cl_name)))
+  if (fetch_db_collation(db, db_cl_name, sizeof (db_cl_name)))
     DBUG_RETURN(1);

   if (switch_character_set_results(mysql, "binary"))
[15 Dec 2016 15:12] MySQL Verification Team
Thank you for the bug report. Verified as described.

LOCK TABLES `t1` WRITE;
/*!40000 ALTER TABLE `t1` DISABLE KEYS */;
/*!40000 ALTER TABLE `t1` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Dumping routines for database ''test'
--
mysqldump: Couldn't execute 'use `\'test`': Unknown database '\'test' (1049)

c:\dbs\5.7\bin>