How to Install and Use Microsoft SQL Server (MS SQL) on RHEL 7, CentOS 7 and Ubuntu 16.04 LTS

Install SQL Server on Ubuntu 16.04

First import the public repository GPG keys, then add Microsoft SQL Server Ubuntu repository and use the apt-get command to perform the installation.

Install SQL Server

$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
$ curl 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 apt-get install -y mssql-server

After the package installation finishes, run the configuration script and follow the prompts to accept the license terms and setup password for SA account.

$ sudo /opt/mssql/bin/sqlservr-setup

Once the configuration is done, verify that the service is running.

$ systemctl status mssql-server

Install SQL Server tools Add the below repo to install SQL Server Command-line tools, which contains Microsoft ODBC drivers, and their dependencies.

  • sqlcmd: Command-line query utility.
  • bcp: Bulk import-export utility.
$ curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
$ curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
$ sudo apt-get update 
$ sudo apt-get install mssql-tools

Connect to SQL Server on Linux

Follow the below steps to connect SQL Server on Linux with help of sqlcmd tool.

$ sqlcmd -S localhost -U SA -P 'YourPassword'

Details :

  • -S : SQL Server name
  • -U : User name
  • -P : Password

To connect remote instance, specify the machine name or IP address for the -S parameter.

$ sqlcmd -S Server IP -U SA -P 'YourPassword'

To display database lists (List of available/created databases).

SELECT Name from sys.Databases;
GO

Create a database and use the database.

CREATE DATABASE magi;
GO
USE magi;
GO

Create a table in the current database.

CREATE TABLE articles (id INT, name NVARCHAR(50), quantity INT);
GO

Insert data into the new table.

INSERT INTO articles VALUES (100, 'prakash', 53);
INSERT INTO articles VALUES (101, 'ramya', 21);
GO

Print the values from table.

SELECT * FROM articles WHERE name ramya;
GO

To end your sqlcmd session, type

QUIT

Enjoy…)

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “How to Install and Use Microsoft SQL Server (MS SQL) on RHEL 7, CentOS 7 and Ubuntu 16.04 LTS”

Leave a Reply

Your email address will not be published. Required fields are marked *