How do I get rid of "Using a password on the command line interface can be insecure"?

The warning appears because you gave the password as a value, as in -ps3cret, and then it sits in the command line where ps can show it. Move it into a [client] section of ~/.my.cnf with mode 600, or into a login path made by mysql_config_editor, and run mysql without it.

Last updated

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), .

The warning means that you wrote the password into the command, for example -ps3cret-pass. On some systems other accounts can read a command line with ps, so MySQL warns you. Do not hide the message. Put the password in a [client] section of ~/.my.cnf, give the file mode 600, and run mysql without -p and without a value. For one job you can keep a separate file and pass it with --defaults-extra-file. mysql_config_editor can also store the login under a name, called a login path.

The commands below ran on the system named in the check line, against a MySQL 8.0 server on the same machine. The account app has a password and connects over TCP.

$ mysql --version
mysql  Ver 8.0.46-0ubuntu0.24.04.5 for Linux on x86_64 ((Ubuntu))

The warning, and what ps shows

This is the warning itself. It goes to standard error, before the result:

$ mysql -h 127.0.0.1 -u app -ps3cret-pass appdb -BNe 'select current_user()'
mysql: [Warning] Using a password on the command line interface can be insecure.
app@localhost

While the client runs, ps lists its command line. The -e option of ps selects every process, whoever owns it:

$ mysql -h 127.0.0.1 -u app -ps3cret-pass appdb -BNe 'select sleep(3)' > /dev/null 2>&1 &
$ sleep 1; ps -eo args= | grep '^mysql '
mysql -h 127.0.0.1 -u app -px xxxxxxxxx appdb -BNe select sleep(3)
$ wait

The client had replaced the password with x characters one second after it started. The manual says that MySQL clients overwrite the argument during startup, that the value is visible for a short interval before that, and that on some systems the overwriting does not work. The manual adds that history files such as ~/.bash_history can hold a password typed on a command line.

Typing -p with no value avoids all of this. mysql asks for the password and prints no warning. It suits a person at a keyboard and cannot work in a script:

$ timeout --foreground 3 mysql -h 127.0.0.1 -u app -p appdb -e 'select 1'; rc=$?; echo; echo "exit status $rc"
Enter password:
exit status 124

Put the password in ~/.my.cnf

Client programs read the [client] group of the option files. ~ stands for your home directory, so the file is ~/.my.cnf. Write the user, the password and the host once:

$ printf '[client]\nuser=app\npassword=s3cret-pass\nhost=127.0.0.1\n' > ~/.my.cnf
$ stat -c '%a %n' ~/.my.cnf
644 /home/dev/.my.cnf
$ mysql appdb -BNe 'select current_user()'
app@localhost

There is no warning, although the file has mode 644 and any other account can read it. The manual tells you to set mode 400 or 600, and the permissions article explains why that works. Set it, then look at ps again:

$ chmod 600 ~/.my.cnf
$ mysql appdb -BNe 'select sleep(3)' > /dev/null 2>&1 &
$ sleep 1; ps -eo args= | grep '^mysql '
mysql appdb -BNe select sleep(3)
$ wait

The password is not in the command line any more. Every program that reads [client] uses the same file, not only mysql:

$ mysqladmin ping
mysqld is alive

--defaults-extra-file for one task

A backup job or a script may need a login that your own account does not use. Keep it in a file of its own, with mode 600, and name the file when you run the program:

$ mv ~/.my.cnf ~/backup.cnf
$ mysql --defaults-extra-file=$HOME/backup.cnf appdb -BNe 'select current_user()'
app@localhost
$ mysql --defaults-extra-file=$HOME/backup.cnf --print-defaults
mysql would have been started with the following arguments:
--user=app --password=***** --host=127.0.0.1

--print-defaults shows what the program would receive from the files. The password is masked. The option must come before the others. The manual says that these options must be given first, and mysql shows what happens otherwise:

$ mysql appdb --defaults-extra-file=$HOME/backup.cnf -BNe 'select 1'
mysql: [ERROR] unknown variable 'defaults-extra-file=/home/dev/backup.cnf'.

mysql_config_editor and the login path

mysql_config_editor is in the Ubuntu package mysql-client-core-8.0. It stores a login path, a named set of host, user and password, in ~/.mylogin.cnf. It reads the password from the terminal, so the example gives it through a pipe and script. At a keyboard you type it at the Enter password: prompt.

$ (sleep 1; printf 's3cret-pass\n') | script -qec 'mysql_config_editor set --login-path=app --host=127.0.0.1 --user=app --password' /dev/null
Enter password:
$ stat -c '%a %n' ~/.mylogin.cnf
600 /home/dev/.mylogin.cnf
$ mysql_config_editor print --all
[app]
user = "app"
password = *****
host = "127.0.0.1"
$ mysql --login-path=app appdb -BNe 'select current_user()'
app@localhost

print masks the password with *****. The file is not plain text. The manual calls it obfuscated and says it is not likely to deter a determined attacker, so mode 600 still matters. Here the password is not found in the file as text:

$ grep -c s3cret ~/.mylogin.cnf; true
0

The tool does check the mode. With group and others allowed in, mysql warns and ignores the file. It then connects as your own account name with no password, and the server refuses:

$ chmod 644 ~/.mylogin.cnf; mysql --login-path=app appdb -BNe 'select 1'
mysql: [Warning] /home/dev/.mylogin.cnf should be readable/writable only by current user.
ERROR 1045 (28000): Access denied for user 'dev'@'localhost' (using password: NO)
$ chmod 600 ~/.mylogin.cnf

Mistakes people make

You silence the warning with 2>/dev/null. The message is gone and the password is still in the command line.

You set MYSQL_PWD. The manual calls this extremely insecure and says it should not be used, and that the variable is deprecated as of MySQL 8.0. The manual also says that an option file is preferable to environment variables in many cases.

You put --defaults-extra-file after another option. mysql then reads it as an option it does not know, as shown above.

You leave ~/.my.cnf at the mode your umask gave it. That was 644 above, and anyone on the machine can read the file.

The database is on another machine and you reach it through a tunnel. Write host=127.0.0.1 and the local port in the file. The port forwarding article covers the tunnel. The PostgreSQL client has its own file and its own mode rule, which the psql article covers. For the account that logs in to the server, see the SSH keys article.

In Valakes

When mysql asks for the password, Valakes can answer from its vault once a credential is bound to mysql on that host. The guide shows the steps. The Help describes a related risk. A placeholder written inside the command, such as curl -u user:{{cred:http}}, is a last resort for you and never for an agent, and a dialog warns that the password will then appear in the process list on the server.

mysql is one of the ten tools Valakes has a rule for. Bind a saved credential to it on a host, and Valakes answers the password prompt when mysql asks, so nothing about the password goes into the command. Feature: Password auto-fill

Questions

Does MySQL refuse an option file that other users can read?

No. The manual says MySQL ignores option files that are world-writable, and it tells you to set mode 400 or 600 yourself so that nobody else can read the password. The test above used a file with mode 644 without a complaint.

What is the difference between .my.cnf and .mylogin.cnf?

.my.cnf is a text file that you edit and that can hold any option. .mylogin.cnf is written by mysql_config_editor, is obfuscated, and holds only host, password, port, socket and user for each login path.

How do I remove a login path?

Run mysql_config_editor remove --login-path=name. With no other option it removes the whole login path, and with an option such as --password it removes only that option.

References

The app is free.

Get notified at launch

macOS is coming to the MacĀ AppĀ Store. iOS and Android later.