从Linux Docker容器连接到远程MSSQL数据库 [英] Connect to Remote MSSQL db from Linux Docker container

查看:1155
本文介绍了从Linux Docker容器连接到远程MSSQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 microsoft / mssql-server-linux / 映像的linux docker容器。容器目前没有任何东西,我试图连接到Windows服务器上的远程MSSQL数据库。我不知道我该怎么做

I have a linux docker container built on microsoft/mssql-server-linux/ image. The container doesn't have anything at this moment, I am trying to connect to remote MSSQL db hosted on windows server somewhere. I am not sure exactly how can I do that.

microsoft / mssql-server-linux /的文档不提供很多细节。任何帮助将不胜感激。

The documentation for microsoft/mssql-server-linux/ doesn't provide much of details. Any help would be appreciated.

更新:
我现在有容器正在工作。但是,如果我尝试通过bash脚本创建和种子化数据库,则容器将以0代替。

Updated: I have got container working now. But, the container gets exited with code 0 if I try to create and seed db through bash script.

这里是 docker文件& docker-compose 文件

version: '2'
services:
  db:
    build: .
    ports:
      - "1433:1433"
    environment:
        ACCEPT_EULA: Y
        SA_PASSWORD: Password
        PATH: /opt/mssql-tools/bin:/opt/mssql/bin:$PATH
    container_name: db

FROM microsoft/mssql-server-linux:latest
EXPOSE 1433
RUN echo $PATH
RUN mkdir -p /usr/src/app
COPY ./db/* /usr/src/app/
WORKDIR /usr/src/app
RUN ls
RUN chmod +x /usr/src/app/dbInit.sh
RUN chmod +x /usr/src/app/dbSeed.sh
CMD /bin/bash ./entrypoint.sh

这是我的 entrypoint.sh

/opt/mssql/bin/sqlservr & /usr/src/app/dbInit.sh

dbInit.sh 包含SQLscripts来创建数据库,一些表并种子。

dbInit.sh contains SQLscripts to create db, some tables and seed them. something like this.

sqlcmd -S localhost -U SA -P password -d master -Q "CREATE DATABASE dbo"

docker-compose up --build可以成功创建数据库,表格和种子。但是容器是以代码0的形式退出的。似乎像SQL Server一样,它自动关闭。

The docker-compose up --build successfully creates db, tables and seeds them. But the container is exited with code 0. Seems like SQL Server it self is closed.

推荐答案

文档很清楚请参阅 连接和查询 for更多细节

The documentation is pretty clear..see the Connect and Query for more details

1.首先您必须安装SQLCMD工具,因为它们未自动安装

1.First you have to install SQLCMD tools,as they are not installed automatically


  • 导入公共存储库GPG密钥。


curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -




  • 注册Microsoft Ubuntu存储库


  • curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

    curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list




    • 更新源列表并使用unixODBC开发人员包运行安装命令。


    • sudo apt-get update
      sudo apt-get install mssql-tools unixodbc-dev

      sudo apt-get update sudo apt-get install mssql-tools unixodbc-dev

      您可以在这里查看进一步的增强功能: 在Ubuntu上安装工具

      You can check out for further enhancements here :Install tools on Ubuntu

      现在,您可以在下面查询

      now you can query like below


      sqlcmd -S localhost -U SA -P''

      sqlcmd -S localhost -U SA -P ''

      对于远程:


      sqlcmd -S 192.555.5.555 -U SA -P

      sqlcmd -S 192.555.5.555 -U SA -P ''

      这篇关于从Linux Docker容器连接到远程MSSQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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