Bug #31113 mysqldump 5.1 can't handle a dash ("-") in database names
Submitted: 20 Sep 2007 14:59 Modified: 19 Nov 2007 5:05
Reporter: Gadi Cohen Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: mysqldump Command-line Client Severity:S2 (Serious)
Version:5.1.21-beta/5.1BK OS:Linux
Assigned to: Sergey Glukhov
Tags: Dash, mysqldump

[20 Sep 2007 14:59] Gadi Cohen
Description:
mysqldump bails out when trying to access existing databases which have a dash ("-") in their name.

$ mysqldump -u root -ppass -A --opt --quote-names > mysql
mysqldump: Couldn't execute 'use d470k-wiki': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-wiki' at line 1 (1064)

mysql> use d470k-wiki;
Database changed

How to repeat:
mysql> create database `test-database`;
Query OK, 1 row affected (0.02 sec)
mysql> use test-database;
Database changed
mysql> create table test (a int);
Query OK, 0 rows affected (0.01 sec)

$ mysqldump -u root -ppass --opt --quote-names test-database > test
mysqldump: Couldn't execute 'use test-database': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-database' at line 1 (1064)

(or with the -A option.  if there are no tables in the database, the command will work).

Suggested fix:
mysqldump should quote database names when communicating with the server.
[20 Sep 2007 15:30] Miguel Solorzano
Thank you for the bug report. I was able to repeat with 5.1 and 5.0 branch
isn't affected.

LOCK TABLES `test` WRITE;
/*!40000 ALTER TABLE `test` DISABLE KEYS */;
/*!40000 ALTER TABLE `test` ENABLE KEYS */;
UNLOCK TABLES;
mysqldump: Couldn't execute 'use test-database': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-database' at line 1 (1064)
[29 Oct 2007 13:21] Timour Katchaounov
P2 as there should be no error in this case.
[30 Oct 2007 7:46] xiaoxin yang
quote database names will be ok

--- mysqldump.c.orig    2007-10-29 17:40:50.000000000 +0800
+++ mysqldump.c 2007-10-30 15:42:00.000000000 +0800
@@ -1033,7 +1033,7 @@
   MYSQL_RES *db_cl_res;
   MYSQL_ROW db_cl_row;

-  my_snprintf(query, sizeof (query), "use %s", db_name);
+  my_snprintf(query, sizeof (query), "use `%s`", db_name);

   if (mysql_query_with_error_report(mysql, NULL, query))
     return 1;
[30 Oct 2007 9:21] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/36620

ChangeSet@1.2576, 2007-10-30 13:16:38+04:00, gluh@mysql.com +3 -0
  Bug#31113 mysqldump 5.1 can't handle a dash ("-") in database names
  db name should be quoted if 'opt_quoted' is enabled
[30 Oct 2007 11:16] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/36631

ChangeSet@1.2576, 2007-10-30 15:11:39+04:00, gluh@mysql.com +3 -0
  Bug#31113 mysqldump 5.1 can't handle a dash ("-") in database names(2nd version)
  db name should be quoted. this code does communication with the server.
  it's always ok to quote names in this case.
[2 Nov 2007 8:29] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/36944

ChangeSet@1.2618, 2007-11-02 12:24:45+04:00, gluh@mysql.com +3 -0
  Bug#31113 mysqldump 5.1 can't handle a dash ("-") in database names
  db name should be quoted. this code does communication with the server.
  it's always ok to quote names in this case.
[16 Nov 2007 9:32] Bugs System
Pushed into 5.1.23-rc
[16 Nov 2007 9:34] Bugs System
Pushed into 6.0.4-alpha
[19 Nov 2007 5:05] Paul Dubois
Noted in 5.1.23, 6.0.4 changelogs.

mysqldump failed to handle databases containing a '-' character in
the name.
[16 Sep 2011 11:16] Rudy Broersma
Has this bug resurfaced?

#mysqldump -uadmin -p`cat /etc/psa/.psa.shadow` joomla-target > /tmp/out  

mysqldump: Couldn't execute 'use joomla-target': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-target' at line 1 (1064)

# mysqldump -V
mysqldump  Ver 10.13 Distrib 5.1.22-rc, for pc-linux-gnu (x86_64)

Using --quote-names does not fix this, as quote-names only quotes tables and columns. Quoting the database on the commandline does not fix this either.
[16 Sep 2011 11:23] Rudy Broersma
Nevermind, my version is too old. Sorry :(