5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Docker で SQL Server を使う

Last updated at Posted at 2023-05-01

参考ページ
クイック スタート:Docker を使用して SQL Server Linux コンテナー イメージを実行する

Docker の設定

docker pull mcr.microsoft.com/mssql/server:2022-latest
sudo docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Password1" \
   -p 1433:1433 --name sql1 --hostname sql1 \
   -d \
   mcr.microsoft.com/mssql/server:2022-latest

ログイン

docker exec -it sql1 "bash"

SQL Server に接続

接続スクリプト

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P Password1

実行例

mssql@sql1:/$  /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P Password1
1> select @@version
2> go
                                                                                                                                                                                                                                                                                                            
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2022 (RTM-CU11) (KB5032679) - 16.0.4105.2 (X64) 
	Nov 14 2023 18:33:19 
	Copyright (C) 2022 Microsoft Corporation
	Developer Edition (64-bit) on Linux (Ubuntu 22.04.3 LTS) <X64>                                                                                                      

(1 rows affected)
1>

データベースの作成

$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P Password1
1> create database city
3> use city
4> go
Changed database context to 'city'.
1> create table cities  (id nvarchar (10) primary key,name nvarchar (20),population int ,date_mod datetime)
2> go
1> insert into cities (id,name,population,date_mod) values ('t1071',N'前橋',48517,'2001-5-24')
2> go

(1 rows affected)
1> insert into cities  (id,name,population,date_mod) values ('t1072',N'高崎',19536,'2001-3-15')
2> go

(1 rows affected)
1> insert into cities (id,name,population,date_mod) values ('t1073',N'桐生',89714,'2001-7-30')
2> go

(1 rows affected)
1> select id,name,population,date_mod from cities
2> go
id         name                 population  date_mod               
---------- -------------------- ----------- -----------------------
t1071      前橋                         48517 2001-05-24 00:00:00.000
t1072      高崎                         19536 2001-03-15 00:00:00.000
t1073      桐生                         89714 2001-07-30 00:00:00.000

(3 rows affected)
1>exit
$

ベースになっている OS

mssql@sql1:/$ cat /etc/os-release 
PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

外部からの接続

Docker の IP アドレスを調べる

  1. Container ID を調べる
  2. $ docker ps
    CONTAINER ID   IMAGE                                        COMMAND                  CREATED      STATUS          PORTS                                       NAMES
    38dab9d6364d   mcr.microsoft.com/mssql/server:2022-latest   "/opt/mssql/bin/perm…"   7 days ago   Up 37 minutes   0.0.0.0:1433->1433/tcp, :::1433->1433/tcp   sql1
    
  3. CONTAINER ID が、38dab9d6364d の IPアドレスを調べる
  4. $ docker inspect 38dab9d6364d | grep IPAddress
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.2",
                        "IPAddress": "172.17.0.2",
    

接続

$ sqlcmd -S 172.17.0.2 -U SA -P Password1 -C
1> select * from cities
2> go
id         name                 population  date_mod               
---------- -------------------- ----------- -----------------------
t1071      前橋                         48517 2001-05-24 00:00:00.000
t1072      高崎                         19536 2001-03-15 00:00:00.000
t1073      桐生                         89714 2001-07-30 00:00:00.000

(3 rows affected)

-C をつけないと次のようになります。

$ sqlcmd -S 172.17.0.2 -U SA -P Password1
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : SSL Provider: [error:0A000086:SSL routines::certificate verify failed:self-signed certificate].
Sqlcmd: Error: Microsoft ODBC Driver 18 for SQL Server : Client unable to establish connection.

Ubuntu 23.04 に sqlcmd をインストール

sudo apt install mssql-tools18 unixodbc-dev

環境設定

$HOME/.bashrc
(省略)
export PATH=$PATH:/opt/mssql-tools18/bin

参考情報

sqlcmd - ユーティリティの使用

サーバーの証明書を信頼し、その有効性を検証しないように設定するには、
クライアント側で -C スイッチを使用します。 
このオプションは、ADO.net オプションの TRUSTSERVERCERTIFICATE = trueと同等です。

SQL文

データベースの一覧

SELECT name, database_id, create_date FROM sys.databases
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?