Aller au contenu

MS SQL Server#

Connect from Ubuntu#

To connect to an MS SQL Server from the command line on a Linux Ubuntu system, you can use the sqlcmd command-line tool provided by Microsoft. Here's how you can do it

1. Install the Required Tools#

Bash
# Import the Microsoft GPG key
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

# Add the Microsoft repository
sudo add-apt-repository "$(curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list)"

# Update package lists
sudo apt-get update

# Install the ODBC driver and sqlcmd
sudo apt-get install -y mssql-tools unixodbc-dev

2. Add sqlcmd to PATH#

Bash
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

3. Connect to MS SQL Server#

Bash
sqlcmd -S <server_name> -U <username> -P <password>

Replace:

  • with the hostname or IP address of the SQL Server (e.g., localhost, 192.168.1.100, or myserver.database.windows.net).
  • with your SQL Server username.
  • with your SQL Server password.

Example:

Bash
1
2
3
sqlcmd -S 10.0.1.100 -U admin -P MyPassword123

sqlcmd -S cf-cpat-dev-db.c7wqg8meub2t.ca-central-1.rds.amazonaws.com -U admin

4. Run SQL Commands#

Once connected, you can run SQL queries. For example:

SQL
SELECT name FROM sys.databases;
GO

Use GO to execute the command. Type QUIT to exit the session.

Troubleshooting

  • Ensure the SQL Server is reachable from your Ubuntu machine (e.g., check firewall rules or network connectivity).

  • If using Azure SQL, ensure your IP is allowed in the server's firewall settings.