0

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_PASSWORD;
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

or

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?

Torben
  • 123
  • 2

1 Answers1

1

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.

GChuf
  • 266
  • 1
  • 7
  • Thanks. That's it :( I've been blind. One question though. What's the difference between $VAR and ${VAR} ? – Torben May 14 '22 at 15:37