mysqldump - username takes "-p" as well

in flag

I use the following script to read database credentials from a Wordpress installation:

DB_NAME=$(sed -n "s/define( *'DB_NAME', *'\([^']*\)'.*/\1/p" wp-config.php)
DB_USER=$(sed -n "s/define( *'DB_USER', *'\([^']*\)'.*/\1/p" wp-config.php)
DB_PASSWORD=$(sed -n "s/define( *'DB_PASSWORD', *'\([^']*\)'.*/\1/p" wp-config.php)
DB_HOST=$(sed -n "s/define( *'DB_HOST', *'\([^']*\)'.*/\1/p" wp-config.php)

When I echo the variables like this:

echo $DB_NAME;
echo $DB_USER;
echo $DB_HOST;

Everything is fine. When I use them in a mysqldump statement:

mysqldump --add-drop-table -u${DB_USERNAME} -p${DB_PASSWORD} ${DB_NAME} > ../backups/${SQL_BACKUP} 2>&1


mysqldump --add-drop-table -u$DB_USERNAME -p$DB_PASSWORD $DB_NAME > ../backups/$SQL_BACKUP 2>&1

I get this error message:

mysqldump: Got error: 1045: Access denied for user '-8zSkcRrgVad3F'@'localhost' (using password: NO) when trying to connect

When I use the statement as this:

mysqldump --add-drop-table -udatabaseuser -p8zSkcRrgVad3F database > ../backups/test.sql 2>&1

Everything works fine. I have no special chars in my user/database/password, but the command takes the -p8zSkcRrgVad3F as the username as well? Do I need to trim the values before or something like this?

cn flag

Looks like the username is empty, so the actual command looks like

mysqldump --add-drop-table -u -p8zSkcRrgVad3F ...

(8zSkcRrgVad3F being your password)

First, add the spaces between the flags and the variables, and second - I think the error is just you using wrong variable names. You mention testing echo $DB_USER which works, but you use $DB_USERNAME in the command.

in flag
Thanks. That's it :( I've been blind. One question though. What's the difference between $VAR and ${VAR} ?

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.