Kubernetes MySql image persistent volume is non empty during init

This issue could be caused by the lost+found directory on the filesystem of the PersistentVolume.

I was able to verify this by adding a k8s.gcr.io/busybox container (in PVC set accessModes: [ReadWriteMany], OR comment out the database container):

- name: init
  image: "k8s.gcr.io/busybox"
  command: ["/bin/sh","-c","ls -l /var/lib/mysql"]
  volumeMounts:
  - name: database
    mountPath: "/var/lib/mysql"

There are a few potential workarounds...

Most preferable is to use a subPath on the volumeMounts object. This uses a subdirectory of the PersistentVolume, which should be empty at creation time, instead of the volume root:

volumeMounts:
- name: database
  mountPath: "/var/lib/mysql"
  subPath: mysql

Less preferable workarounds include:

  • Use a one-time container to rm -rf /var/lib/mysql/lost+found (not a great solution, because the directory is managed by the filesystem and is likely to re-appear)
  • Use mysql:5 image, and add args: ["--ignore-db-dir=lost+found"] to the container (this option was removed in mysql 8)
  • Use mariadb image instead of mysql

More details might be available at docker-library/mysql issues: #69 and #186