I'm trying to build a script to automate the backup process for an existing database.

The problem I'm having is passing to the mysqldump command (see bellow) the database password that has two words separated by a blank space.

# database credentials
DATABASEPASSWORD="Compound Password"

# Local directory of mysqldump file

#--- end config


When I run the script, the relevant mysqldump related message is:

mysqldump: Got error: 1045: "Access denied for user 'username'@'localhost' (using password: YES)" when trying to connect

What am I doing wrong here and how to solve it?


Quote your variables:

mysqldump --host="$DATABASEHOST" \
          --user="$DATABASEUSER" \
          --password="$DATABASEPASSWORD" \
          --database="$DATABASESCHEMA" \
  > "$LOCALDIR"/"$(date +%Y%m%d)_bkp_$DATABASESCHEMA.sql"

See also: Security implications of forgetting to quote a variable in bash/POSIX shells

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.