:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mysql): give specific connection errors at source validation (#65446)
Tom Owers4f49e22ac8e4
diff --git a/posthog/temporal/data_imports/sources/mysql/source.py b/posthog/temporal/data_imports/sources/mysql/source.pyindex 5e1a3ca3a2fc..99d81a666f9e 100644--- a/posthog/temporal/data_imports/sources/mysql/source.py+++ b/posthog/temporal/data_imports/sources/mysql/source.py@@ -34,6 +34,40 @@ from products.data_warehouse.backend.types import ExternalDataSourceType_MYSQL_IMPLEMENTATION = MySQLImplementation()+# Create-time-only refinement of the connection error shown when a source fails to+# validate. pymysql collapses every connect-level failure into error 2003,+# "Can't connect to MySQL server on '<host>' (<os detail>)", so the generic+# "check all connection details" message can't tell the user whether the host is+# wrong, the port is closed, or a firewall is dropping us — unlike the Postgres+# source, which is granular. We match the OS detail (and the 1049 "Unknown database"+# server error) to give the same actionable messages. Kept out of+# get_non_retryable_errors — which the sync path also consults for retry+# classification — so connection-error retry behaviour is unchanged.+_VALIDATE_CONNECTION_HINTS: list[tuple[str, str]] = [+ (+ "Name or service not known",+ "Host could not be resolved. Check the host is spelled correctly and reachable from PostHog.",+ ),+ (+ "nodename nor servname provided",+ "Host could not be resolved. Check the host is spelled correctly and reachable from PostHog.",+ ),+ (+ "Connection refused",+ "Could not connect to the host on the port given. Check the host and port are correct and the MySQL server is accepting connections.",+ ),+ ("timed out", "Connection timed out. Does your database have our IP addresses allowed?"),+ (+ "No route to host",+ "Could not reach the host. Check the host is correct and that PostHog's IP addresses are allowed through your firewall.",+ ),+ (+ "Network is unreachable",+ "Could not reach the host. Check the host is correct and that PostHog's IP addresses are allowed through your firewall.",+ ),+ ("Unknown database", "Database does not exist. Check the database name is correct."),+]+@SourceRegistry.registerclass MySQLSource(SQLSource[MySQLSourceConfig], SSHTunnelMixin, ValidateDatabaseHostMixin):@@ -245,6 +279,10 @@ class MySQLSource(SQLSource[MySQLSourceConfig], SSHTunnelMixin, ValidateDatabase# message without reporting them to error tracking — only genuinely unexpected# failures get captured. Mirrors the Postgres and MSSQL `validate_credentials` handling.error_msg = " ".join(str(arg) for arg in e.args) if e.args else str(e)+ # Refine the generic connect failure into a specific, actionable message first.+ for hint_pattern, hint_message in _VALIDATE_CONNECTION_HINTS:+ if hint_pattern in error_msg:+ return False, hint_messagefor pattern, friendly_error in self.get_non_retryable_errors().items():if pattern in error_msg:return (diff --git a/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py b/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.pyindex 2b0e97384570..61b322b32f90 100644--- a/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py+++ b/posthog/temporal/data_imports/sources/mysql/tests/test_mysql.py@@ -1276,20 +1276,42 @@ class TestMySQLSourceValidateCredentials:@pytest.mark.parametrize("raised,expected_error",[- # The exact error that fired this triage: an unreachable host surfaces as a- # pymysql OperationalError(2003) wrapping an OSError — already non-retryable.- # A connection failure keeps the generic "check connection details" message.+ # pymysql collapses every connect-level failure into OperationalError(2003)+ # wrapping an OSError; the OS detail is matched to give a specific, actionable+ # message instead of the generic "check connection details" fallback.(pymysql.err.OperationalError(- 2003, "Can't connect to MySQL server on 'db.example.com' ([Errno 101] Network is unreachable)"+ 2003, "Can't connect to MySQL server on 'db.example.com' ([Errno -2] Name or service not known)"),- "Could not connect to MySQL. Please check all connection details are valid.",+ "Host could not be resolved. Check the host is spelled correctly and reachable from PostHog.",),(pymysql.err.OperationalError(2003, "Can't connect to MySQL server on 'db.example.com' ([Errno 111] Connection refused)"),- "Could not connect to MySQL. Please check all connection details are valid.",+ "Could not connect to the host on the port given. Check the host and port are correct and the MySQL server is accepting connections.",+ ),+ (+ pymysql.err.OperationalError(2003, "Can't connect to MySQL server on 'db.example.com' (timed out)"),+ "Connection timed out. Does your database have our IP addresses allowed?",+ ),+ (+ pymysql.err.OperationalError(+ 2003, "Can't connect to MySQL server on 'db.example.com' ([Errno 113] No route to host)"+ ),+ "Could not reach the host. Check the host is correct and that PostHog's IP addresses are allowed through your firewall.",+ ),+ (+ pymysql.err.OperationalError(+ 2003, "Can't connect to MySQL server on 'db.example.com' ([Errno 101] Network is unreachable)"+ ),+ "Could not reach the host. Check the host is correct and that PostHog's IP addresses are allowed through your firewall.",+ ),+ # Server error 1049: the host/port/credentials are fine but the named database+ # doesn't exist. Previously fell through to capture as an unexpected error.+ (+ pymysql.err.OperationalError(1049, "Unknown database 'nope'"),+ "Database does not exist. Check the database name is correct.",),# An auth failure (error 1045) must name the credentials, not the generic message# that sends the user to inspect the host/port instead. Mirrors Postgres.