docker安装 sql-server

作者: admin 分类: IT运维,SQL 发布时间: 2019-03-11 11:18

下载SQL Server镜像

docker pull microsoft/mssql-server-linux:2017-latest

命令执行过程:

euming@eumingdeMBP:~ % docker pull microsoft/mssql-server-linux:2017-latest
2017-latest: Pulling from microsoft/mssql-server-linux
59ab41dd721a: Pull complete
57da90bec92c: Pull complete
06fe57530625: Pull complete
5a6315cba1ff: Pull complete
739f58768b3f: Pull complete
0b751601bca3: Pull complete
bcf04a22644a: Pull complete
6b5009e4f470: Pull complete
a9dca2f6722a: Pull complete
Digest: sha256:9b700672670bb3db4b212e8aef841ca79eb2fce7d5975a5ce35b7129a9b90ec0
Status: Downloaded newer image for microsoft/mssql-server-linux:2017-latest

启动SQL Server容器

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Asdfgh123' \
   -p 1433:1433 --name sql-server \
   -d microsoft/mssql-server-linux:2017-latest

具体参数意义,请参看微软文档。(MSSQL_SA_PASSWORD为初始密码,初识密码必须为六个字母以上,否则无法初始化成功,可以自己修改)

命令执行过程:

euming@eumingdeMBP:~ % docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Asdfgh123' \
   -p 1433:1433 --name sql-server \
   -d microsoft/mssql-server-linux:2017-latest
3372b4f9b0d4a962da51ca45b52196c64f1e653c20e8ba0f99195aee9c3ec250

查看SQL Server容器是否启动成功:

euming@eumingdeMBP:~ % docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Asdfgh123' \
   -p 1401:1433 --name sql-server \
   -d microsoft/mssql-server-linux:2017-latest
0f0baa606ee970a507de0f990fae6269f59cbc79641f93d8c20fa71c7b9ff0aa
euming@eumingdeMBP:~ % docker container ls -a
CONTAINER ID        IMAGE                                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
0f0baa606ee9        microsoft/mssql-server-linux:2017-latest   "/opt/mssql/bin/sqls…"   3 seconds ago       Up 3 seconds        0.0.0.0:1401->1433/tcp   sql-server
euming@eumingdeMBP:~ %

docker ps -a,status 字段为 up 则执行成功

修改 SA(system administrator) 密码

通过 docker exec命令执行 sqlcmd 可以修改 SA 密码。命令如下:

euming@eumingdeMBP:~ % docker exec -it sql-server /opt/mssql-tools/bin/sqlcmd \
-S localhost -U SA -P 'Asdfgh123' \
-Q 'ALTER LOGIN SA WITH PASSWORD="qazWSX123"'

通过命令连接数据库

连接数据库,通过 sql-server 命令行工具 sqlcmd,在容器内部连接到 SQL Server
通过docker exec -it命令打开运行中容器的交互 shell。命令如下:

euming@eumingdeMBP:~ % docker exec -it sql-server "bash"
root@0f0baa606ee9:/# /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'qazWSX123'
1>
  • 如果出现>提示符,则表示登录成功
  • 其中sql-server的值就是创建容器时命名的值
  • 进入容器之后,可以通过 sqlcmd 连接上本地的 SQL Server
  • 可以直接执行sql语句操作数据库

通过DBeaver访问 SQL Server

发表评论

电子邮件地址不会被公开。 必填项已用*标注