How to Install sqlcmd & bcp on Red Hat

If you’ve installed SQL Server on your Red Hat machine, you’ll need some tools to connect to it and run queries, etc.

The sqlcmd and bcp utilities are a couple of good options for helping you perform the most common tasks.

What are sqlcmd and bcp?

  • sqlcmd is is a command-line utility for ad hoc, interactive execution of T-SQL statements and scripts and for automating T-SQL scripting tasks.
  • bcp (bulk copy program) is a utility that can be used to import large numbers of new rows into SQL Server tables or to export data out of tables into data files.

Installation

Below are the steps I took when installing sqlcmd and bcp on to Red Hat Enterprise Linux (RHEL) 8.3.

Download the Microsoft Red Hat repository configuration file:

sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo

If you have a different version of RHEL, you may need to change /rhel/8/ to reflect the applicable version number.

If you have a previous version of mssql-tools installed, the following code will remove any older unixODBC packages:

sudo yum remove unixODBC-utf16 unixODBC-utf16-devel

If you’re not sure, you can go ahead and run it anyway. If it’s not already installed, it will simply report that there’s nothing to do.

Now you can install mssql-tools with the unixODBC developer package:

sudo yum install -y mssql-tools unixODBC-devel

You’ll be prompted to accept the licence terms twice. If you agree with the terms, enter YES on both occassions.

Add to PATH

You can optionally add the  /opt/mssql-tools/bin/ to your PATH environment variable, so that you can run the tools without having to provide the full path.

To make sqlcmd/bcp accessible from the bash shell for login sessions:

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

To make sqlcmd/bcp accessible from the bash shell for interactive/non-login sessions:

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

Connect to SQL Server

Now that the command-line tools are installed, you can use the command-line tools to connect to SQL Server.

Enter the following, but replace <YourPassword> with your own password:

sqlcmd -S localhost -U SA -P '<YourPassword>'

Alternatively, you can omit -P '<YourPassword>' and you will be prompted for it instead.

This example assumes that you have SQL Server installed on the local machine.

To connect to a remote instance, change localhost to the machine name or IP address, and be sure to open the applicable port on the firewall (port 1433 is the default for SQL Server).

Once you’ve connected successfully, you should see a prompt that looks like this:

1>

From here, you can create databases, run queries, and more.

For now, enter the following code to check your version of SQL Server:

SELECT @@version
GO

You should see something like this:

Microsoft SQL Server 2019 (RTM-CU8) (KB4577194) - 15.0.4073.23 (X64)
Sep 23 2020 16:03:08
Copyright (C) 2019 Microsoft Corporation
Developer Edition (64-bit) on Linux (Red Hat Enterprise Linux 8.3 (Ootpa))