Home > Backend Development > Golang > Why Can\'t My Go Program Connect to SQL Server?

Why Can\'t My Go Program Connect to SQL Server?

Linda Hamilton
Release: 2024-11-24 13:19:13
Original
397 people have browsed it

Why Can't My Go Program Connect to SQL Server?

Go with SQL Server: Unable to Connect Successfully

Problem:

When using Go with a SQL Server driver to connect to a database, an error occurs:

login failed for user 'sakhaloo'
Copy after login

Additionally, attempts to open the database using SQL Server Management Studio fail with the error:

Error:233: 
A connection was successfully established with the server, but then an
error occurred during the login process. (provider: Shared Memory
Provider, error: 0-no process is on the other end of the
pipe.)(Microsoft SQL Server,)
Copy after login

Cause:

The issue stems from a combination of factors:

  • The SQL Server instance is not properly configured for TCP connections.
  • The Go program is attempting to use Windows Authentication without specifying the domain or user.
  • The SQL Server user does not have the necessary permissions.

Solution:

  1. Configure TCP Connections for SQL Server:
  • Open SQL Server Configuration Manager.
  • Expand Network Configuration > Protocols for MSSQLSERVER.
  • Enable TCP/IP.
  • Restart the SQL Server service.
  1. Create an SQL Server User with Permissions:
  • Use SQL Server Management Studio to create an SQL Server user with the necessary permissions.
  • Ensure the user has sufficient privileges to create and modify the database.
  1. Specify the SQL Server User in the Go Program:
  • In the Go program, specify the SQL Server user name and password in the connection string.
  • If Windows Authentication is desired, leave the user name and password blank, but ensure that the SQL Server instance is configured for Kerberos authentication.

Updated Solution for Trusted Windows Authentication:

  • On Windows, if the user id is empty or missing, Single-Sign-On (trusted connection) is used.
  • To use Trusted Windows Authentication, ensure the following in the Go program:

    condb, errdb := sql.Open("mssql", "server=servernameorip;user>
    Copy after login

The above is the detailed content of Why Can\'t My Go Program Connect to SQL Server?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template