Forums

Bash Script Use Database

Hi,

I am trying to build a script in which I connect to mysql, select a database, drop a table. I have this :

mysql -u layogabox -h layogabox.mysql.pythonanywhere-services.com <<EOF
USE 'layogabox$layogabox';
DROP TABLE if exists simp_postmeta;
EOF

But I get an error when launching the script in a console :

ERROR 1044 (42000) at line 1: Access denied for user 'layogabox'@'%' to database 'layogabox'

I have tried a lot of variations but still get the error, am I allowed to do that from bash? If yes, what is my error.

Thank you for your help,

It looks like you've got a shell expansion problem -- the layogabox$layogabox is being treated as layogabox followed by the contents of the environment variable layogabox, which is empty, so the first line in your SQL is equivalent to

USE 'layogabox';

...hence the error message mentioning database 'layogabox' instead of database 'layogabox$layogabox'.

This Stack Overflow answer suggests that this is the solution:

mysql -u layogabox -h layogabox.mysql.pythonanywhere-services.com <<'EOF'
USE 'layogabox$layogabox';
DROP TABLE if exists simp_postmeta;
EOF

-- note the single quotes around the first 'EOF'.

Thank you so much!

No problem, glad to help :-)