How to connect to SQL Server using sqlcmd on Linux?












5















I have set up the Microsoft ODBC Driver 13 for SQL Server on an Ubuntu 16.04 machine. I am now trying to test a database connection, that includes both the server name and an instance name.



The following both work fine from a Windows machine:



sqlcmd -S "SERVERNAMEINSTANCENAME" -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAMEINSTANCENAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


However none of them work on Linux - they all return the error message below. The error implies the server cannot be found:



Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : MAX_PROVS: Error Locating Server/Instance Specified [xFFFFFFFF]. .
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..


I have also tried escaping the backslash with the same error (and with single and double quotes).



sqlcmd -S "SERVERNAME\INSTANCENAME" -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAME\INSTANCENAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


The driver is working however as I can connect to a different server with no instance name:



sqlcmd -S SERVERNAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


I am fairly certain it is due to the backslash, but cannot find a way to get around this.










share|improve this question


















  • 2





    Have you tried connecting using a port specification rather than instance name (e.g. -S "SERVERNAME,port")?

    – Dan Guzman
    Aug 18 '16 at 11:26











  • @DanGuzman - thanks! That approach works. Will I add details as an answer?

    – geographika
    Aug 18 '16 at 11:46
















5















I have set up the Microsoft ODBC Driver 13 for SQL Server on an Ubuntu 16.04 machine. I am now trying to test a database connection, that includes both the server name and an instance name.



The following both work fine from a Windows machine:



sqlcmd -S "SERVERNAMEINSTANCENAME" -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAMEINSTANCENAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


However none of them work on Linux - they all return the error message below. The error implies the server cannot be found:



Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : MAX_PROVS: Error Locating Server/Instance Specified [xFFFFFFFF]. .
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..


I have also tried escaping the backslash with the same error (and with single and double quotes).



sqlcmd -S "SERVERNAME\INSTANCENAME" -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAME\INSTANCENAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


The driver is working however as I can connect to a different server with no instance name:



sqlcmd -S SERVERNAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


I am fairly certain it is due to the backslash, but cannot find a way to get around this.










share|improve this question


















  • 2





    Have you tried connecting using a port specification rather than instance name (e.g. -S "SERVERNAME,port")?

    – Dan Guzman
    Aug 18 '16 at 11:26











  • @DanGuzman - thanks! That approach works. Will I add details as an answer?

    – geographika
    Aug 18 '16 at 11:46














5












5








5








I have set up the Microsoft ODBC Driver 13 for SQL Server on an Ubuntu 16.04 machine. I am now trying to test a database connection, that includes both the server name and an instance name.



The following both work fine from a Windows machine:



sqlcmd -S "SERVERNAMEINSTANCENAME" -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAMEINSTANCENAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


However none of them work on Linux - they all return the error message below. The error implies the server cannot be found:



Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : MAX_PROVS: Error Locating Server/Instance Specified [xFFFFFFFF]. .
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..


I have also tried escaping the backslash with the same error (and with single and double quotes).



sqlcmd -S "SERVERNAME\INSTANCENAME" -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAME\INSTANCENAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


The driver is working however as I can connect to a different server with no instance name:



sqlcmd -S SERVERNAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


I am fairly certain it is due to the backslash, but cannot find a way to get around this.










share|improve this question














I have set up the Microsoft ODBC Driver 13 for SQL Server on an Ubuntu 16.04 machine. I am now trying to test a database connection, that includes both the server name and an instance name.



The following both work fine from a Windows machine:



sqlcmd -S "SERVERNAMEINSTANCENAME" -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAMEINSTANCENAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


However none of them work on Linux - they all return the error message below. The error implies the server cannot be found:



Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : MAX_PROVS: Error Locating Server/Instance Specified [xFFFFFFFF]. .
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..


I have also tried escaping the backslash with the same error (and with single and double quotes).



sqlcmd -S "SERVERNAME\INSTANCENAME" -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAME\INSTANCENAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


The driver is working however as I can connect to a different server with no instance name:



sqlcmd -S SERVERNAME -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"


I am fairly certain it is due to the backslash, but cannot find a way to get around this.







sql-server linux ubuntu sqlcmd






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 18 '16 at 10:34









geographikageographika

1582212




1582212








  • 2





    Have you tried connecting using a port specification rather than instance name (e.g. -S "SERVERNAME,port")?

    – Dan Guzman
    Aug 18 '16 at 11:26











  • @DanGuzman - thanks! That approach works. Will I add details as an answer?

    – geographika
    Aug 18 '16 at 11:46














  • 2





    Have you tried connecting using a port specification rather than instance name (e.g. -S "SERVERNAME,port")?

    – Dan Guzman
    Aug 18 '16 at 11:26











  • @DanGuzman - thanks! That approach works. Will I add details as an answer?

    – geographika
    Aug 18 '16 at 11:46








2




2





Have you tried connecting using a port specification rather than instance name (e.g. -S "SERVERNAME,port")?

– Dan Guzman
Aug 18 '16 at 11:26





Have you tried connecting using a port specification rather than instance name (e.g. -S "SERVERNAME,port")?

– Dan Guzman
Aug 18 '16 at 11:26













@DanGuzman - thanks! That approach works. Will I add details as an answer?

– geographika
Aug 18 '16 at 11:46





@DanGuzman - thanks! That approach works. Will I add details as an answer?

– geographika
Aug 18 '16 at 11:46










2 Answers
2






active

oldest

votes


















5














With the help of Dan Guzman's comment I got the connection working using a port.



I had a Windows machine with SQL Management Studio connected to the database in question, and used:



netstat -abn


Then I searched for ssms.exe for the connection details:



TCP    192.168.0.31:50777     192.168.0.78:49399     ESTABLISHED [Ssms.exe]


Alternatively log into the database server and look at the ports used by the instance in SQL Server Configuration Manager (SQL Server Network Configuration > Protocols for INSTANCENAME > TCP/IP > Properties > IPAll TCP Dynamic Ports).



The following then both worked:



sqlcmd -S 192.168.0.78,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
sqlcmd -S SERVERNAME,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"





share|improve this answer





















  • 1





    In your second example, is INSTANCENAME a typo? I think it should be SERVERNAME. Although the port number specification may be an acceptable work-around for you, I think the instance name should work from Ubuntu as it does from Windows. Make sure UDP 1434 (for the SQL Server Browser service), can be reached using a utility like netcat.

    – Dan Guzman
    Aug 18 '16 at 12:23





















0














It worked like a charm. Thanks for sharing!!






share|improve this answer








New contributor




truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f147185%2fhow-to-connect-to-sql-server-using-sqlcmd-on-linux%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    With the help of Dan Guzman's comment I got the connection working using a port.



    I had a Windows machine with SQL Management Studio connected to the database in question, and used:



    netstat -abn


    Then I searched for ssms.exe for the connection details:



    TCP    192.168.0.31:50777     192.168.0.78:49399     ESTABLISHED [Ssms.exe]


    Alternatively log into the database server and look at the ports used by the instance in SQL Server Configuration Manager (SQL Server Network Configuration > Protocols for INSTANCENAME > TCP/IP > Properties > IPAll TCP Dynamic Ports).



    The following then both worked:



    sqlcmd -S 192.168.0.78,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
    sqlcmd -S SERVERNAME,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"





    share|improve this answer





















    • 1





      In your second example, is INSTANCENAME a typo? I think it should be SERVERNAME. Although the port number specification may be an acceptable work-around for you, I think the instance name should work from Ubuntu as it does from Windows. Make sure UDP 1434 (for the SQL Server Browser service), can be reached using a utility like netcat.

      – Dan Guzman
      Aug 18 '16 at 12:23


















    5














    With the help of Dan Guzman's comment I got the connection working using a port.



    I had a Windows machine with SQL Management Studio connected to the database in question, and used:



    netstat -abn


    Then I searched for ssms.exe for the connection details:



    TCP    192.168.0.31:50777     192.168.0.78:49399     ESTABLISHED [Ssms.exe]


    Alternatively log into the database server and look at the ports used by the instance in SQL Server Configuration Manager (SQL Server Network Configuration > Protocols for INSTANCENAME > TCP/IP > Properties > IPAll TCP Dynamic Ports).



    The following then both worked:



    sqlcmd -S 192.168.0.78,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
    sqlcmd -S SERVERNAME,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"





    share|improve this answer





















    • 1





      In your second example, is INSTANCENAME a typo? I think it should be SERVERNAME. Although the port number specification may be an acceptable work-around for you, I think the instance name should work from Ubuntu as it does from Windows. Make sure UDP 1434 (for the SQL Server Browser service), can be reached using a utility like netcat.

      – Dan Guzman
      Aug 18 '16 at 12:23
















    5












    5








    5







    With the help of Dan Guzman's comment I got the connection working using a port.



    I had a Windows machine with SQL Management Studio connected to the database in question, and used:



    netstat -abn


    Then I searched for ssms.exe for the connection details:



    TCP    192.168.0.31:50777     192.168.0.78:49399     ESTABLISHED [Ssms.exe]


    Alternatively log into the database server and look at the ports used by the instance in SQL Server Configuration Manager (SQL Server Network Configuration > Protocols for INSTANCENAME > TCP/IP > Properties > IPAll TCP Dynamic Ports).



    The following then both worked:



    sqlcmd -S 192.168.0.78,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
    sqlcmd -S SERVERNAME,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"





    share|improve this answer















    With the help of Dan Guzman's comment I got the connection working using a port.



    I had a Windows machine with SQL Management Studio connected to the database in question, and used:



    netstat -abn


    Then I searched for ssms.exe for the connection details:



    TCP    192.168.0.31:50777     192.168.0.78:49399     ESTABLISHED [Ssms.exe]


    Alternatively log into the database server and look at the ports used by the instance in SQL Server Configuration Manager (SQL Server Network Configuration > Protocols for INSTANCENAME > TCP/IP > Properties > IPAll TCP Dynamic Ports).



    The following then both worked:



    sqlcmd -S 192.168.0.78,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"
    sqlcmd -S SERVERNAME,49399 -U User -P pwd -d DatabaseName -Q "SELECT TOP 5 Id FROM dbo.MyTable;"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 18 '16 at 12:33

























    answered Aug 18 '16 at 11:53









    geographikageographika

    1582212




    1582212








    • 1





      In your second example, is INSTANCENAME a typo? I think it should be SERVERNAME. Although the port number specification may be an acceptable work-around for you, I think the instance name should work from Ubuntu as it does from Windows. Make sure UDP 1434 (for the SQL Server Browser service), can be reached using a utility like netcat.

      – Dan Guzman
      Aug 18 '16 at 12:23
















    • 1





      In your second example, is INSTANCENAME a typo? I think it should be SERVERNAME. Although the port number specification may be an acceptable work-around for you, I think the instance name should work from Ubuntu as it does from Windows. Make sure UDP 1434 (for the SQL Server Browser service), can be reached using a utility like netcat.

      – Dan Guzman
      Aug 18 '16 at 12:23










    1




    1





    In your second example, is INSTANCENAME a typo? I think it should be SERVERNAME. Although the port number specification may be an acceptable work-around for you, I think the instance name should work from Ubuntu as it does from Windows. Make sure UDP 1434 (for the SQL Server Browser service), can be reached using a utility like netcat.

    – Dan Guzman
    Aug 18 '16 at 12:23







    In your second example, is INSTANCENAME a typo? I think it should be SERVERNAME. Although the port number specification may be an acceptable work-around for you, I think the instance name should work from Ubuntu as it does from Windows. Make sure UDP 1434 (for the SQL Server Browser service), can be reached using a utility like netcat.

    – Dan Guzman
    Aug 18 '16 at 12:23















    0














    It worked like a charm. Thanks for sharing!!






    share|improve this answer








    New contributor




    truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      0














      It worked like a charm. Thanks for sharing!!






      share|improve this answer








      New contributor




      truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        0







        It worked like a charm. Thanks for sharing!!






        share|improve this answer








        New contributor




        truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        It worked like a charm. Thanks for sharing!!







        share|improve this answer








        New contributor




        truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 14 mins ago









        truthtriumphstruthtriumphs

        1




        1




        New contributor




        truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        truthtriumphs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f147185%2fhow-to-connect-to-sql-server-using-sqlcmd-on-linux%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liste der Baudenkmale in Friedland (Mecklenburg)

            Single-Malt-Whisky

            Czorneboh