You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET连接MySQL时SSPI调用失败问题求助

问题排查与解决方案

核心问题原因

你的连接字符串同时混用了MySQL账号密码认证(uid=root;pwd=)和Windows集成认证(Integrated Security=SSPI),这两种认证方式无法同时生效,导致SSPI调用失败,触发AuthenticationException异常(错误码80090326)。

另外,原代码的Catch块仅捕获MySqlException,但实际抛出的是AuthenticationException,因此无法捕获并显示正确的错误信息。


解决方案

方案1:使用MySQL账号密码认证(最常用)

删除连接字符串中的Integrated Security=SSPI,确保MySQL的root账号允许空密码(或填入正确密码)本地登录:

myConnectionString = "server=localhost;database=sakila;uid=root;pwd=;"

同时扩展Try-Catch块,捕获所有类型的异常以排查问题:

Try
    conn.ConnectionString = myConnectionString
    conn.Open()
    MessageBox.Show("数据库连接成功!")
Catch exMySql As MySql.Data.MySqlClient.MySqlException
    MessageBox.Show($"MySQL错误:{exMySql.Message}")
Catch exAuth As System.Security.Authentication.AuthenticationException
    MessageBox.Show($"认证错误:{exAuth.Message}")
Catch ex As Exception
    MessageBox.Show($"未知错误:{ex.Message}")
End Try

方案2:使用Windows集成认证(需MySQL配置支持)

若要使用Windows账号登录MySQL,需先确保MySQL服务器安装了auth_windows插件,并创建对应Windows用户的MySQL账号(例如CREATE USER '你的Windows用户名'@'localhost';),然后修改连接字符串,移除uid和pwd参数:

myConnectionString = "server=localhost;database=sakila;Integrated Security=SSPI"

额外排查项:SSL连接问题

如果MySQL服务器强制要求SSL连接,可在连接字符串中添加SslMode=none(无需SSL时)或SslMode=Required(需要SSL时):

myConnectionString = "server=localhost;database=sakila;uid=root;pwd=;SslMode=none"

内容的提问来源于stack exchange,提问作者Archibald De Praingy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 12:30:09