Anchit Pancholi
Anchit Pancholi

Reputation: 1214

How to connect MySQL running on Kubernetes

I have deployed my application on Google gcloud container engine. My application required MySQL. Application is running fine and connecting to MySQL correctly. But I want to connect MySQL database from my local machine using MySQL Client (Workbench, or command line), Can some one help me how to expose this to local machine? and how can I open MySQL command line on gcloud shell ?

I have run below command but external ip is not there :

$ kubectl get deployment
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
app-mysql   1         1         1            1           2m
$ kubectl get pods
NAME                         READY     STATUS             RESTARTS   AGE
app-mysql-3323704556-nce3w   1/1       Running            0          2m
$ kubectl get service
NAME        CLUSTER-IP    EXTERNAL-IP       PORT(S)    AGE
app-mysql   11.2.145.79   <none>            3306/TCP   23h

EDIT

I am using below yml file:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: app-mysql
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: app-mysql
    spec:
      volumes:
      - name: data
        emptyDir: {}
      containers:
      - name: mysql
        image: mysql:5.6.22
        env:
        - name: MYSQL_USER
          value: root
        - name: MYSQL_DATABASE
          value: appdb
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql/
---
apiVersion: v1
kind: Service
metadata:
  name: app-mysql
spec:
  selector:
    app: app-mysql
  ports:
  - port: 3306

Upvotes: 25

Views: 45860

Answers (4)

Rotem jackoby
Rotem jackoby

Reputation: 22198

There are 2 steps involved:

1 ) You first perform port forwarding from localhost to your pod:

kubectl port-forward <your-mysql-pod-name> 3306:3306 -n <your-namespace>

2 ) Connect to database:

mysql -u root -h 127.0.0.1 -p <your-password>
 

Notice that you might need to change 127.0.0.1 to localhost - depends on your setup.

If host is set to:
localhost - then a socket or pipe is used.
127.0.0.1 - then the client is forced to use TCP/IP.

You can check if your database is listening for TCP connections with netstat -nlp.


Read more in:

Cant connect to local mysql server through socket tmp mysql sock

Can not connect to server

Upvotes: 9

Tim
Tim

Reputation: 639

Try the kubectl port-forward command.

In your case; kubectl port-forward app-mysql-3323704556-nce3w 3306:3306

See The documentation for all available options.

Upvotes: 39

Nadir Latif
Nadir Latif

Reputation: 3773

You need to add a service to your deployment. The service will add a load balancer with a public ip in front of your pod, so it can be accessed over the public internet.

See the documentation on how to add a service to a Kubernetes deployment. Use the following code to add a service to your app-mysql deployment:

kubectl expose deployment/app-mysql

You may also need to configure your MySQL service so it allows remote connections. See this link on how to enable remote access on MySQL server:

Upvotes: 0

Dawid Gorczyca
Dawid Gorczyca

Reputation: 991

To add to the above answer, when you add --address 0.0.0.0 kubectl should open 3306 port to the INTERNET too (not only localhost)!

kubectl port-forward POD_NAME 3306:3306 --address 0.0.0.0

Use it with caution for short debugging sessions only, on development systems at most. I used it in the following situation:

  • colleague who uses Windows
  • didn't have ssh key ready
  • environment was a playground I was not afraid to expose to the world

Upvotes: 3

Related Questions