MySQLDump - Is there any way I can enter the password with -p or --password?

I have an Ubuntu server with MySQL.

On the command line, this works

  mysqldump -u root -p paydaydebt

(then I have to manually type the password)

On the command line, these do not work:

mysqldump -u root -p{password} paydaydebt 
mysqldump -u root --password={password} paydaydebt

Is there any way I can enter the password with -p or --password?


Solution 1:

You should not. Given the password at the command line is evil as it is visible to everybody issuing a ps or top.

The recommended way is to specify the password in a separate file and then add a command line switch --defaults-extra-file=/etc/mysql/mysqlpassword.cnf

The mysqlpassword.cnf then contains (only):

[mysqldump]
# The following password will be sent to mysqldump 
password="ThisIsThePassword"

Make sure that this file is only readable for the user executing mysqldump (preferable root).

Solution 2:

In addition to mailqs answer: --defaults-extra-file=/etc/mysql/mysqlpassword.cnf must be the first option in the command. Example:

mysqldump --defaults-extra-file=/etc/mysql/mysqlpassword.cnf -u root paydaydebt