When I run the following code,
import ballerina/http;
import ballerina/sql;
import ballerinax/mysql;
import ballerinax/mysql.driver as _;
type isValid record {
boolean valid;
string nic;
};
type Person record {
string mic;
@sql:Column {name: "firstname"}
string firstName;
@sql:Column {
name: "lastname"
}
string lastName;
};
configurable string database = ?;
configurable string username = ?;
configurable string host = ?;
mysql:Client mysqlEp = check new (host = host, user = username, database = database);
service / on new http:Listener(9090) {
resource function get checkNic/[string nic]() returns isValid|error? {
Person|error person = check mysqlEp->queryRow(`select * from nic_details where nic=${nic.trim()}`);
if (person is error) {
isValid result = {
valid: false,
nic: nic
};
return result;
} else {
isValid result = {
valid: true,
nic: nic
};
return result;
}
}
resource function get path() {
}
}
getting the following error:
error: Error in SQL connector configuration: Failed to initialize pool: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Caused by :Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Caused by :Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Caused by :Connection refused
I gave the port correctly which is 3306 (default), and also Xampp shows that the MySQL server is running on the port 3306.
Furthermore, I gave localhost
as host.
What could be the cause of the above error?
Here the vscode instance is running on a remote server. When you refer localhost
there, it refers to that remote server. Not your local machine.
That's why you cannot access your local MySQL instance via vscode here.
You have to have your MySQL instance in a place which is accessible via public internet (ex: Amazon RDS) and provide that IP/hostname to the MySQL client here.