Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

mysql warns about a password on the command line in a script

Errormysql: [Warning] Using a password on the command line interface can be insecure.

Short answer

The warning is correct: a password written after -p can be seen by other users in process listings. Do not hide the message. Remove the password from the command, using a protected option file with a [client] group or a login path made by mysql_config_editor.

  • Applies to MySQL 8.4, Linux
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the command in your script that has the password glued to -p or written as --password=value.

    grep -n -E -- '-p[^ ]|--password=' backup.sh
  2. 2Match what you see, then jump to its fix

    1. 1mysql -u root -psecret db_nameThe password is typed into the commandFix
    2. 2You cannot run mysql_config_editor on the server that runs the job, or you prefer a plain fileA protected option file is a better fit for the scriptFix

What does the error look like?

  • mysql: [Warning] Using a password on the command line interface can be insecure.

Cause 1 of 2

The password is typed into the command

1Check

Any value attached to -p or --password triggers the warning.

ps aux | grep '[m]ysql '

2Fix

Store credentials in a login path. The file .mylogin.cnf is kept in your home directory and read by the client programs. Supply the password at the prompt when you run set.

mysql_config_editor set --login-path=backup --host=localhost --user=backup_user --password
mysql --login-path=backup db_name -e "SELECT 1"
mysqldump --login-path=backup db_name > dump.sql

3Verify

The command runs with no warning.

mysql_config_editor print --all

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 2

A protected option file is a better fit for the script

1Check

Put the password in the [client] group of an option file that only your user can read.

ls -l ~/.my.cnf

2Fix

Create the file and limit access to yourself. To use a file with another name, pass --defaults-file=path as the first option on the command line; the manual says it must come before the other options.

cat > ~/.my.cnf <<'CNF'
[client]
user=backup_user
password=your-password-here
CNF
chmod 600 ~/.my.cnf

3Verify

The client uses it without any password in the command.

mysql db_name -e 'SELECT 1'

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Never filter out the warning with 2>/dev/null; it hides real errors too.
  • Use a dedicated MySQL account with only the privileges the job needs.
  • Keep credential files out of git and out of backups that others can read.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.