David Velasquez
David Velasquez

Reputation: 2366

Is there a way to refer to a long host name and use that reference to log into mysql?

In order to log into MySQL in the command line, I have to do the usual to log in:

mysql -u [username] -h [host name] -p

But my host name is very long, so I'm constantly having to copy it from a document and paste it in the field. Is there a way I can save it to some sort of variable and then use that variable in the host name field? This would save me some time. I've looked but can't find anything about this. Maybe it's not possible, but if it is, I would appreciate if someone can show me how to do this. Also, I am using Windows if that's helpful.

Upvotes: 0

Views: 38

Answers (2)

Barmar
Barmar

Reputation: 781833

Create a file .my.cnf in your home directory. Put:

[client_h1]
host=host name 1
user=username 1
database=db 1

[client_h2]
host=host name 2
user=username 2
database=db 2

Then you can use:

mysql --defaults-group-suffix=_h1 -p

to get the defaults from the [client_h1] group, and

mysql --defaults-group-suffix=_h2 -p

to get the defaults from the [client_h2] group.

If you have any defaults that are the same for both servers, you can put them together in a single [client] group instead of repeating them in each group.

Upvotes: 1

fvu
fvu

Reputation: 32973

You could use environment variables, either set permanently via control panel --> system, or ad hoc (meaning that their lifetime is the one of the command line window), on the command line, like this:

set h1=verververylongname.thaticannotrembereasily.com

and then

mysql -u [username] -h %h1% -p

You can make one variable per host to contact obviously

Upvotes: 1

Related Questions