简体   繁体   English

使用 SQL Server Management Studio 远程连接到 docker 容器

[英]Using SQL Server Management Studio to remote connect to docker container

Context : I am trying to build a development SQL Server that I can continue to learn SQL on and use Microsoft SQL Server Management Studio ( SSMS ) to access on a Windows PC.上下文:我正在尝试构建一个开发 SQL Server,我可以继续在其上学习 SQL 并使用 Microsoft SQL Server Management Studio ( SSMS ) 在 Windows PC 上访问。

So I have the AdventureWorks database sitting on a Docker Container for MS SQL Server 2017 running on a DigitalOcean Ubuntu 16.04 box.因此,我将 AdventureWorks 数据库放在了一个运行在 DigitalOcean Ubuntu 16.04 机器上的 MS SQL Server 2017 的 Docker 容器上。 From my Mac I can remote SSH in to the server, access the container and query the database.在我的 Mac 上,我可以远程 SSH 连接到服务器,访问容器并查询数据库。

However I wish to use SSMS on my Windows PC and am unsure how I begin to connect to the remote box.但是,我希望在我的 Windows PC 上使用 SSMS,但我不确定如何开始连接到远程盒子。 In the picture below, there are no options to specify an SSH key or to even login in to the Ubuntu box, only to access the SQL server.在下图中,没有选项可以指定 SSH 密钥,甚至无法登录 Ubuntu 框,只能访问 SQL 服务器。

在此处输入图像描述

Is this even possible?这甚至可能吗?

connect via SSMS using the public IP address, followed by comma separator and then the port (xxx.xx.xx.xxx,port)使用公共 IP 地址通过 SSMS 连接,后跟逗号分隔符,然后是端口 (xxx.xx.xx.xxx,port)

You'll also need the sa credentials to make this work.您还需要 sa 凭据才能完成这项工作。

In your case, Server Name input will become Server IP, port#在您的情况下,服务器名称输入将成为服务器 IP、端口#

远程设置图像

No need of ssh, you just need to expose SQL Server service to the internet.不需要 ssh,您只需要将 SQL Server 服务公开到 Internet。

When Using SQL Server Management Studio to connect to the docker container on local machine , you can use localhost ip.使用 SQL Server Management Studio 连接本地机器上的 docker 容器时,可以使用 localhost ip。 In that case Server Name input will become 127.0.0.1,port#在这种情况下,服务器名称输入将变为127.0.0.1,port#

为了清楚起见,我附上了我的 SSMS 连接对话框的图像

Following instruction of this site solved it for me on Windows Containers: https://www.sqlshack.com/sql-server-with-a-docker-container-on-windows-server-2016/ It was possible to connect to SQL Server instantly from SSMS.按照本网站的说明在 Windows 容器上为我解决了这个问题: https ://www.sqlshack.com/sql-server-with-a-docker-container-on-windows-server-2016/ 可以连接到 SQL从 SSMS 即时服务器。

Give it a try if this is also working on Linux containers with this command:如果这也适用于使用以下命令的 Linux 容器,请尝试一下:

docker run --name mssqltrek-con1 -d -p 1433:1433 -e sa_password=My$eCurePwd123# -e ACCEPT_EULA=Y microsoft/mssql-server-windows-developer

After running this you can retrieve the correct ip with:运行此之后,您可以使用以下命令检索正确的 ip:

docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" mssqltrek-con1

In my case it was the Cisco VPN that was blocking the host to connect to container IP.在我的例子中,是 Cisco VPN 阻止了主机连接到容器 IP。 I had to uncheck the "Cisco Any Connect Network Access Manager" as shown in the image below for it to work.我必须取消选中“Cisco Any Connect Network Access Manager”,如下图所示,它才能工作。 It ensures that the VPN no longer manages the connection.它确保 VPN 不再管理连接。

在此处输入图像描述

You can connect to SQL docker server您可以连接到 SQL docker 服务器

  1. Using IP of Machine on which docker image is hosted,port使用托管 docker 镜像的机器的 IP,端口

IP_Of_Machine,Port IP_Of_Machine,端口

  1. Provide User - sa (default) and password.提供用户 - sa(默认)和密码。

  2. you can also do it command>你也可以这样做命令>

docker exec -it <container_id|container_name> /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P <your_password> docker exec -it <container_id|container_name> /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P <your_password>

This isn't exactly the same problem as the OP's but I created a sql docker container and then couldn't log in to it from SQL Server Management Studio.这与 OP 的问题并不完全相同,但我创建了一个 sql docker 容器,然后无法从 SQL Server Management Studio 登录到它。 It turned out that the sa password I used wasn't secure enough.原来我用的sa密码不够安全。 Once I deleted my container and recreated one with a more secure password it let me connect to it from SQL Server Management Studio.一旦我删除了我的容器并使用更安全的密码重新创建了一个容器,它就可以让我从 SQL Server Management Studio 连接到它。 If you do not specify a strong enough password it still creates the container OK - you just can't log in to it!如果您没有指定足够强的密码,它仍然可以创建容器 - 您只是无法登录它! Hopefully this will save someone some time.希望这可以节省一些时间。

I also faced same issue when I try to connect through SSMS, even I tried with localhost,1433 it doesn't help me out.当我尝试通过 SSMS 连接时,我也遇到了同样的问题,即使我尝试使用 localhost,1433 也无济于事。 enter image description here在此处输入图像描述

After some R&D I found the way to solve it经过一些研发,我找到了解决它的方法

  • Open Kubernetes which will be installed as part of docker toolbox (mine was win 10 home which will not support hyper-v)打开 Kubernetes,它将作为 docker 工具箱的一部分安装(我的是 win 10 home,它不支持 hyper-v)
  • click on sql container which will be in left side of app单击将在应用程序左侧的 sql 容器
  • copy the IP address which will be in right side of app复制将在应用右侧的IP地址
  • use IP address with port like XXX.XXX.XX.XX,1433 along with credentials in SSMS在 SSMS 中使用带有 XXX.XXX.XX.XX、1433 等端口的 IP 地址以及凭据

enter image description here在此处输入图像描述

I benefited from the answers on this page, however, i had to go through my own tweak.我从这个页面上的答案中受益,但是,我必须进行自己的调整。 for some reason, in my case, it didnt accept localhost,1433 but it did accept 127.0.0.1,1433出于某种原因,就我而言,它不接受 localhost,1433 但它确实接受了 127.0.0.1,1433

In my case I was using docker-compose and was unable to connect.就我而言,我使用的是docker-compose并且无法连接。 I fixed this by explicitly specifying the port in the docker compose file.我通过在 docker compose 文件中明确指定端口来解决此问题。

mysqldb:
    ports:
      - "1433:1433"
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=SuperSecretPassword!23
    image: registry/mysqlimage

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

相关问题 使用SQL Management Studio连接到Linux(docker)上的SQL Server - Connect to SQL Server on Linux (docker) using SQL Management Studio 无法通过SQL Server Management Studio连接到SQL Server Linux Docker容器 - Unable to connect to SQL Server Linux Docker container via SQL Server Management Studio 使用SQL Server 2008 Management Studio无法连接到远程SQL Server - Fail to connect to a remote SQL server with SQL Server 2008 Management Studio 我可以在没有管理工作室的情况下连接远程sql server数据库吗 - Can I connect remote sql server database without management studio 无法从本地管理工作室连接到远程SQL Server数据库 - Unable to connect to remote SQL Server database from local management studio Docker 容器无法连接到远程服务器上的 SQL 服务器 - Docker container can't connect to SQL Server on a remote server 使用SQL Server Management Studio的SQL Server远程连接 - SQL Server remote connection using SQL Server Management Studio 无法通过 sql management studio 从 docker 容器连接到 ms sql 数据库 - Can't connect to ms sql database from docker container by sql management studio 不在域上时,使用Windows凭据使用Management Studio连接到SQL Server - Connect to SQL Server with Management Studio using a Windows Credential while not on the domain 无法使用 Management Studio 连接到 RDS SQL Server 数据库 - Cannot connect to RDS SQL Server Database using Management Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM