简体   繁体   中英

How to setup mysql (@Ubuntu-Server) auto connect to other host?

I am using Ubuntu-server. Can I setup / configure 'mysql' to auto-connect to another host? I mean if I type mysql on my terminal, it will connect automatically to specific host.

Thanks.

There are two parts for the answer.

First - how to make an alias, a word that if you type it, a specific command will execute (taken from here :

  1. Create ~/.bash_aliases if not exists
  2. touch ~/.bash_aliases
  3. Open ~/.bash_aliases in a editor and append alias MY_COMMAND_ALIAS="THE COMMAND YOU WANT" or use command echo 'alias MY_COMMAND_ALIAS="THE COMMAND YOU WANT"' >> ~/.bash_aliases Save it
  4. Use reload source ~/.bash_profile command to reload profile or reopen the terminal

Second part - the command you want to run mysql command, it will be something like:

mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"

You can write a simple wrapper like this:

#!/bin/sh

/usr/bin/mysql --user $MYSQL_USER -p $MYSQL_PASSWORD --host $MYSQL_HOST $*

Where that should work almost identically to the default mysql command with a few tiny exceptions, like how LOAD DATA INFILE will read files only on the server, not your local machine.

You may need to do the same for mysqldump and other related commands like mysqladmin if you use those.

Be sure to specify the actual path to the mysql binary you want to run. I'm using /usr/bin/mysql here but it could be something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM