Skip to content

MySQL pod access from Kubernetes

MySQL pod access from Kubernetes refers to the methods used to connect to a MySQL database running within a cluster from a local machine or other environments.

Local Access via Port Forwarding

To access a MySQL pod directly from a local environment, kubectl port-forward is commonly used. This command maps a local port to the port on the specific Pod running the database^[400-devops__06-Kubernetes__k8s-mysql__README.md].

The general command syntax is:

[kubectl port-forward](<./kubectl-port-forward.md>) [POD_NAME] 3306:3306 --address 0.0.0.0

For example, to access a specific MySQL Pod, you would execute:

[kubectl port-forward](<./kubectl-port-forward.md>) mysql-dp-8dfb795cf-2hkgm 3306:3306 --address 0.0.0.0

This command forwards traffic from local port 3306 to port 3306 on the Pod mysql-dp-8dfb795cf-2hkgm.^[400-devops__06-Kubernetes__k8s-mysql__README.md]

Identifying Target Pods

To execute the port-forwarding command, the specific Pod name must be known. This information can be retrieved by listing all Pods across all namespaces^[400-devops__06-Kubernetes__k8s-mysql__README.md].

[kubectl get pods](<./kubectl-get-pods.md>) -A

Sources

^[400-devops__06-Kubernetes__k8s-mysql__README.md]