23

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

4 Answers 4

36

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.

4
  • i dont know if this is correct or not but its not working for me .. its showing below Forwarding from 127.0.0.1:3306 -> 3306 Forwarding from [::1]:3306 -> 3306 and after that when i run kubectl get services its not showing external ip Oct 7, 2016 at 14:21
  • You'll have to use the MySQL client on your local machine and connect to localhost:3306. This will use the tunnel that you created using kubectl port-forward. Oct 7, 2016 at 19:55
  • 1
    its working fine now, i have reinstalled gcloud. Thanks For help Oct 8, 2016 at 22:46
  • 1
    @Tim Slot I have the same case. I did all the above steps and it didn't work for me. Nothing is showing on my workbench at localhost. Jan 17, 2017 at 0:23
6

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

2

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
-1

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:

2
  • 3
    i dont think this might be correct way with kubernetes. Oct 8, 2016 at 10:58
  • I updated the answer so it is more relevant to the question Jan 7, 2021 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.