Search code examples
linuxsh

MySQL statement works in mysql but not from command line


I am trying to execute a particular mysql command from the command line. When I do so, it fails, but if I execute the exact same command from within mysql interactive, it works.

Here is the command line version which fails:

$ mysql -uscott -p -e "GRANT ALL PRIVILEGES ON `myuser\_%`.* TO 'myuser'@'localhost';"

The error given is:

ERROR 1064 (42000) at line 1: 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 '* TO 'myuser'@'localhost'' at line 1

Copying and pasting just the mysql command into mysql works:

mysql> GRANT ALL PRIVILEGES ON `myuser\_%`.* TO 'myuser'@'localhost';

I have taken this command verbatim from phpmyadmin when creating a user for a database. How can I fix it so it works from the command line?


Solution

  • The * is being expanded by your shell (bash?).

    You can fix this by using single quotes around the SQL statement instead of double or escaping the * with a \