How to Install SQL Server Command Line Tools SQLCMD and BCP on RHEL 8.1


By:   |   Updated: 2021-11-23   |   Comments   |   Related: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | More > Virtualization


Problem

We installed SQL Server 2019 on RHEL 8.1 in our last article. Now we are going to install SQL Server command line tools like SQLCMD and BCP to access SQL Server databases locally on the system.

Solution

You need a utility, tool or program to access, manage or configure databases hosted on Linux systems. You can use SQL Server Management Studio or Azure Data Studio to establish database connections to the SQL Server on Linux instance, but in this article we will look at how to install some command line tools locally.

Note, this is 8th article in this series, read the previous articles to understand how to install and configure SQL Server 2019 on RHEL 8.1. See the next steps at the end of this article for links to the other articles.

Install SQL Server Command Line Tools SQLCMD and BCP

Let’s first check whether SQLCMD is installed on the target machine. Simply type SQLCMD on your terminal and press enter. If this utility is installed, most operating systems will recognize it and show details about this utility or a connect error.

--Check whether SQLCMD is installed or not
sqlcmd

I ran this command and found this tool is not installed on this machine as shown in the below image. We can run the below command to check if the repository exists.

--Verify SQL Server Repository
sudo ls /etc/yum.repos.d

We can see only two repositories in the below image. One is for SQL Server 2019 and the other for RHEL, so we will need to download and install.

check respository

We can run the below command to download the repository. Change the path in the below url from /rhel/8 to /rhel/7/ if you are using RHEL 7.x.

--Download Microsoft Red Hat Repository Configuration file 
sudo curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/8/prod.repo

The command executed successfully and now we can see the additional repository configuration file msprod.repo in the below image.

download msprod.repo

The below step is optional if you are installing SQL Server 2019 and the command line tools SQLCMD on a new machine. But if you are installing these products on an older machine and already had a previous version of mssql-tools installed then you need to remove the older unixODBC packages installed on your machine. You can run the below command to remove this component.

--Remove unixODBC packages
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel

As I am installing this on a newly system the output shows no match for the above software, but if you have an older machine then you should run this to remove this component.

remove old respository

Now we will install SQL Server command line tool mssql-tools along with unixODBC-devel package by executing the below commands.

--Install mssql-tool along with unixODBC package
sudo yum install -y mssql-tools unixODBC-devel

We can see all packages that will be installed with the mssql-tools component. We can see the unixODBC package as well which is being install. All these packages will be downloaded and installed as shown in the below image. The installer will ask you to enter YES to accept the license terms of this tool.

install mssql-tools on linux

You can see I have entered YES twice to accept the license terms for both packages. Both packages have now been installed successfully after accepting the license terms as shown in the below screenshot.

install mssql-tools on linux

The SQL Server command line tools have now been installed. You can go ahead and use these tools to create and manage databases as per your requirements.

If you are still facing issues accessing or launching the SQLCMD tool, then you need to add path environment variables to launch them easily. Run the below commands to modify the PATH for both login sessions and interactive or non-login sessions.

--Add /opt/mssql-tools/bin/ to our path environment variable
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

At first, sqlcmd is not recognized as shown below.

I will add /opt/mssql-tools/bin/ to our path environment variable and run again.  This time it works, although we have some other errors, so we know that the tool can be found.

sqlcmd check

We can validate installation by connecting to the SQL Server 2019 instance using the SQLCMD tool. Run the below command to establish a database connection.

--Connect to SQL Server 2019 instance hosted on RHEL8.1 using SQLCMD tool
--Replace 192.168.6.128 with your IP or server name
--I used sa account with -U parameter. Press Enter to write password
Sqlcmd -S192.168.6.128 -Usa
 
--Connect locally on the server
sqlcmd -S localhost -U SA -P '<YourPassword>'

You can see I have established a database connection using sqlcmd tool and then I executed a simple T-SQL statement to fetch the installed version to SQL Server.

sample query using sqlcmd

I used the PuTTY utility to do all installations and connection validation in the above steps. When I checked on my local server there was same issue. SQLCMD was not recognized by RHEL server terminal. When I added the path environment variable it was successful. If you are facing a similar situation where you are not able to access SQL Server command line tool locally or remotely then try adding the path environment variable.

access via sqlcmd

I have also established a database connection locally on the server and fetched the same information that I did using PuTTY. You can see the successful execution and validation for this tool below.

sample query using sqlcmd

Similarly, I checked the BCP command line utility and you can see it is successfully recognized by both terminals locally on the server as well as remotely using the PuTTY utility.

bcp utility check

Here is the screenshot for the BCP utility using the PuTTY utility.

bcp utility check
Next Steps

SQL Server command line tools have been installed which is very useful to interact with the SQL Server instance. Go ahead and use it as per your needs to manage and configure databases on this server.






get scripts

next tip button



About the author
MSSQLTips author Manvendra Singh Manvendra Singh has over 5 years of experience with SQL Server and has focused on Database Mirroring, Replication, Log Shipping, etc.

View all my tips


Article Last Updated: 2021-11-23

Comments For This Article





download














vote


get free sql tips
agree to terms