Emacs how to use ssh tunnel to connect to remote MySQL

岁酱吖の 提交于 2019-12-03 03:30:35

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.

Take a look to 'sql-mysql-program' to "and 'sql-mysql-options'. You can bind the first to "ssh" and the second to "host mysql". If emacs does not insert 'sql-mysql-login-params' between them, you will be in the clear. If that is not the case, you need to create a script that just does "ssh host mysql" and then point 'sql-mysql-program' to that script.

If you need to use both tunneled and not tunneled mysql connections at the same time, you might consider adding a new element to 'sql-product-alist' (something like "tunneled-mysql").

As of Emacs 25.1 there is a new sql-default-directory option in the sql-connection-alist. Setting this for each server that needs it allows you to use the backend program from any given server without any defadvice or product-specific tricks. Just the setting to your the connection like:

(sql-default-directory "/host.myhost.com:")

Or, if you need to specify ssh for some reason:

(sql-default-directory "/ssh:host.myhost.com:")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!