简体   繁体   English

如何设置mysql(@ Ubuntu-Server)自动连接到其他主机?

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

I am using Ubuntu-server. 我正在使用Ubuntu服务器。 Can I setup / configure 'mysql' to auto-connect to another host? 我可以设置/配置“ mysql”以自动连接到另一台主机吗? I mean if I type mysql on my terminal, it will connect automatically to specific host. 我的意思是,如果我在终端上键入mysql,它将自动连接到特定主机。

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 如果不存在,则创建〜/ .bash_aliases
  2. touch ~/.bash_aliases 触摸〜/ .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 在编辑器中打开〜/ .bash_aliases并添加别名MY_COMMAND_ALIAS =“ THE WAND AND WANT”或使用命令echo'alias MY_COMMAND_ALIAS =“ THE COMMAND YOU WANT”'>> >> /。bash_aliases保存
  4. Use reload source ~/.bash_profile command to reload profile or reopen the terminal 使用reload source〜/ .bash_profile命令重新加载配置文件或重新打开终端

Second part - the command you want to run mysql command, it will be something like: 第二部分-您要运行mysql命令的命令,它将类似于:

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. 除了一些小小的例外,它应该与默认的mysql命令几乎相同,例如LOAD DATA INFILE将如何在服务器上而不是本地计算机上读取文件。

You may need to do the same for mysqldump and other related commands like mysqladmin if you use those. 如果使用这些命令,则可能需要对mysqldump和其他相关命令(如mysqladmin执行相同的操作。

Be sure to specify the actual path to the mysql binary you want to run. 确保指定要运行的mysql二进制文件的实际路径。 I'm using /usr/bin/mysql here but it could be something else. 我在这里使用/usr/bin/mysql ,但这可能是其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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