<p>I want that mysql pod doesn't remove all mysql data when I restart the computer.</p>
<p>I should be able to store the data in my machine, so when I reboot my computer and the mysql pod starts again, the databases are still there.</p>
<p>here are my yaml's:</p>
<p><strong>storage-class.yaml</strong></p>
<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
</code></pre>
<p><strong>mysql-pv.yaml</strong></p>
<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  volumeMode: Filesystem
  storageClassName: local-storage
  local:
    path: &quot;C:\\mysql-volume&quot; #2 \ for escape characters right?
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop
  #hostPath:
   # path: /mysql-volume
    #type: DirectoryOrCreate
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    type: local
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
</code></pre>
<p><strong>mysql-deployment.yaml</strong></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
  - protocol: TCP
    port: 3306
    nodePort: 30001
  selector:        
    app: mysql
  type: NodePort
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql-custom-img-here
        imagePullPolicy: IfNotPresent
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-root-password
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-user
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim
</code></pre>
<p>After trying that, the first error I got was:</p>
<pre><code>MountVolume.NewMounter initialization failed for volume &quot;mysql-pv-volume&quot; : path &quot;C:\\mysql-volume&quot; does not exist
</code></pre>
<p>Since im using windows, I guess that's the correct path right? Im using 2 &quot;&quot; for a escape character, Maybe the problem is here in the path, but not sure. If it is, how can I give my local path on my windows machine?</p>
<p>Then I changed the <code>local: path:</code> to <code>/opt</code> and the following error apeared:</p>
<pre><code>initialize specified but the data directory has files in it.
</code></pre>
<p>log:</p>
<pre><code>2020-09-24 12:53:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.31-1debian10 started.
2020-09-24 12:53:00+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-09-24 12:53:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.31-1debian10 started.
2020-09-24 12:53:00+00:00 [Note] [Entrypoint]: Initializing database files
2020-09-24T12:53:00.271130Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-09-24T12:53:00.271954Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2020-09-24T12:53:00.271981Z 0 [ERROR] Aborting
</code></pre>
<p>but if I change the <code>mountPath: /var/lib/mysql</code> to for example <code>mountPath: /var/lib/mysql-test</code></p>
<p>It works, but not as expected(saving the data after rebooting the computer).</p>
<p>Even after removing the PV, PVC and MYSQL deployment/service, that same error keeps appearing.
I even removed the volumes using the docker command, and changing my mysql custom image just to 'mysql:5.7' just in case, but the same <code>initialize specified but the data directory has files in it.</code> appears.</p>
<p>How does that happen, even when I remove the pod? mountPath is the container path, so the data should disappear.</p>
<p>And how can I give my local path in the persistentVolume?</p>
<p>Thanks for your time!</p>
<p>edit: forgot the tell that I already saw this: <a href="https://stackoverflow.com/questions/54406110/how-to-create-a-mysql-kubernetes-service-with-a-locally-mounted-data-volume">How to create a mysql kubernetes service with a locally mounted data volume?</a></p>
<p>I searched a lot, but no luck</p>

<p>I finally solved the problem...</p>
<p>The problem of <code>initialize specified but the data directory has files in it.</code> was answered by @Jakub</p>
<p>The <code>MountVolume.NewMounter initialization failed for volume &quot;mysql-pv-volume&quot; : path &quot;C:\\mysql-volume&quot; does not exist</code> .... I can't even believe the time spent because of this silly problem...</p>
<p>the correct path is: <code>path: /c/mysql-volume</code> after that, all worked as expected!</p>
