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

Stop the MySQL password warning with ~/.my.cnf, --defaults-extra-file or mysql_config_editor. See what ps shows, and why MYSQL_PWD is not a fix.

Source: https://valakes.com/learn/mysql-password-command-line-warning/

Last updated: 2026-11-29

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.

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

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.

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/file-permissions/) explains why that works. Set it, then look at `ps` again:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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.

```console
$ (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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/ssh-port-forwarding/) covers the tunnel. The PostgreSQL client has its own file and its own mode rule, which [the psql article](https://valakes.com/learn/psql-without-password-prompt/) covers. For the account that logs in to the server, see [the SSH keys article](https://valakes.com/learn/ssh-keys/).

## 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](https://valakes.com/guides/auto-fill-a-database-password-over-ssh/) 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.

## In Valakes

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](https://valakes.com/features/password-auto-fill/)

[Read the docs: Credentials and auto-fill](https://docs.valakes.com/credentials/)

## 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

- [End-user guidelines for password security, MySQL 8.0 reference manual](https://dev.mysql.com/doc/refman/8.0/en/password-security-user.html)
- [Using option files, MySQL 8.0 reference manual](https://dev.mysql.com/doc/refman/8.0/en/option-files.html)
- [Command-line options that affect option-file handling, MySQL 8.0 reference manual](https://dev.mysql.com/doc/refman/8.0/en/option-file-options.html)
- [mysql_config_editor, MySQL 8.0 reference manual](https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html)
- [Environment variables, MySQL 8.0 reference manual (MYSQL_PWD)](https://dev.mysql.com/doc/refman/8.0/en/environment-variables.html)

## Related articles

- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
- [How do I change permissions recursively on Linux?](https://valakes.com/learn/file-permissions/): Do not use one number for the whole tree. Run find site -type d -exec chmod 755 {} + for folders and find site -type f -exec chmod 644 {} + for files, then chown -R to set the owner and group. chmod -R 777 lets every account on the machine change your files.
- [How do I run psql without typing the password?](https://valakes.com/learn/psql-without-password-prompt/): Put one line in ~/.pgpass in the form hostname:port:database:username:password and run chmod 600 on the file. psql then finds the password itself, and psql -w makes a script fail with an error instead of waiting for a prompt that nobody can answer.
