Shared server guidance · Question & answer

How do I connect to MySQL from the server without exposing a password in the command?

Pass --password without a value so the MySQL client prompts securely instead of placing the password in the command line. Specify the intended host, port and database rather than relying on defaults. The example uses TCP to a database on the same server and an existing application account.

BeginnerLinux servers with the MySQL command-line clientMySQL 8.x servers
Go to the answer

Before you start

  • An existing authorised database account and its password.
  • The actual database name and local listener port.

Instructions

1. Connect with explicit parameters

Replace appuser and appdb with approved values. The shell username and the MySQL username are separate identities. The example assumes the database is local; a remote database needs its approved encrypted connection configuration.

Remote Linux shell
mysql --protocol=TCP --host=127.0.0.1 --port=3306 --user=appuser --password appdb

Prompts for the password without adding it to the process arguments or shell history.

2. Verify the selected database and identity

Run SQL only after the mysql prompt appears. CURRENT_USER identifies the authenticated account used for privileges; USER describes the supplied connection identity.

MySQL prompt
SELECT DATABASE(), USER(), CURRENT_USER();

Reads connection identity and the selected database without accessing application records.

3. Leave the client cleanly

Exit the database prompt before returning to operating-system commands. If authentication fails, preserve the exact error code without disclosing the password.

MySQL prompt
exit

Leaves the MySQL client and returns to the server shell.

Check the result

  • The connection succeeds and the selected database and authenticated account are the ones intended.

Troubleshooting

localhost works but 127.0.0.1 fails
On Unix, localhost normally selects a UNIX socket while 127.0.0.1 selects TCP. Check the intended transport, listener and account host matching before changing credentials.

Official references

Use the documentation for your installed version when a flag, service name or platform feature differs.