1

I am using the statement below to try and open a connection to the latest download of SQLServer Express.

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mssql"
    "log"
)

        db, err := gorm.Open("mssql", "sqlserver://tony:Password6!@localhost:1433?database=go_user")

The statement results in an error:

2019/09/30 10:29:16 Unable to open tcp connection with host 'localhost:1433': dial tcp [::1]:1433: connectex: No connection could be made because the target machine actively refused it. panic: Unable to open tcp connection with host 'localhost:1433': dial tcp [::1]:1433: connectex: No connection could be made because the target machine actively refused it.

Is anyone successfully connecting to SQLServer Express with GORM on Golang?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Arlo Guthrie
  • 1,152
  • 3
  • 12
  • 28
  • That's not a valid SQL Server Express string. SQL Server Express installs only named instances. Use `localhost\\SQLEXPRESS` instead of `localhost` – Panagiotis Kanavos Sep 30 '19 at 15:44
  • I have tried various permutations of that, but gorm does not like when you insert "\\" into the connection string. The result is: invalid character "\\" in host name. – Arlo Guthrie Sep 30 '19 at 15:47
  • I realize that doesn't look like a SQL Server connect string, this is GORM's interpretation of a SQL Server connect string. :D – Arlo Guthrie Sep 30 '19 at 16:12

1 Answers1

6

TCP port 1433 is the default port for SQL Server. This port is also the official Internet Assigned Number Authority (IANA) socket number for SQL Server. Client systems use TCP 1433 to connect to the database engine; SQL Server Management Studio (SSMS) uses the port to manage SQL Server instances across the network. You can reconfigure SQL Server to listen on a different port, but 1433 is by far the most common implementation.

However, if you still want to open it, follow the below steps:

Step 1

Probably TCP/IP channel is disabled under SQL Server Configuration Manager. SO go there and enable all TCP/IP options.

enter image description here

Step 2

Just in case at the same place SQL Server Configuration Manager make sure you have 1433 port.

enter image description here

Step 3

Make sure that SQL server is configured to allow remote connections. Use MS SQL Management Studio and right click on the top node which server itself.

enter image description here

Credit from the solution found here.

cdrrr
  • 1,138
  • 4
  • 13
  • 44
  • 1
    This would apply only if the connection string pointed to the correct named database. It doesn't – Panagiotis Kanavos Sep 30 '19 at 15:52
  • @PanagiotisKanavos - totally agree and thanks for mentioning. Just wanted to expose how to configure and open the port in case of a pointed connection string. – cdrrr Sep 30 '19 at 15:54
  • 1
    There's a highly upvoted question that shows how to troubleshoot networking issues. If that were the case here the question would be closed as a duplicate already – Panagiotis Kanavos Sep 30 '19 at 15:55
  • @PanagiotisKanavos - there is. A solution I've posted the link to. – cdrrr Sep 30 '19 at 15:56
  • This is a partial answer. The final thing I did to make it work correctly was to go to SQL Server Configuration Manager -- > SQL Server Network Configuration -- > TCP/IP (double click) -- > IP Addresses -- > Scroll all the way down to IPALL -- > Set port to 1433 – Arlo Guthrie Sep 30 '19 at 16:08
  • @ArloGuthrie Partial or not, it helped you to get to the result. If you consider, accept my answer. Thanks! – cdrrr Sep 30 '19 at 16:10