<p>I have deployed a mysql database in kubernetes and exposed in via a service. When my application tries to connect to that database it keeps being refused. I also get the same when I try to access it locally. Kubernetes node is run in minikube.</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
---
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_db
        imagePullPolicy: Never
        ports:
        - containerPort: 3306
        volumeMounts:
        - name:  mysql-persistent-storage
          mountPath:  "/var/lib/mysql"
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
</code></pre>

<p>And here's my yaml for persistent storage:</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec: 
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/Users/Work/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
</code></pre>

<p>After this I get this by running <code>minikube service list</code>:</p>

<pre><code>default              | mysql-service             | http://192.168.99.101:31613 
</code></pre>

<p>However I cannot access the database neither from my application nor my local machine.
What am I missing or did I misconfigure something?</p>

<p>EDIT:
I do not define any envs here since the image run by docker already a running mysql db and some scripts are run within the docker image too.</p>

<p>Mysql must not have started, confirm it by checking the logs. <code>kubectl get pods | grep mysql</code>; <code>kubectl logs -f $POD_ID</code>. Remember you have to specify the environment variables <strong>MYSQL_DATABASE</strong> and <strong>MYSQL_ROOT_PASSWORD</strong> for mysql to start. If you don't want to set a password for root also specify the respective command. Here I am giving you an example of a mysql yaml.</p>

<pre><code>---
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_db
        imagePullPolicy: Never
        env:
        - name: MYSQL_DATABASE
          value: main_db
        - name: MYSQL_ROOT_PASSWORD
          value: s4cur4p4ss
        ports:
        - containerPort: 3306
        volumeMounts:
        - name:  mysql-persistent-storage
          mountPath:  "/var/lib/mysql"
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
</code></pre>
