3132. Installing Microsoft SQL Server in Docker
SQL Server and Docker

  •  

Create Microsoft SQL Server container in Docker and use Oracle SQL Developer as client tool.

1. What is SQL Server?

SQL Server is a relational database management system (RDBMS) developed by Microsoft. SQL Server is originally released to Windows platform. Recently, Microsoft released the linux version. In this tutorial, we will use docker to install SQL Server for Linux. Thus, we can use it on Mac.

2. Prerequisite

2.1 Docker Machine with Large Storage

The SQL Server container requires at least 3.25 GB of RAM and large storage. If you are unable to install it because of the ‘no space left on device’ error, you have to create a new docker machine with large storage assigned. For more details, refer to Creating Docker Machine with More Disk Space.

3. Creating SQL Server Container in Kitematic

3.1 Creating SQL Server Container

Search mssql-server-linux(Released by Microsoft) in Kitematic, click Create button. image
Kitematic will start to download the image and create container for it. image
The installation takes several minutes. Finally, the container is created but it’s in ‘stopped’ status. We have to make some changes before using it. image

3.2 Environment Variables

Switch to Settings tab, add two environment variables. Accept the license by setting ACCEPT_EULA to Y. And create password for default user sa.

  • ACCEPT_EULA=Y
  • MSSQL_SA_PASSWORD=Abc%123456789

image

3.3 Interactive Shell

Click the ‘EXEC’ button to start an interactive shell. image Now, we are in the bash shell for sql server. image

4. Creating SQL Server Container in Command Line

4.1 Creating SQL Server Container

Download mssql-server-linux image and create container for it.

$ docker pull microsoft/mssql-server-linux
$ docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=Abc%123456789' -p 1401:1433 --name mssql -d microsoft/mssql-server-linux

What is this command doing?

  • Create a mssql-server-linux container named mssql.
  • Set environment variable ACCEPT_EULA to Y.
  • Set environment variable MSSQL_SA_PASSWORD to Abc%123456789.
  • Expose 1433 and map to 1401 for outside world to connect to this SQL Server database.

Check the running containers with following command.

$ docker ps -a

image
In Kitematic, we also see a new container. image
If the container is not launched properly, check logs with following command to get some clues.

$ docker logs <containerid>

4.2 Interactive Shell

Start an interactive bash shell with following command.

docker exec -it mssql "bash"

image

5. Using SQL Server

5.1 Connecting SQL Server

Inside the interactive shell, connect SQL Server locally with sqlcmd.

$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Abc%123456789'

5.2 Creating Database

Run the following sql script to create new database named TestDB.

 
 
CREATE DATABASE TestDB
 
SELECT Name from sys.Databases
 
GO

image

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

5.3 Creating Table and Inserting Data

Run the following sql script to create new table named Inventory, and create two rows for it.

 
 
USE TestDB
 
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
 
INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
 
GO

image

5.4 Querying Data

Run the following sql script to find rows whose quantity is larger than 152.

 
 
SELECT * FROM Inventory WHERE quantity > 152;
 
GO

image

6. SQL Client Tool

It’s more convenient to use UI client tool to manipulate database.

6.1 Getting Oracle SQL Developer

Go to http://www.oracle.com/technetwork/developer-tools/sql-developer/downloads/index.html, download SQL Developer for Mac OSX. Unzip the package file, there is only one file named SQLDeveloper.

6.2 Getting Plugin File

Go to https://sourceforge.net/projects/jtds/files/, downland jtds-1.3.1-dist.zip. Extract jtds-1.3.1.jar from the package file. It is a plugin module for SQL Server and Sybase JDBC.

6.3 Importing Plugin to SQL Developer

Launch SQL Developer, go to -> Preferences -> Database -> Third-party JDBC Drivers, add the jar file. image

6.4 Creating Connection

In SQL Developer, New Connection.., provide the user, password, host and port mentioned when we create the SQL Server container. Test and Connect. image
Connection is created. After expanding the nodes by level, you will see the database and table we created through bash shell. image

6.5 Running Query

Right-click on the TestDB database, and choose Select Default Database. image
Then, apply. image
In the worksheet, input the following sql script and run. You will see all rows in table Inventory.

SELECT * FROM Inventory;

image

7. Others

7.1 Restoring Database with Backup File

The following command copies the backup file named ShoeStore.bak to the root directory of SQLServer container named mssql.

$ docker cp ShoeStore.bak mssql:/ShoeStore.bak

Go to interactive bash shell and connect to SQL Server.

$ docker exec -it mssql "bash"
$ /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Abc%123456789'

Use the following sql script to restore the database.

RESTORE DATABASE ShoeStore
FROM DISK = '/ShoeStore.bak'
WITH MOVE 'ShoeStore' TO '/var/opt/mssql/data/ShoeStore.mdf',
MOVE 'ShoeStore_Log' TO '/var/opt/mssql/data/ShoeStore_Log.ldf'
GO

For .Net Applications, the connectionString looks like below. 1401 is the port number.

<add name="EFDbContext" connectionString="server=192.168.99.100,1401;database=ShoeStore;uid=sa;pwd=Abc%123456789;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />

8. References

[source] https://jojozhuang.github.io/tutorial/docker/installing-microsoft-sql-server-in-docker/

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 86288
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 78744
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95487
82 sqlrelay (Queue 서버 설치) SQL Relay는 강력한 데이터베이스 연결 관리 솔루션입니다 file 졸리운_곰 2020.02.17 1887
81 MySQL: 대량 접속 처리하기 졸리운_곰 2020.02.17 1625
80 My-SQL 연산함수/ISNULL/group by/compute 졸리운_곰 2020.02.01 1565
79 MySQL 세자리 마다 콤마 찍기 file 졸리운_곰 2020.01.24 1018
78 docker mysql 한글 깨짐 해결 & 초기 설정 졸리운_곰 2020.01.23 1979
77 select 결과를 update하는 방법은 여러가지가 존재합니다. 졸리운_곰 2019.03.02 1196
76 DB 중복 개수 확인 file 졸리운_곰 2019.02.16 1604
75 여러 row를 1개의 컬럼으로 리턴 ( GROUP_CONCAT ) 졸리운_곰 2019.01.29 1138
74 MySQL CONCAT 함수로 2개 이상의 필드(컬럼) 결합하기 졸리운_곰 2019.01.29 1070
73 [Mysql] 중복 데이터 값 찾기 졸리운_곰 2019.01.17 1500
72 [MySQL] 레코드 데이터 치환하기 (REPLACE) 졸리운_곰 2018.12.19 1343
71 [mysql] FOREIGN KEY 연관된 데이터 삭제 ON DELETE CASCADE file 졸리운_곰 2018.12.16 1379
70 [MYSQL]NULL 값을 다른 값으로 변경, [MySQL] IFNULL , select문에서 null값 치환 졸리운_곰 2018.11.23 1963
69 MySQL workbench safe mode 해제하기 file 졸리운_곰 2018.11.19 986
68 MySQL 중복 데이터 찾아서 삭제하기 졸리운_곰 2018.10.29 1287
67 MySQL에서 중복되는 행을 하나만 남기도 모두 삭제하는 방법 졸리운_곰 2018.10.29 1255
66 7 ways to convince MySQL to use the right index 졸리운_곰 2018.08.30 1766
65 MYSQL에서 원치않는 TABLE LOCK이 걸렸을 경우 해제 방법입니다. 졸리운_곰 2018.08.29 1316
64 제목 : [mysql] 현재날짜에서 이전달 구하기 file 졸리운_곰 2018.08.28 1741
63 [mysql-함수]날짜 관련 함수 모음 졸리운_곰 2018.08.24 1459
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED