What's the recommended way to connect to MySQL from Go?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The recommended way to connect to MySQL from Go is to use the standard database/sql package with the go-sql-driver/mysql driver. That combination gives you connection pooling, context support, prepared statements, and portability without committing you to a heavier abstraction too early.
Use database/sql With The MySQL Driver
Install the driver:
Then open the database handle:
Two details matter immediately:
- '
sql.Openvalidates the arguments and creates a pooled handle, but it does not establish the connection yet' - '
PingContextis the explicit connectivity check'
That pattern is more reliable than assuming sql.Open alone proves the database is reachable.
Configure The Connection Pool
The *sql.DB value is not a single connection. It is a concurrency-safe pool manager. That is why you should create it once and reuse it across the application.
A good starting configuration:
The right numbers depend on your workload and database limits, but the design principle is stable: keep one shared pool, not one new sql.Open per request.
Repeatedly opening and closing handles inside request handlers is a common performance mistake.
Use Context-Aware Queries
Once connected, use QueryContext, QueryRowContext, and ExecContext so timeouts and cancellations propagate cleanly.
This is the standard idiomatic style in modern Go. It keeps database operations integrated with request deadlines in web servers and background job cancellation in workers.
Parse Time Values Correctly
MySQL date and datetime values often surprise Go developers if the DSN omits parseTime=true. Without it, time-related columns may scan as byte slices or strings depending on the driver behavior and query context.
That is why the DSN example included:
If your application reads timestamp columns, this option is usually the right default.
Keep SQL Simple And Explicit
A small repository function is often enough:
For many services, database/sql plus a few focused query functions is simpler and easier to debug than introducing an ORM too early.
If you later want convenience helpers, libraries such as sqlx can layer on top without replacing the core model entirely.
Handle Credentials Carefully
Do not hardcode production credentials into the source file. Build the DSN from configuration or environment variables.
Example:
That still uses the same underlying approach, but it keeps secrets out of the codebase.
Common Pitfalls
The biggest mistake is calling sql.Open for every query or every HTTP request. *sql.DB is meant to be long-lived and shared.
Another mistake is skipping PingContext during startup and discovering connectivity problems only after the application begins serving traffic.
People also forget parseTime=true, then run into awkward scanning bugs with MySQL datetime fields.
Finally, avoid string interpolation for SQL values. Use placeholders with query arguments so the driver can bind parameters safely.
Summary
- The standard recommendation is
database/sqlwithgo-sql-driver/mysql. - Create one shared
*sql.DBand configure its connection pool deliberately. - Use
PingContextto verify connectivity and context-aware query methods for real work. - Include
parseTime=truewhen you need proper time scanning. - Prefer parameterized queries and configuration-driven DSN construction.
Related reading
- What's the recommended way to use pg_dump/ysql_dump with YugabyteDB to export data when a table is still receiving inserts?
- When are rows overwritten in cassandra
- When does DynamoDB throttle request?
- When does SQLiteOpenHelper onCreate / onUpgrade run?
- When I remove rows in Cassandra I delete only columns not row keys
- When is data consistency not an issue?
- When might 2 phase commit not make progress?
- When should I use a composite index?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.