Connect to 2 instances of MySQL with no password interaction using command line

Viewed 745

I would like to connect to mysql database without interaction for the password (I need for using it during a batch script). I'm using this script, but before start the connection I must insert the password.

mysql -u username@databasename -p "Password" -h hostnamedatabase -P 3344 

I have tried this other approach but I have no success

mysql -u username@databasename -pPassword -h hostnamedatabase -P 3344 

Is there a way to enter the password directly without typing it in?

1 Answers

New answer

OP is on command line on a remote server (using SSH). From there, mysql needs to be used password-less - so to speak - to create user on INSTANCE1 and INSTANCE2.

That's doable also. You'd use --defaults-group-suffix switch.

~/.my.cnf

Let's start with creating a file called .my.cnf in your home directory (aka ~/.my.cnf). Put this in it.

[client1]
user=INSTANCE1-USERNAME
password=INSTANCE1-PASSWORD
database=INSTANCE1-DATABASE (this could be mysql)
host=INSTANCE1-HOSTNAME-OR-IP

[client2]
user=INSTANCE2-USERNAME
password=INSTANCE2-PASSWORD
database=INSTANCE2-DATABASE (this could be mysql)
host=INSTANCE2-HOSTNAME-OR-IP

Save the file. Do chmod 600 ~/.my.cnf to ensure only your username and root/root-like user can see it.

Now, type this to get to first server:

mysql --defaults-group-suffix=1

Then, use this to get to the second server:

mysql --defaults-group-suffix=2

Explanation

Typically ~/.my.cnf will have the following block

[client]
user=USERNAME
password=PASSWORD
database=DATABASE (this could be mysql)
host=localhost (or whatever hostname/IP)

That allows you to just type mysql and log on. MySQL looks for credentials, host, port etc. in ~/.my.cnf. If it gets that info, it'll use it to log in to MySQL. Cool. Easy enough.

--defaults-group-suffix=2 tells MySQL to look into ~/.my.cnf but not read the [client] block but instead read the [client2] block for credentials/information.

Similarly --defaults-group-suffix=1 tells MySQL to look into ~/.my.cnf and read the [client1] block for credentials/information.

That way, you can have credentials for multiple servers or databases within a single server and log on to MySQL without having to prompt/provide credentials through command line.

You can use this tool with scripts as long as the ~/.my.cnf file is in the username that is running those scripts.

Documentation

Another method using --login-path

See https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html. It shows you can log on to mysql using --login-path switch. Since the above method will work well for you, I am just adding this as a reference.

Old answer

You can actually do that. Assuming you are on Linux, create a .my.cnf file under your home directory. Type this in it:

[client]
user=username
password=yourpass

Then, you can do mysql -h host -P 3344 -D databasename

See documentation here: https://dev.mysql.com/doc/refman/8.0/en/option-files.html

Also make sure that this file is adequately protected (do chmod 600 ~/.my.cnf).

Related