Skip to content

Commit 6858698

Browse files
authored
Fix PyMySQL localhost resolution to IPv6 (#54)
PyMySQL resolves 'localhost' to ::1 (IPv6) instead of 127.0.0.1, which causes connection failures for users with existing MySQL connections configured with 'localhost'. Normalize localhost to 127.0.0.1 to ensure TCP/IP connections work. Co-authored-by: Peter Adams <18162810+Maxteabag@users.noreply.github.com>
1 parent 17855d8 commit 6858698

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

sqlit/db/adapters/mysql.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ def connect(self, config: ConnectionConfig) -> Any:
8888
raise
8989

9090
port = int(config.port or get_default_port("mysql"))
91+
# PyMySQL resolves 'localhost' to ::1 (IPv6) which often fails.
92+
# Normalize to 127.0.0.1 to ensure TCP/IP connection works.
93+
host = config.server
94+
if host and host.lower() == "localhost":
95+
host = "127.0.0.1"
9196
return pymysql.connect(
92-
host=config.server,
97+
host=host,
9398
port=port,
9499
database=config.database or None,
95100
user=config.username,

0 commit comments

Comments
 (0)