Description:
When I run mysqldump on a database named “haha\ha” with no other arguments, the output comes out fine.
When I add the --routines argument, though, it apparently misquotes the database name such that the backslash gets doubled. This produces an error since I have no database named “haha\\ha”.
felipe@Macintosh 18:37:23 ~
> sudo /usr/local/mysql/bin/mysqldump --routines --socket=/opt/local/var/run/mysql56/mysqld.sock 'haha\ha'
-- MySQL dump 10.13 Distrib 5.7.9, for osx10.9 (x86_64)
--
-- Host: localhost Database: haha\ha
-- ------------------------------------------------------
-- Server version 5.6.27
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Dumping routines for database 'haha\ha'
--
mysqldump: Couldn't execute 'use `haha\\ha`': Unknown database 'haha\\ha' (1049)
How to repeat:
1) Make a database named: haha\ha
2) mysqldump 'haha\ha'
OBSERVE: success
3) mysqldump --routines 'haha\ha'
OBSERVE: failure; the error message suggests that it’s trying to double-up the backslash on a USE statement.
Suggested fix:
mysqldump.c has this command:
mysql_real_escape_string_quote(mysql, db_name_buff,
db, (ulong)strlen(db), '\'');
… which looks like it ought to be passing in '`' rather than '\''.
The same problem goes back at least as far as 5.1.73.