函数计算无法访问自建数据库,函数运行环境为.NET Core 3.1
using (var conn = new SqlConnection(connStr))
{
conn.Open();
...
}
数据库连接串为 Data Source=ip,端口;Initial Catalog=数据库名;User ID=账号;Password=密码
报错信息:
System.Data.SqlClient.SqlException (0x80131904): Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=41; handshake=231; [Login] initialization=2; authentication=9; [Post-Login] complete=14179;
---> System.ComponentModel.Win32Exception (258): Unknown error 258 at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) at System.Data.SqlClient.SqlConnection.Open() Error Number:-2,State:0,Class:11
函数可以访问自建mssql数据库
在函数内可以调用http的接口说明函数是可以连到自建的服务器的,代码及数据库连接串正确
asp.net core 程序是跑在容器中的,容器镜像用的是 mcr.microsoft.com/dotnet/core/aspnet:3.0 运行容器内的 openssl 命令发现 openssl 的版本比较高 # docker exec -t $(docker ps --filter name=blog_api -q) openssl version OpenSSL 1.1.1b 26 Feb 2019 查看 openssl.cnf 配置文件 # docker exec -t $(docker ps --filter name=blog_api -q) cat /etc/ssl/openssl.cnf [system_default_sect] MinProtocol = TLSv1.1 CipherString = DEFAULT@SECLEVEL=2 发现允许的 ssl 最低版本是 TLSv1.2 ,而程序所使用的 SQL Server 数据库版本比较低不支持 TLSv1.2 ,修改为 TLSv1.0 后问题解决 修改方法:在 Dockerfile 中添加下面的指令 RUN sed -i 's/TLSv1.2/TLSv1.0/g' /etc/ssl/openssl.cnf
函数计算是可以访问数据库的,函数计算不会区分你是自建的还是用的云数据库,我本人自己尝试过自建和云数据库都是可以用的。目前我就是在本地物理机搭建的数据库,用frp做代理映射到工网,然后函数计算访问,无论是VPC内还是公网访问,也都是没问题的。所以上面的问题大概率还是代码和配置原因。
初步来看是“Connection Timeout Expired. ”,一般情况下都要考虑:
这个问题目前看确实是代码和配置因素。如果有问题也可以进一步联系我。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。