This slightly tongue in cheek lightning talk shows how to get started with Microsoft SQL server on Linux in Docker.It explains installation, startup, data base creation on a local volume and how to connect using ODBC or JDBC.
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Microsoft SQL Server with Linux and Docker
1.
2. Why would I want to use
Microsoft SQL Server on Linux?
●
You don’t. Instead, use PostgreSQL (for larger
projects) or sqllite (for trivial things).
●
When does it still make sense?
– When you locally develop on Linux for customers using
Windows and MS SQL.
– When you want to educate yourself.
– When you want to irritate people on Linuxtage!
3. Why would I want to use Docker for
Microsoft SQL Server?
●
Less clutter after installation.
●
Simple updates of SQL server
→ Container images are maintained by Microsoft.
●
Because Docker is hip!
4. Install docker
●
Depends on several things:
https://docs.docker.com/install/
●
Typically community edition (CE) sufces.
●
With Ubuntu: use Ubuntu Software shop
5. Install MS SQL Server
●
From docker store (requires login):
https://store.docker.com/images/mssql-server-linu
x
●
From command line:
https://docs.microsoft.com/en-us/sql/linux/quickst
art-install-connect-docker?view=sql-server-linux-2
017
$ sudo docker pull microsoft/mssql-server-
linux:2017-latest
6. Run the server
●
sudo docker run
-e 'ACCEPT_EULA=Y'
-e 'MSSQL_SA_PASSWORD=x2-B3.k4'
-p 1401:1433
--name mssql
-d microsoft/mssql-server-linux:2017-latest
●
See also: quick installation guide.
●
Consider changing SA password stored in
environment variable as described there.
7. Running commands in the container
●
Open a shell in the container:
sudo docker exec -it mssql "bash"
●
Send a single shell command to the container:
sudo docker exec -it mssql echo hello world
8. Execute SQL commands
●
Use command line tool sqlcmd
●
Full path: /opt/mssql-tools/bin/sqlcmd
●
Has various options
9. Sqlcmd online help
●
$ sudo docker exec -it mssql
/opt/mssql-tools/bin/sqlcmd -?
●
Useful options:
-b if SQL command fails, abort and set exit code
-S host[,port]
-U username
-P password
-Q run SQL command and exit
-q run SQL command and remain in sqlcmd console
-i some.sql to run SQL commands stored in fle
10. Create a new database
●
sudo docker exec -it mssql
/opt/mssql-tools/bin/sqlcmd
-S localhost
-U SA -P x2-B3.k4
-b -Q "create database linuxtage"
●
Creates data base on container volume
→ will be deleted upon docker rm <image>
●
Use more command line options to create
database on local disk
11. Run with mounted local volume
●
sudo mkdir /home/roskakori/mssql
●
sudo docker run
-e 'ACCEPT_EULA=Y'
-e 'MSSQL_SA_PASSWORD=x2-B3.k4'
-p 1401:1433
-v /home/roskakori/mssql:/var/opt/mssql
--name mssql
-d microsoft/mssql-server-linux:2017-latest
12. Create database on local volume
●
sudo docker exec -it mssql
/opt/mssql-tools/bin/sqlcmd
-S localhost
-U SA -P x2-B3.k4
-b -Q "create database linuxtage on
(name=’ltg_data’,
filename=’/var/opt/mssql/ltg.dat’) log on
(name=’ltg_log’,
filename=’/var/opt/mssql/ltg.log’) "
13. How to access from software
●
On docker, MS SQL runs on port 1433
●
Locally mapped to port 1401
●
To access from local you need drivers
– JDBC driver:
https://docs.microsoft.com/en-us/sql/connect/jdbc/microsoft-jdbc-driver-for-sq
l-server?view=sql-server-2017
– ODBC driver for Linux
https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-mic
rosoft-odbc-driver-for-sql-server?view=sql-server-2017
– Avoid jTDS, has issues with modern servers and is not really maintained any
more
●
Supported by various programming languages
– Java: JDBC driver
– Python: pyodbc
14. Install local ODBC for MS SQL
●
See Installing the Microsoft ODBC Driver for SQL Server on Linux and macOS
●
Example for Ubuntu 17.10:
– curl >/tmp/microsoft.asc https://packages.microsoft.com/keys/microsoft.asc
– sudo apt-key add </tmp/microsoft.asc
– curl >/tmp/prod.list https://packages.microsoft.com/confg/ubuntu/17.10/prod.list
– sudo cp /tmp/prod.list /etc/apt/sources.list.d/mssql-release.list
– sudo apt-get update
– sudo ACCEPT_EULA=Y apt-get install msodbcsql17
– sudo ACCEPT_EULA=Y apt-get install mssql-tools
– echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profle
– echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
– source ~/.bashrc
15. Local sqlcmd to docker
●
Port 1401 must be specifed as part of the
hostname
●
Unintuitive syntax using comma
●
sqlcmd -S localhost,1401,1401 -U SA -P x2-B3.k4
-b -Q "select 'hello world'"
16. Summary
●
MS SQL on Linux
– It can be done.
– It does work.
●
Use postgreSQL if possible.
●
Use MS SQL if necessary.
●
Use sqlite if appropriate.
●
Don’t use MySQL / MariaDB.