How to run MSSQL in Linux

If you are a Microsoft fan or your company is relying on Microsoft technologies, in most of the cases, the database in use will be MSSQL. But if you want try out the things in a Linux based system like Redhat or Ubuntu it is convenient to run an MSSQL instance in the local machine itself. With docker we have a faster and feasible solution. Here I will share the relevant commands and instructions to get started right away.

1. Pull the docker image

docker pull mcr.microsoft.com/mssql/server
 
This will download the latest mssql server tag.

2. To start the container

docker run --name MSSQLLatest -d -p 1433:1433 -e "SA_PASSWORD=
<StrongPassword>" -e "ACCEPT_EULA=Y" mcr.microsoft.com/mssql/server
 
These command options are case sensitive. We have mapped default port 1433 to itself of the host machine. You can choose another if that port is occupied. Choose a password of your own in place of '<StrongPassword>'.

3. To start and stop the MSSQL server you can use below commands.

docker stop MSSQLLatest
docker start MSSQLLatest
Now we have a MSSQL server running locally and can use it similar to any other installation. Just for the convenient I will submit a few here. 
 
If you do not have a MSSQL client installed in Linux, let's install one. I selected mssql-cli. Complete instructions can be found at https://docs.microsoft.com/en-us/sql/tools/mssql-cli?view=sql-server-ver15.
 
If the installation is successful, you can use below command to connect to the MSSQL server via the CLI.
At the first time use below.
mssql-cli -S localhost -U sa 
If you have already created a database somehow, use below to directly connect to the schema.
mssql-cli -S localhost -d <database_name> -U sa
Both above will prompt for the password. Upon successful submission you will be inside. There you can use below basic commands.
\ld   --> list databases
\lt    --> list tables
create database <database_name> ;
use <database_name>;

Then the common requirement is how to run a script. Similar to MySQL you might try 'source' command, but it does not work in MSSQL. Below command can be used there, pointing to a local file that has the database script. (Executed outside from mssql-cli.)
mssql-cli -S localhost -U sa -P <password> -d <database_name> 
-i <absolute_path_to_db_script>;
Cheers!

 


Comments

Popular posts from this blog

Sign into Dokuwiki with Google

Single Sign On Integrations - Intro

Shibboleth based SSO for SAP