Run MySQL queries in Bash without the password warning

X2ucV

When MySQL commands are automated in Bash, passing the password directly with -pPASSWORD can print this warning: “Using a password on the command line interface can be insecure.”

Use MYSQL_PWD

MYSQL_PWD=PASSWORD mysql DBNAME -h SERVER_IP -u USER -N -e "SELECT * FROM user_table WHERE id != 1"

Useful options

--skip-column-names, -N
--execute=statement, -e statement

-N removes column headers and -e executes the SQL statement from the command line.

Security considerations

MYSQL_PWD avoids the warning, but credentials still need protection. Prefer restricted database users, environment files outside web roots and file permissions that only allow the script owner to read secrets.

Better for production

For recurring jobs, use a MySQL option file with proper permissions or a secrets manager.

Leave a Reply