title: How do I access MySQL as a Service on Kubernetes?
body: <p>I have the following deployment...</p>
  
  <pre><code>---
  apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: mysql-data-disk
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
  ---
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: mysql-deployment
    labels:
      app: mysql
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: mysql
    template:
      metadata:
        labels:
          app: mysql
      spec:
        containers:
          - name: mysql
            image: mysql:5.7
            ports:
              - containerPort: 3306
            volumeMounts:
              - mountPath: "/var/lib/mysql"
                subPath: "mysql"
                name: mysql-data
            env:
              - name: MYSQL_ROOT_PASSWORD
                valueFrom:
                  secretKeyRef:
                    name: mysql-secrets
                    key: ROOT_PASSWORD
        volumes:
          - name: mysql-data
            persistentVolumeClaim:
              claimName: mysql-data-disk
  </code></pre>
  
  <p>This works great I can access the db like this...</p>
  
  <pre><code>kubectl exec -it mysql-deployment-&lt;POD-ID&gt; -- /bin/bash
  </code></pre>
  
  <p>Then I run...</p>
  
  <pre><code>mysql -u root -h localhost -p
  </code></pre>
  
  <p>And I can log into it. However, when I try to access it as a service by using the following yaml...</p>
  
  <pre><code>---
  apiVersion: v1
  kind: Service
  metadata:
    name: mysql-service
  spec:
    selector:
      app: mysql
    ports:
      - protocol: TCP
        port: 3306
        targetPort: 3306
  </code></pre>
  
  <p>I can see it by running this <code>kubectl describe service mysql-service</code> I get...</p>
  
  <pre><code>Name:              mysql-service
  Namespace:         default
  Labels:            &lt;none&gt;
  Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                       {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"mysql-service","namespace":"default"},"spec":{"ports":[{"port":33...
  Selector:          app=mysql
  Type:              ClusterIP
  IP:                10.101.1.232
  Port:              &lt;unset&gt;  3306/TCP
  TargetPort:        3306/TCP
  Endpoints:         172.17.0.4:3306
  Session Affinity:  None
  Events:            &lt;none&gt;
  </code></pre>
  
  <p>and I get the ip by running <code>kubectl cluster-info</code> </p>
  
  <pre><code>#kubectl cluster-info
  Kubernetes master is running at https://192.168.99.100:8443
  KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  </code></pre>
  
  <p>but when I try to connect using Oracle SQL Developer like this...</p>
  
  <p><a href="https://i.stack.imgur.com/zk2M4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zk2M4.png" alt="enter image description here"></a></p>
  
  <p>It says it cannot connect.</p>
  
  <p><a href="https://i.stack.imgur.com/ZeEeW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZeEeW.png" alt="enter image description here"></a></p>
  
  <p>How do I connect to the MySQL running on K8s?</p>