Writing MySQL Proxy in GO for self-learning: Part 1 — TCP Proxy

  • Testing and benchmarking of the infrastructure
  • Testing your applications for dealing with MySQL network failures and delays
  • Wrap the MySQL usage with own business layer
  • Load balancing
  • Multiplexing


I’m not sure if I can exactly categorize the content of this and the next articles as junior, middle or senior-oriented. I’ll assume that you worked with or at least touched things like:

  • HTTP Web Server
  • HTTP Client
  • TCP Socket, TCP Connection
  • Port
  • MySQL
  • Any TCP/HTTP Proxy (NGINX, ELB)


I’m not gonna look to existing MySQL Proxies. There are many of them, including the official one. I’ll do it from scratch and will try to feel the pain of implementing something by following the official documentation.


  • Running MySQL Server
  • Golang environment installed
  • macOS or Linux
  • IDE

Let’s start

This article will be focused on creating a general, very simple TCP Proxy, that will be used as a start point for writing MySQL Proxy. At the and of this article, we will have a program that will be used as a man-in-the-middle between MySQL Client and MySQL Server.

  • Create a TCP Server that will listen for incoming TCP connection
  • Accept the connection, and run a handler as a go-routine
  • Create TCP connection to MySQL
  • Start proxying byte stream from the client to the MySQL Server, and back by using pipes


We created the simplest TCP Proxy. To run it locally, just clone the repository, and run

go run .

What’s next?

The next article is almost ready. A little spoiler :)

  • We will introduce abstractions so our TCP Proxy will be more modular and maintainable
  • We’ll do refactor and get rid of io.Copy() function, cause we need access to a raw byte stream
  • We’ll learn a general MySQL Protocol concepts
  • We’ll implement decoding of MySQL Connection Phase
  • We’ll meet go-sql-driver/mysql, and use it in tests
  • At the and, we’ll have working partially implemented MySQL Proxy (Connection Phase)


I hope you enjoyed. If you are new to Golang, I suggest you to through Golang Tour. It should be enough to understand the code.


Your feedback is welcome! Critics, suggestions, whatever :)


Second part of these series — Writing MySQL Proxy in GO for self-learning: Part 2 — decoding handshake packet



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alexander Ravikovich

Alexander Ravikovich

In GO we trust. Software Engineer. @Isreal