author | ms.author | ms.date | ms.service | ms.subservice | ms.custom | |
---|---|---|---|---|---|---|
rwestMSFT |
randolphwest |
11/18/2024 |
sql |
linux |
|
The following steps use sqlcmd to locally connect to your new [!INCLUDE ssnoversion-md] instance.
Caution
[!INCLUDE password-complexity]
-
Run sqlcmd with parameters for your [!INCLUDE ssnoversion-md] name (
-S
), the user name (-U
), and the password (-P
). In this tutorial, you connect locally, so the server name islocalhost
. The user name issa
and the password is the one you provided for thesa
account during setup.sqlcmd -S localhost -U sa -P '<password>'
[!NOTE]
Newer versions of sqlcmd are secure by default. For more information about connection encryption, see sqlcmd utility for Windows, and Connecting with sqlcmd for Linux and macOS. If the connection doesn't succeed, you can add the-No
option to sqlcmd to specify that encryption is optional, not mandatory.You can omit the password on the command line to be prompted to enter it.
If you later decide to connect remotely, specify the machine name or IP address for the
-S
parameter, and make sure port 1433 is open on your firewall. -
If successful, you should get to a sqlcmd command prompt:
1>
. -
If you get a connection failure, first attempt to diagnose the problem from the error message. Then review the connection troubleshooting recommendations.
The following sections walk you through using sqlcmd to create a new database, add data, and run a simple query.
For more information about writing Transact-SQL statements and queries, see Tutorial: Write Transact-SQL statements.
The following steps create a new database named TestDB
.
-
From the sqlcmd command prompt, paste the following Transact-SQL command to create a test database:
CREATE DATABASE TestDB;
-
On the next line, write a query to return the name of all of the databases on your server:
SELECT Name FROM sys.databases;
-
The previous two commands aren't executed immediately. You must type
GO
on a new line to execute the previous commands:GO
Next create a new table, dbo.Inventory
, and insert two new rows.
-
From the sqlcmd command prompt, switch context to the new
TestDB
database:USE TestDB;
-
Create new table named
dbo.Inventory
:CREATE TABLE dbo.Inventory ( id INT, name NVARCHAR (50), quantity INT, PRIMARY KEY (id) );
-
Insert data into the new table:
INSERT INTO dbo.Inventory VALUES (1, 'banana', 150); INSERT INTO dbo.Inventory VALUES (2, 'orange', 154);
-
Type
GO
to execute the previous commands:GO
Now, run a query to return data from the dbo.Inventory
table.
-
From the sqlcmd command prompt, enter a query that returns rows from the
dbo.Inventory
table where the quantity is greater than 152:SELECT * FROM dbo.Inventory WHERE quantity > 152;
-
Execute the command:
GO
To end your sqlcmd session, type QUIT
:
QUIT
After installing [!INCLUDE ssnoversion-md] on Linux, review the best practices for configuring Linux and [!INCLUDE ssnoversion-md] to improve performance for production scenarios. For more information, see Performance best practices and configuration guidelines for SQL Server on Linux.
In addition to sqlcmd, you can use the following cross-platform tools to manage [!INCLUDE ssnoversion-md]:
Tool | Description |
---|---|
Azure Data Studio | A cross-platform GUI database management utility. |
Visual Studio Code | A cross-platform GUI code editor that run Transact-SQL statements with the mssql extension. |
PowerShell Core | A cross-platform automation and configuration tool based on cmdlets. |
mssql-cli | A cross-platform command-line interface for running Transact-SQL commands. |
[!INCLUDE ssnoversion-md] tools on Windows connect to [!INCLUDE ssnoversion-md] instances on Linux in the same way they would connect to any remote [!INCLUDE ssnoversion-md] instance.
If you have a Windows machine that can connect to your Linux machine, try the same steps in this topic from a Windows command-prompt running sqlcmd. You must use the target Linux machine name or IP address rather than localhost
, and make sure that TCP port 1433 is open on the [!INCLUDE ssnoversion-md] machine. If you have any problems connecting from Windows, see connection troubleshooting recommendations.
For other tools that run on Windows but connect to [!INCLUDE ssnoversion-md] on Linux, see:
For other installation scenarios, see the following resources:
- Upgrade: Learn how to upgrade an existing installation of [!INCLUDE ssnoversion-md] on Linux
- Uninstall: Uninstall [!INCLUDE ssnoversion-md] on Linux
- Unattended install: Learn how to script the installation without prompts
- Offline install: Learn how to manually download the packages for offline installation
For answers to frequently asked questions, see the SQL Server on Linux FAQ.
[!INCLUDE contribute-to-content]