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

Ballerina连接MySQL报错:加载数据库驱动失败

问题

尝试使用以下Ballerina代码连接MySQL数据库:

import ballerina/io;
// import ballerina/http;
import ballerinax/mysql;
import ballerina/sql;

configurable string dbHost = ?;
configurable int dbPort = ?;
configurable string dbUser = ?;
configurable string dbPassword = ?;
configurable string dbName = ?;

type User record {
    int userId;
    string email;
    string password;
    string createdAt;
    string updatedAt;
};

mysql:Client|sql:Error dbClientResult = check new (
    user = dbUser,
    password = dbPassword,
    database = dbName,
    host = dbHost,
    port = dbPort
);
final mysql:Client dbClient = check dbClientResult;

public function main() returns error? {
     io:println("MySQL Connected to " + dbName);
     return;
}

isolated function insertUser(mysql:Client dbClient, User entry) returns sql:ExecutionResult|error {
    User {userId, email, password, createdAt, updatedAt} = entry;
    sql:ParameterizedQuery insertQuery = `INSERT INTO users (id, email, password, createdAt, updatedAt) 
                                          VALUES (${userId}, ${email}, ${password}, ${createdAt}, ${updatedAt})`;
    return dbClient->execute(insertQuery);
}

运行后遇到错误:

error: Error while loading database driver. This may be because the database driver path is not configured correctly in the `Ballerina.toml` file or provided database driver version is not supported by the connector

已添加mysql-connector-java-8.0.26.jar文件,并在Ballerina.toml中配置:

[mysql]
driverPath = "libs\\mysql-connector-java-8.0.26.jar"

使用版本:

  • Ballerina = 2201.12.10
  • MySQL连接器版本 = 1.16.1

请问该如何解决此驱动加载失败问题?

解决方案

  • 修正配置节名称:Ballerina MySQL连接器的正确配置节是[ballerinax.mysql],而非[mysql]。修改Ballerina.toml内容为:
    [ballerinax.mysql]
    driverPath = "libs\\mysql-connector-java-8.0.26.jar"
    
  • 校验驱动路径有效性:确认libs目录和Ballerina.toml处于同一项目根目录下,若路径层级不对,改用绝对路径(如C:\\my-project\\libs\\mysql-connector-java-8.0.26.jar)。
  • 升级驱动版本:Ballerina Swan Lake Update 10(2201.12.10)搭配MySQL连接器1.16.1时,建议使用MySQL驱动8.0.28及以上版本,替换现有旧版驱动jar包可解决兼容性问题。
  • 检查驱动文件完整性:重新从官方渠道下载驱动jar包,通过MD5校验值确认文件未损坏。
  • 验证Java环境兼容性:MySQL 8.x驱动要求Java 8及以上版本,执行java -version确认当前Java版本符合要求。
  • 清理缓存重构项目:运行bal clean清理项目缓存,再执行bal run重新构建运行,避免缓存导致的加载异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 03:57:06