Sascha Vogt

maybebuggy.de

Software developer who cares for clean code

2-Minute Read

This is just a quick post (merely to have something for myself to remember) on how to get MS SQL running on Linux.
As you might have noticed, there is a developer version of the next MS SQL server available (for now called “vNext Community Preview”) which is also available on Linux and even as a Docker container.

Microsoft provides convenient to use package repositories to be able to use the SQL server on various Linux distributions. Currently RedHat, SuSe and Ubuntu are officially support. As I usually use Debian based Linux distributions, I’ll focus on Ubuntu here.

Installing on Ubuntu (16.04)

With the following snippets you will add Microsoft’s GPG key to verify package signatures, add the repository and update the package cache. The package name is called mssql-server and to be able to silently install it, you need to accept the EULA by setting the environment variable ACCEPT_EULA to Y.

The package contains a convenient helper script to set the database administrator’s password and enable the service.

curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl -fsSL https://packages.microsoft.com/config/ubuntu/16.04/mssql-server.list | sudo tee /etc/apt/sources.list.d/mssql-server.list
sudo apt-get update
sudo ACCEPT_EULA=y apt-get install mssql-server -y
sudo SA_PASSWORD='<insert-secure-password>' /opt/mssql/bin/sqlservr-setup --accept-eula --set-sa-password --start-service --enable-service

In additon there is another repository containing the sqlcmd command, which you can use in scripts to execute SQL statements on the server. Use the following snippet to add the repository and install the commandline tools.

curl -fsSL https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/mssql-prod.list
sudo ACCEPT_EULA=y apt-get install mssql-tools -y 

Running the Docker container

There is also a ready to use Docker container. It accepts the DB administrator’s password as an environment variable. Make sure to expose the SQL servers network port, if you want to use the database from another machine.

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<insert-secure-password>' -p 1433:1433 -d mcr.microsoft.com/mssql/server

You can optionally put the database files on the Docker host or in a data container to have them persistent. That way you should be able to update the container and keep your database.

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<insert-secure-password>' -p 1433:1433 -v /path/to/local-db-files:/var/opt/mssql -d mcr.microsoft.com/mssql/server

For more information check the Github repository for the Docker container at https://github.com/Microsoft/mssql-docker

Greetings
.sascha

Recent Posts

Categories