How to Get Root and User SSH Login Email Alerts

Login to Linux server

Linux / June 6, 2019

Sqlcmd is part of the SQL Server command-line tools, which are not installed automatically with SQL Server on Linux. If you have not already installed the SQL Server command-line tools on your Linux machine, you must install them. For more information on how to install the tools, follow the instructions for your Linux distribution:

Connection requirements

To connect to SQL Server on Linux, you must use SQL Authentication (username and password). To connect remotely, you must ensure that the port SQL Server listens on is open. By default, SQL Server listens on TCP port 1433. Depending on your Linux distribution and configuration, you might have to open this port in the firewall.

Connect to SQL Server on Linux

In the following steps, connect to SQL Server 2017 on Linux with sqlcmd.

Tip
  1. On your Linux box, open a command terminal.
  2. Run sqlcmd with parameters for your SQL Server name (-S), the user name (-U), and the password (-P).

    The following command connects to the local SQL Server instance (localhost) on Linux.

    sqlcmd -S localhost -U SA -P ''

    You can omit the password on the command-line to be prompted to enter it.

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

    sqlcmd -S 192.555.5.555 -U SA -P ''

Query SQL Server

After you connect to SQL Server you can run queries to return information or create database objects. If you are new to writing queries, see . In the following steps, you will use sqlcmd to:

  1. Query SQL Server for a list of the databases.
  2. Use Transact SQL to create a database.
  3. Create and populate a table in the new database.
  4. Query the table.

To to complete each of these tasks, copy the Transact-SQL from the examples below into the sqlcmd session that you created in the previous step.

For example, this query returns the name of all of the databases.

SELECT Name from sys.Databases; GO

Create a database using the SQL Server default settings.

CREATE DATABASE testdb; GO

Use the database:

USE testdb; GO

Create a table in the current database:

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

Insert data into the new table:

INSERT INTO inventory VALUES (1, 'banana', 150); INSERT INTO inventory VALUES (2, 'orange', 154); GO

Select from the table:

SELECT * FROM inventory WHERE quantity > 152; GO

To end your sqlcmd session, type QUIT.

QUIT

In this walk-through you connected to SQL Server with sqlcmd, and created and populated a database. For more information on how to use sqlcmd.exe, see sqlcmd Utility.

Connect and query from Windows

It is important to note that SQL Server tools on Windows connect to SQL Server instances on Linux in the same way they would connect to any remote SQL Server instance. So, you can follow the same steps in this topic running sqlcmd.exe from a remote Windows machine. Just verify that you use the target Linux machine name or IP address rather than localhost. For other connection requirements, see connection troubleshooting recommendations.

Source: docs.microsoft.com