无法使用sqlcmd通过SSH隧道访问SQL数据库 [英] Unable to reach SQL database through SSH tunnel using sqlcmd

查看:197
本文介绍了无法使用sqlcmd通过SSH隧道访问SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过跳转框(Azure VM)从本地计算机连接到Azure SQL DB.我使用以下命令设置端口转发:

I need to connect to a an Azure SQL DB from my local machine through a jump box (Azure VM). I set up the port forwarding using the command:

ssh -fN -L 41433:my-db.database.windows.net:1433 me@jump-box

我可以确认隧道已建立,因为在详细模式下我会看到消息

I can confirm the tunnel is set up because in verbose mode i see the message

Local connections to LOCALHOST:41433 forwarded to remote address my-db.database.windows.net:1433

现在,当我运行

sqlcmd -S 127.0.0.1,41433 -U username -P password -d db 

我收到以下错误消息

Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : [Microsoft][ODBC Driver 13 for SQL Server]
[SQL Server]Cannot open server "127.0.0.1" requested by the login.  The login failed..

我已确保/etc/ssh/sshd_config具有行GatewayPorts yesAllowTcpForwarding yes

有人可以帮我弄清楚我在做什么错吗?

Could someone help me figure out what I am doing wrong?

推荐答案

Azure SQL Database需要知道您的用户名用于哪个数据库实例.通常使用服务器名称来完成.如果您将服务器名称指定为127.0.0.1,则Azure无法知道您的实例.

Azure SQL Database needs to know what database instance your username is for. This is normally done with the server name. If you specify 127.0.0.1 as your server name, Azure cannot know your instance.

最好的方法是在登录名中指定实例,例如username@my-db.database.windows.net.

The best way is to specify the instance in the login name, e.g. username@my-db.database.windows.net.

sqlcmd -S 127.0.0.1,41433 -U username@yourdb.database.windows.net -P password -d db 

通过这种方式,您无需修改​​主机文件,并且可以使用相同的隧道同时连接到多个Azure SQL数据库实例.我希望这可以帮助您安全地连接到Azure SQL数据库.

This way you do not need to modify your hosts-file and you can have connections to multiple Azure SQL Database instances simultaneously with same tunneling. I hope this helps you connecting securely to Azure SQL Database.

这篇关于无法使用sqlcmd通过SSH隧道访问SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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