Emacs如何使用ssh隧道连接到远程MySQL [英] Emacs how to use ssh tunnel to connect to remote MySQL

查看:173
本文介绍了Emacs如何使用ssh隧道连接到远程MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆远程MySQL服务器,只允许从本地主机连接。要连接到他们,请执行以下操作:

  ssh host 
mysql -uuser -psecret -hhost.myhost.com

在emacs中,我配置了连接到本地MySQL,使用sql-mysql-mode:

 (setq sql-connection-alist 
'((pool-a
(sql-product'mysql)
(sql-serverlocalhost)
(sql-useruser)
(sql-passwordsecret)
(sql-database)
sql-port 3306))
))

(defun sql-connect-preset(name)
连接到'sql-connection-alist'中列出的预定义SQL连接
(eval`(let,(cdr(assoc name sql-connection-alist))
(flet((sql-get-login(& rest what)))
-product-interactive sql-product)))))

(defun sql-local()
连接到本地MySQL服务器
(interactive)
(sql-connect-preset'pool-a)
(delete-other-windows))

(def ine-key global-map [f10]'sql-local)

所以,每次点击 F10 ,我得到MySQL shell。



是否可以调整sql-mysql,所以它连接到外部机器通过ssh并在该机器上使用mysql程序,所以我可以从Emacs到任何地方连接?

解决方案

Sql模式使用 default-directory 当它建立到数据库的连接时,所以如果变量是TRAMP格式,ssh(或任何)将被用来连接到远程主机,然后使用数据库客户端本地要自动执行,您可以执行类似

 (defadvice sql-mysql(围绕sql-mysql-around activate))
SSH到linux,然后连接
(let((default-directory/ssh:host.myhost.com:))
ad-do-it))

您当然可以替换/ ssh:host.myhost.com:使用函数调用,可以询问您连接哪个主机等。


I have a bunch of remote MySQL servers, that allow connection only from localhost. To connect to them I do the following:

ssh host
mysql -uuser -psecret -hhost.myhost.com

In emacs I configured the connection to local MySQL, using sql-mysql-mode:

(setq sql-connection-alist
      '((pool-a
         (sql-product 'mysql)
         (sql-server "localhost")
         (sql-user "user")
         (sql-password "secret")
         (sql-database "")
         (sql-port 3306))
        ))

(defun sql-connect-preset (name)
  "Connect to a predefined SQL connection listed in `sql-connection-alist'"
  (eval `(let ,(cdr (assoc name sql-connection-alist))
    (flet ((sql-get-login (&rest what)))
      (sql-product-interactive sql-product)))))

(defun sql-local ()
  "Connect to the local MySQL server"
  (interactive)
  (sql-connect-preset 'pool-a)
  (delete-other-windows))

(define-key global-map [f10] 'sql-local)

So, every time I click F10, I get MySQL shell.

Is it possible to tune sql-mysql, so it connected to external machine via ssh and used mysql program on that machine, so I could connect from Emacs to everywhere?

解决方案

Sql mode uses default-directory when it establishes the connection to the database, so if the variable is in TRAMP format, ssh (or whatever) will be used to connect to the remote host first, then use the database client locally. To automate that you can do something like

(defadvice sql-mysql (around sql-mysql-around activate)
  "SSH to linux, then connect"
  (let ((default-directory "/ssh:host.myhost.com:"))
    ad-do-it))

You can of course replace "/ssh:host.myhost.com:" with the function call that would ask you which host to connect, etc.

这篇关于Emacs如何使用ssh隧道连接到远程MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆