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 86287
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 78744
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95486
72 [R library] library(XML) # install.packages("XML") 인스톨 에러 졸리운_곰 2023.05.06 1178
71 [R 데이터 분석] Titanic: Machine Learning from Disaster (타이타닉 생존 예측) file 졸리운_곰 2023.04.29 1179
70 [R 데이터 분석] R 유명한 패키지 정리 졸리운_곰 2023.04.24 1906
69 [R 데이터 분석] Shiny : 대시보드 배포하기 file 졸리운_곰 2023.03.19 1415
68 [stat(통계) R 언어] 유명하고 많이 사용하는 R 패키지 정리 졸리운_곰 2022.04.19 1299
67 [R 데이터 분석] anaconda에서 R 사용하기 file 졸리운_곰 2022.01.16 1307
66 [R 데이터 분석] Using C/C++ in R , R언어에서 C/C++ 사용하기 졸리운_곰 2021.11.21 1648
65 [데이터 통계분석 R언어] Shiny 웹앱 개발 졸리운_곰 2021.02.14 1389
64 How to Deploy Interactive R Apps with Shiny Server file 졸리운_곰 2021.02.13 1216
63 [통계, 데이터분석 언어 R] R Shiny Server를 사용해 보자. 졸리운_곰 2021.02.13 1415
62 통계분석툴 R & MongoDB 연동 방법 file 졸리운_곰 2020.12.12 1823
61 Reading OECD.Stat into R file 졸리운_곰 2019.04.04 1871
60 Scrape OECD Data by R lang : R언어로 OECD.net 데이터 가져오기 졸리운_곰 2019.04.04 1485
59 윈도우에서 통계 분석 프로그램 R Project 활용 방안 file 졸리운_곰 2018.12.24 1699
58 C#과 R을 연동하기 file 졸리운_곰 2018.12.24 1555
57 Use R in C# : C#에서 R 사용 졸리운_곰 2018.12.24 1549
56 R 언어_ 범주형 자료(factor) 함수 사용 file 졸리운_곰 2018.12.22 1665
55 R - 도수분포표/교차표, table( )함수 file 졸리운_곰 2018.12.22 1429
54 주피터 노트북에 R 커널 설치하기 + 아나콘다 설치 file 졸리운_곰 2018.12.21 2315
53 R기초입문_r_book_mac_v3.pdf file 졸리운_곰 2018.12.20 1593
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED