Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the MySQL Command Line Interface (CLI) have connection environment variables?

Does the MySQL Command Line Interface (CLI) or "shell" offer the ability to establish a connection using environment variables? The PostgreSQL CLI psql does via libpq with connection variables:

  • PGHOST
  • PGPORT
  • PGDATABASE
  • PGUSER
  • PGPASSWORD

I know I can connect via command-line switches and a ~/my.cnf file, but I'm wondering if there is an environment variable option analogous to the way psql works. Thanks!

like image 483
David Ventimiglia Avatar asked Jan 26 '26 07:01

David Ventimiglia


1 Answers

Yes, for some of them: https://dev.mysql.com/doc/refman/8.0/en/environment-variables.html

  • MYSQL_HOST
  • MYSQL_TCP_PORT
  • There is no environment variable for the schema to use as the default schema. Client should specify that in the connection DSN, or else the USE schema statement after connecting.
  • There is no environment variable for the MySQL username.
  • MYSQL_PWD but this is deprecated and insecure and will be removed in a future version of MySQL. It's insecure for PostgreSQL for the same reason: another user can view your client's environment variables using ps.

It's more secure to use ~/.my.cnf, or you can specify a different option file to use. You can even store the username/password in an encrypted options file. See https://dev.mysql.com/doc/refman/8.0/en/option-files.html

like image 95
Bill Karwin Avatar answered Jan 28 '26 00:01

Bill Karwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!