Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

fix(windows): MySQL connection on external servers + surface real error

DangHuynh23e8cdd6cfaa
diff --git a/windows/Gridex/ConnectionManager.cpp b/windows/Gridex/ConnectionManager.cpp
index 0f5991e..2475a00 100644
--- a/windows/Gridex/ConnectionManager.cpp
+++ b/windows/Gridex/ConnectionManager.cpp
@@ -92,6 +92,45 @@ namespace DBModels
return ok;
}
+ bool ConnectionManager::testConnectionWithError(
+ const ConnectionConfig& config,
+ const std::wstring& password,
+ std::wstring& outError)
+ {
+ outError.clear();
+
+ std::unique_ptr<SSHTunnelService> testTunnel;
+ auto effectiveConfig = applySSHTunnel(config, password, testTunnel);
+
+ auto adapter = createAdapter(config.databaseType);
+
+ // Call adapter->connect directly instead of the bool
+ // testConnection shortcut: connect() throws DatabaseError with
+ // the driver's own message, which is exactly what we want to
+ // surface in the UI.
+ bool ok = false;
+ try
+ {
+ adapter->connect(effectiveConfig, password);
+ auto result = adapter->execute(L"SELECT 1");
+ adapter->disconnect();
+ ok = result.success;
+ if (!ok) outError = result.error;
+ }
+ catch (const std::exception& e)
+ {
+ const std::string w = e.what();
+ outError.assign(w.begin(), w.end());
+ }
+ catch (...)
+ {
+ outError = L"Unknown error";
+ }
+
+ if (testTunnel) testTunnel->close();
+ return ok;
+ }
+
std::shared_ptr<DatabaseAdapter> ConnectionManager::createAdapter(DatabaseType type)
{
switch (type)
diff --git a/windows/Gridex/HomePage.cpp b/windows/Gridex/HomePage.cpp
index 71dc35b..834c7cb 100644
--- a/windows/Gridex/HomePage.cpp
+++ b/windows/Gridex/HomePage.cpp
@@ -366,13 +366,15 @@ namespace winrt::Gridex::implementation
try
{
DBModels::ConnectionManager mgr;
- if (!mgr.testConnection(config, config.password))
+ std::wstring err;
+ if (!mgr.testConnectionWithError(config, config.password, err))
{
+ std::wstring msg = L"Could not connect to " + config.name + L".";
+ if (!err.empty()) msg += L"\n\n" + err;
+ else msg += L" Check host, port, credentials, and ensure the server is running.";
muxc::ContentDialog errDlg;
errDlg.Title(winrt::box_value(winrt::hstring(L"Connection Failed")));
- errDlg.Content(winrt::box_value(winrt::hstring(
- L"Could not connect to " + config.name +
- L". Check host, port, credentials, and ensure the server is running.")));
+ errDlg.Content(winrt::box_value(winrt::hstring(msg)));
errDlg.CloseButtonText(L"OK");
errDlg.XamlRoot(this->XamlRoot());
errDlg.ShowAsync();
@@ -504,13 +506,15 @@ namespace winrt::Gridex::implementation
try
{
DBModels::ConnectionManager mgr;
- if (!mgr.testConnection(selectedConn, selectedConn.password))
+ std::wstring err;
+ if (!mgr.testConnectionWithError(selectedConn, selectedConn.password, err))
{
+ std::wstring msg = L"Could not connect to " + selectedConn.name + L".";
+ if (!err.empty()) msg += L"\n\n" + err;
+ else msg += L" Check host, port, credentials, and ensure the server is running.";
muxc::ContentDialog errDlg;
errDlg.Title(winrt::box_value(winrt::hstring(L"Connection Failed")));
- errDlg.Content(winrt::box_value(winrt::hstring(
- L"Could not connect to " + selectedConn.name +
- L". Check host, port, credentials, and ensure the server is running.")));
+ errDlg.Content(winrt::box_value(winrt::hstring(msg)));
errDlg.CloseButtonText(L"OK");
errDlg.XamlRoot(this->XamlRoot());
errDlg.ShowAsync();
diff --git a/windows/Gridex/Models/ConnectionManager.h b/windows/Gridex/Models/ConnectionManager.h
index 3400156..07fd730 100644
--- a/windows/Gridex/Models/ConnectionManager.h
+++ b/windows/Gridex/Models/ConnectionManager.h
@@ -37,6 +37,15 @@ namespace DBModels
const ConnectionConfig& config,
const std::wstring& password);
+ // Same as testConnection but surfaces the underlying adapter
+ // error message (libpq / libmariadb / sqlite3 text) so UI can
+ // show the real reason instead of a generic "check host/port"
+ // line. outError stays empty when the call returns true.
+ bool testConnectionWithError(
+ const ConnectionConfig& config,
+ const std::wstring& password,
+ std::wstring& outError);
+
// Get the active adapter (nullptr if not connected)
std::shared_ptr<DatabaseAdapter> getActiveAdapter() const { return activeAdapter_; }
diff --git a/windows/Gridex/MySQLAdapter.cpp b/windows/Gridex/MySQLAdapter.cpp
index 16a773a..be79d9d 100644
--- a/windows/Gridex/MySQLAdapter.cpp
+++ b/windows/Gridex/MySQLAdapter.cpp
@@ -83,19 +83,63 @@ namespace DBModels
// Set UTF-8
mysql_options(conn_, MYSQL_SET_CHARSET_NAME, "utf8mb4");
- // SSL — respect sslMode: Preferred/Required skip cert verification
- // (common for self-signed certs); VerifyCA/VerifyIdentity enforce it.
- if (config.sslMode != DBModels::SSLMode::Disabled)
+ // SSL policy.
+ //
+ // MariaDB Connector/C 3.4 (vcpkg build) auto-upgrades the
+ // connection to TLS whenever the server advertises CLIENT_SSL —
+ // even if the caller never touched any SSL option. The client's
+ // default cert-verify flag is TRUE, so a self-signed / untrusted
+ // server cert then bombs with "Certificate verification failure:
+ // The certificate is NOT trusted", turning what the user picked
+ // as "Disabled" into a verification failure instead of a plain
+ // TCP connect. macOS MySQLNIO doesn't have this auto-upgrade so
+ // the same server works there with Disabled.
+ //
+ // Pragmatic fix: always disable cert verification UNLESS the
+ // user explicitly picked VerifyCA / VerifyIdentity. That covers
+ // Disabled (may or may not get SSL silently, but won't fail on
+ // cert), Preferred, and Required.
{
- mysql_ssl_set(conn_, nullptr, nullptr, nullptr, nullptr, nullptr);
+ bool wantSSL =
+ (config.sslMode != DBModels::SSLMode::Disabled);
+ unsigned char enforce = wantSSL ? 1 : 0;
+ mysql_options(conn_, MYSQL_OPT_SSL_ENFORCE, &enforce);
bool verifyCert =
(config.sslMode == DBModels::SSLMode::VerifyCA ||
config.sslMode == DBModels::SSLMode::VerifyIdentity);
unsigned char verify = verifyCert ? 1 : 0;
mysql_options(conn_, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &verify);
+
+ if (wantSSL)
+ {
+ mysql_ssl_set(conn_, nullptr, nullptr, nullptr, nullptr, nullptr);
+ }
}
+ // MariaDB Connector/C 3.4+ defaults its restricted_auth list to
+ // a subset that on some vcpkg builds **excludes**
+ // mysql_native_password — the most common MySQL auth plugin and
+ // the one MySQLNIO (macOS Gridex) has no problem with. On
+ // Windows this caused "Authentication plugin 'mysql_native_password'
+ // couldn't be found in restricted_auth plugin list." whenever a
+ // server's user account wasn't on caching_sha2_password.
+ //
+ // NOTE: passing "" would mean "whitelist = nothing" → block
+ // every plugin, which is worse. Pass an explicit whitelist
+ // covering every plugin libmariadb supports and that real
+ // servers actually use. Mirrors the mysql CLI behaviour.
+ static const char* kAllowedAuthPlugins =
+ "mysql_native_password,"
+ "caching_sha2_password,"
+ "sha256_password,"
+ "mysql_clear_password,"
+ "dialog,"
+ "auth_gssapi_client,"
+ "client_ed25519";
+ mysql_optionsv(conn_, MARIADB_OPT_RESTRICTED_AUTH,
+ (void*)kAllowedAuthPlugins);
+
auto host = toUtf8(config.host);
auto user = toUtf8(config.username);
auto pass = toUtf8(password);