Escaping a password using mysqldump console

57

13

I am running a mysqldump via a bash script and have encountered a problem with a password that contains special characters.

mysqldump -hlocalhost -uUSERNAME -pPA$$W0RD DATABASE | 
                                gzip > /home/USERNAME/backups-mysql/BACKUP.gz

How do I escape the password?

psynnott

Posted 2010-03-25T09:49:57.753

Reputation: 1 615

Answers

85

I found the answer. You have to quote the password, like this:

mysql -u root -p'PASSWORD'

You must do this if the password has any of the following characters: * ? [ < > & ; ! | $ ( )

psynnott

Posted 2010-03-25T09:49:57.753

Reputation: 1 615

1On Windows I found I had to use double quotes. Single quotes did not work. (MySQL 5.6) – TheStoryCoder – 2015-03-25T08:08:40.297

1blanks in the password also require the ' ' – Hafenkranich – 2018-10-22T12:43:05.217

Do you know how to escape apostrophes within the password? – Steve Mayne – 2013-05-14T20:00:18.100

3@SteveMayne i think its just a backslash before it – psynnott – 2013-05-15T06:33:23.770

3parenthesis also need to be in quotes. – Félix Gagnon-Grenier – 2014-06-05T17:12:21.770

12

when you use the quotes, make sure there is no space :
between -p and 'PASSWORD' or
between --password= and 'PASSWORD'

correct:
mysql -u root -p'PASSWORD'
mysql -u root --password='PASSWORD'

does not work:
mysql -u root -p 'PASSWORD'
mysql -u root --password = 'PASSWORD'

you can also define a variable and then use it for the command (still with no spaces in between) MSQLPWD='PASSWORD'
mysql -u root -p$MSQLPWD

MReiter

Posted 2010-03-25T09:49:57.753

Reputation: 121

2

Depends on your shell. Are you using Microsoft Windows or Linux? If you are using Linux/BASH then it is likely that $$ is being interpreted as your current process ID. Have you tried putting a backslash in front of each dollar sign? e.g.

mysqldump \
  -hlocalhost \
  -uUSERNAME \
  -pPA\$\$W0RD \
  DATABASE \
| gzip -c \
> /home/USERNAME/backups-mysql/BACKUP.gz

Note that gzip probably requires the "-c" option if you want to compress to STDOUT.

PP.

Posted 2010-03-25T09:49:57.753

Reputation: 1 995

The password I am using is not PA$$W0RD but I used this as an example. The actual password I am using has an ampersand and this is what is causing the problem. I used the backslash as you suggested but it did not work. – psynnott – 2010-03-25T11:35:35.847

2

Try backslashing (\) those special chars.

antichris

Posted 2010-03-25T09:49:57.753

Reputation: 1 038