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

使用PowerShell的Add-OdbcDsn配置MariaDB ODBC DSN遇错误求助

解决MariaDB ODBC DSN PowerShell配置报错问题

问题场景

使用PowerShell自动配置MariaDB ODBC 3.1 Driver(64位)的用户DSN时,执行代码出现配置失败错误,尝试调整用户名密码参数又触发安全限制报错:

原配置代码

$DriverName = (Get-OdbcDriver -Name "MariaDB*" | Where-Object Platform -eq "64-bit").Name
# 设置$dsn, $server, $port, $user, $pwd和$db
$Properties = @("SERVER=$server", "PORT=$port", "USER=$user", "PASSWORD=$pwd", "DATABASE=$db")
Add-OdbcDsn -Name $DSNName -DriverName $DriverName -DsnType "User" -Platform "64-bit" -SetPropertyValue $Properties

触发的配置失败错误

Add-OdbcDsn : Driver's ConfigDSN, ConfigDriver, or ConfigTranslator failed (Installer error code: 11).
+     Add-OdbcDsn -Name $DSNName -DriverName $DriverName -DsnType "User ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (... ODBC User DSN):Root/Microsoft/...SFT_OdbcDsnTask) [Add
   -OdbcDsn], CimException
    + FullyQualifiedErrorId : MI RESULT 0,Add-OdbcDsn

改用UID/PWD后的安全错误

Add-OdbcDsn : Attempt to set the {UID or PWD} key of a DSN. These keys should not be stored in the registry for security reason. Provide the credential information at runtime via 
SQLDriverConnect, SQLConnect or SQLBrowseConnect.

错误原因

  1. 错误代码11:MariaDB ODBC驱动禁止在DSN中存储用户名、密码这类敏感信息,强行添加会触发驱动配置逻辑失败。
  2. 安全限制错误:驱动官方出于安全考虑,不允许将UID/PWD写入系统注册表(DSN信息存储位置),必须在数据库连接阶段动态提供凭据。

正确配置代码

移除Properties中的用户名和密码字段,仅配置非敏感的连接基础参数:

$DriverName = (Get-OdbcDriver -Name "MariaDB*" | Where-Object Platform -eq "64-bit").Name
# 仅配置服务器、端口、数据库三个核心参数
$Properties = @("SERVER=$server", "PORT=$port", "DATABASE=$db")
Add-OdbcDsn -Name $DSNName -DriverName $DriverName -DsnType "User" -Platform "64-bit" -SetPropertyValue $Properties

连接时动态提供凭据

DSN配置完成后,在实际连接数据库时,需要通过应用程序或脚本动态传入用户名和密码。举两个常用场景示例:

PowerShell中使用Invoke-SqlCmd

$cred = Get-Credential
Invoke-SqlCmd -Query "SELECT VERSION();" -Dsn $DSNName -Credential $cred

使用连接字符串附加凭据

# 建议从安全存储(如Azure Key Vault、Windows凭据管理器)读取用户名密码,避免硬编码
$connectionString = "DSN=$DSNName;UID=$user;PWD=$pwd"
# 后续可通过ADO.NET等数据库连接组件传入该字符串建立连接

内容的提问来源于stack exchange,提问作者gee3107

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:17:14