Kubernetes – Kubectl MySQL Client One-Liner

Some of my applications, if not all of them, use MySQL in some fashion. The database pod is not exposed outside it’s kube-network service and is only reachable from within it’s namespace… Troubleshooting or random database querying can be a hassle. Here’s a one-liner to help you:

kubectl run -it --rm --image=mysql:5.7 --restart=Never mysql-client -- mysql -hMYSQL_SERVICE -uMYSQL_ROOT -pMYSQL_ROOT_PASSWORD

The container will disappear when you exit the MySQL prompt.

For an even quicker startup, I have created the the alias ksql in my .zshrc

alias ksql='kubectl run -it --rm --image=mysql:5.7 --restart=Never mysql-client -- mysql -hMYSQL_SERVICE -uMYSQL_ROOT -pMYSQL_ROOT_PASSWORD'

And yes, storing the MySQL root password in your terminal session file is generally not a good idea. Use this alias on your own risk.

Query on 👾

Leave a Reply

Your email address will not be published. Required fields are marked *