如何在Ballerina中正确配置config.toml进行MySQL连接?

问题描述 投票:0回答:1

我正在开发 Ballerina 项目,需要连接到 MySQL 数据库。我正在使用

config.toml
来存储我的 MySQL 连接详细信息,但我不确定所需的格式和字段是否正确。

这是我迄今为止在

config.toml
中尝试过的:

host = "localhost"
port = 3306
username = "root"
password = "password"
database = "testdb"

在我的 Ballerina 服务中,我正在加载这些配置来初始化 MySQL 客户端,但我不断遇到诸如

Invalid database configuration
Connection timeout
之类的错误。

有人可以指导我在 Ballerina 中为 MySQL 连接配置

config.toml
文件的正确方法,以及如何在 Ballerina 代码中使用这些配置来建立成功的连接吗?

这是我的

db.bal
。我尝试了这些方法并收到了提到的错误错误消息

import ballerinax/mysql;
import ballerina/sql;
// import ballerinax/mysql.driver as _;

// Configurable variables
configurable string HOST = ?;
configurable int PORT = ?;
configurable string USERNAME = ?;
configurable string PASSWORD = ?;
configurable string DATABASE = ?;

// Client options configuration (if needed)
configurable mysql:Options & readonly connectionOptions = {};

// Initialize the MySQL client
mysql:Client|sql:Error dbClientResult = new (HOST, USERNAME, PASSWORD, DATABASE, PORT);

// Ensure dbClient is correctly initialized
 final mysql:Client dbClient = check dbClientResult;

# Insert a new user into the database
isolated function insertUser(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);
}

# Select a user by ID
isolated function selectUserById(int id) returns User|sql:Error {
    sql:ParameterizedQuery selectQuery = `SELECT * FROM users WHERE id = ${id}`;
    return dbClient->queryRow(selectQuery);
}

# Select a user by email (for login)
isolated function selectUserByEmail(string email) returns User|sql:Error {
    sql:ParameterizedQuery selectQuery = `SELECT * FROM users WHERE email = ${email}`;
    return dbClient->queryRow(selectQuery);
}

# Insert a new chat into the database
isolated function insertChat(Chat entry) returns sql:ExecutionResult|error {
    Chat {chatId, userId, role, text, img, createdAt} = entry;
    sql:ParameterizedQuery insertQuery = `INSERT INTO chats (id, userId, role, text, img, createdAt) 
                                          VALUES (${chatId}, ${userId}, ${role}, ${text}, ${img}, ${createdAt})`;
    return dbClient->execute(insertQuery);
}

# Select a chat by ID and userId
isolated function selectChatByIdAndUser(int id, int userId) returns Chat|sql:Error {
    sql:ParameterizedQuery selectQuery = `SELECT * FROM chats WHERE id = ${id} AND userId = ${userId}`;
    return dbClient->queryRow(selectQuery);
}

# Select all chats for a user
isolated function selectChatsByUserId(int userId) returns Chat[]|error {
    sql:ParameterizedQuery selectQuery = `SELECT * FROM chats WHERE userId = ${userId}`;
    stream<Chat, error?> chatStream = dbClient->query(selectQuery);
    return from Chat chat in chatStream select chat;
}

# Insert a new user chat entry into the database
isolated function insertUserChat(UserChat entry) returns sql:ExecutionResult|error {
    UserChat {id, userId, chatId, title, createdAt} = entry;
    sql:ParameterizedQuery insertQuery = `INSERT INTO user_chats (id, userId, chatId, title, createdAt) 
                                          VALUES (${id}, ${userId}, ${chatId}, ${title}, ${createdAt})`;
    return dbClient->execute(insertQuery);
}

# Select all user chats by userId
isolated function selectUserChatsByUserId(int userId) returns UserChat[]|error {
    sql:ParameterizedQuery selectQuery = `SELECT * FROM user_chats WHERE userId = ${userId}`;
    stream<UserChat, error?> userChatStream = dbClient->query(selectQuery);
    return from UserChat userChat in userChatStream select userChat;
}

我正在使用:

  • 芭蕾舞女演员版本:2201.10.0(天鹅湖更新10)

任何帮助或工作示例将不胜感激!

mysql ballerina
1个回答
1
投票

您的代码中有几个问题。

  1. 您需要
    mysql.driver
    导入,因为没有驱动程序就无法连接到 MySQL 数据库。
  2. 您的配置变量和
    Config.bal
    中的值不同。这些是区分大小写的,您应该遵循这一点。使用这个
    Config.toml
    可以解决这个问题:
    HOST = "localhost"
    PORT = 3306
    USERNAME = "root"
    PASSWORD = "password"
    DATABASE = "testdb"
    

您可以查看本指南以了解有关 Ballerina 配置的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.