我通过SQL Server 2008 R2安装设置了SQLEXPRESS实例,添加了一个包含所有服务器角色的安全登录,一个名为'sqluser'。
每当我尝试池连接时都面临异常..连接字符串正在构建如下:
<db.driverClassName>com.microsoft.sqlserver.jdbc.SQLServerDriver</db.driverClassName>
<database.username>sqluser</database.username>
<database.password>qwerty</database.password>
<database.schemaname>dbo</database.schemaname>
<database.url>jdbc:sqlserver://localhost\SQLEXPRESS</database.url>
但是连接面临这个堆栈跟踪的根本原因:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
... 71 more
服务器身份验证是SQL Server和Windows身份验证模式。但是,当我指定-S属性时,登录失败。
没有启用防火墙,SQL Server甚至接受来自远程主机的连接。
C:\Users\user>sqlcmd -U sqluser -P qwerty -Q "Select * FROM testdb.dbo.testtable"
输出:
integer
-------
1
2
3
4
(4 rows affected)
但是,当指定'localhost'时,查询失败...问题是为什么?
C:\Users\user>sqlcmd -S localhost/sqlexpress -U cpt -P 1234 -Q "Select * FROM cpt.dbo.testme"
输出:
HResult 0x43, Level 16, State 1
Named Pipes Provider: Could not open a connection to SQL Server [67].
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : A network-related or in stance-specific error .....
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout expired.
如果有人想知道,使用'%COMPUTERNAME'更改'localhost'是相同的结果。服务器作为LocalSystem实例运行。
修改的 为喇嘛分配答案虽然主要是提示:)这是我对问题的解决方案:
1)检查tcp ip all listener port是否设置为1434 2)检查连接字符串是否正确设置(database.url) 3)检查登录是否具有足够的访问权限(与此特定堆栈跟踪无关)
答案 0 :(得分:1)
我们是否相当确定唯一的问题就是'localhost'?
只是要判断是否可以使用斜杠以正确的方式尝试sqlcmd和正确的登录(如前文所述),看看它是否有效:
sqlcmd -S localhost\sqlexpress -U sqluser -P qwerty -Q "Select * FROM cpt.dbo.testme"
您提到它接受来自远程主机的命令,但您的示例命令行未指定要连接的服务器,因此无法从远程主机运行。
jdbc代码是否与SQL Server在同一台机器上运行?这将告诉我们是否可以从分析中排除防火墙。
答案 1 :(得分:1)
我已修复此问题,不记得HOST / INSTANCE或tcp / ip之间的 / 是否正在侦听。这些是用于创建登录,db和正确设置SQL服务的脚本命令:
将设置混合身份验证并打开tcpport 1433的批处理作业:
echo off
rem is sql server installed at this build number?
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS" >nul
if %errorlevel% EQU 0 (
echo SET : SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\Enabled : True
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\"&_
"MSSQL10_50.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp" /v Enabled /t REG_DWORD /d 0x1 /f
echo SET : SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\ListenOnAllIPs : True
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\"&_
"MSSQL10_50.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp" /v ListenOnAllIPs /t REG_DWORD /d 0x1 /f
echo SET : SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll\TcpPort : 1433
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\"&_
"MSSQL10_50.SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp\IPAll" /v TcpPort /d 1433 /f
reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\MSSQLServer" /t REG_DWORD /v LoginMode /d 2 /f
)
将在
上创建登录并设置混合身份验证的SQL脚本CREATE DATABASE dbnamehere;
GO
USE [master]
GO
CREATE LOGIN [usernamehere] WITH PASSWORD=N'1234', DEFAULT_DATABASE=[dbnamehere], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
EXEC master..sp_addsrvrolemember @loginame = N'usernamehere', @rolename = N'bulkadmin'
GO
EXEC master..sp_addsrvrolemember @loginame = N'usernamehere', @rolename = N'dbcreator'
GO
EXEC master..sp_addsrvrolemember @loginame = N'usernamehere', @rolename = N'diskadmin'
GO
EXEC master..sp_addsrvrolemember @loginame = N'usernamehere', @rolename = N'processadmin'
GO
EXEC master..sp_addsrvrolemember @loginame = N'usernamehere', @rolename = N'securityadmin'
GO
EXEC master..sp_addsrvrolemember @loginame = N'usernamehere', @rolename = N'serveradmin'
GO
EXEC master..sp_addsrvrolemember @loginame = N'usernamehere', @rolename = N'setupadmin'
GO
EXEC master..sp_addsrvrolemember @loginame = N'usernamehere', @rolename = N'sysadmin'
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2
GO
最后,断言正确的服务正在机器重启上运行
@echo off
sc config "MSSQL$SQLEXPRESS" start= auto
sc failure "MSSQL$SQLEXPRESS" actions= restart/180000/restart/180000/""/180000 reset= 86400
sc config "SQLBrowser" start= auto
sc failure "SQLBrowser" actions= restart/180000/restart/180000/""/180000 reset= 86400
net stop MSSQL$SQLEXPRESS
net start MSSQL$SQLEXPRESS
sc config "SQLBrowser" depend= "MSSQL$SQLEXPRESS"
net stop SQLBrowser
net start SQLBrowser
答案 2 :(得分:-1)
尝试:
C:\Users\user>sqlcmd -S 192.168.01\sqlexpress
-U cpt -P 1234 -Q "Select * FROM cpt.dbo.testme"
检查:SQL-Server-Log,如果SuperSocket在localhost上运行。如果没有,请开始配置。