3

When I log in to mysql from a bash script such as this:

mysql --user="$USERNAME" --password="$PASSWORD" --database="$DATABASE" << EOF
# No command here
EOF;

My user is logged into the database and it works fine. But when my bash script does something as simple as

mysql --user="$USERNAME" --password="$PASSWORD" --database="$DATABASE" << EOF
show tables;
show columns from some_table;
...
# Now there are commands
EOF;

Mysql gives me this error: Access denied for user 'user'@'localhost' to database 'database' What would cause this?

To clarify, I'm perfectly able to just call mysql -uUser -pPass database from the command line, log in, then call show tables;. It's only when run from a bash script, and when that bash script includes a mysql command does my access get denied.

Furthermore is there a simple-to-use library for python/c++ that handles mysql interaction and doesn't involve having to sign up for something in order to obtain the library?

1 Answer 1

1

I always use the -e flag for passing commands in a script:

  mysql --user="$USERNAME" --password="$PASSWORD" --database="$DATABASE"  -e "show tables;"
Sign up to request clarification or add additional context in comments.

12 Comments

I'm actually calling more than one command, sorry for not specifying, for that reason I'm using the << operator.
@kjh you can specify as many command you want separated by a semicolon
Ah ok. I'll go ahead and give this a try. does it ignore newlines?
I'd assue so as long as the starting quote is on the same line as the -e
It should still work as the question wrote it. mysql will read from stdin if there's no -e option, and I have many scripts that pipe to mysql.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.