1. 程式人生 > 其它 >k8s 執行 mysql 報錯 --initialize specified but the data directory has files in it

k8s 執行 mysql 報錯 --initialize specified but the data directory has files in it

最近在 k8s 上面執行 mysql 報錯

$ kubectl -n devops logs mysql-679745f64f-4cdzc 
2021-12-10 01:18:26+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2021-12-10 01:18:26+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-12-10 01:18:26+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2021-12-10 01:18:26+00:00 [Note] [Entrypoint]: Initializing database files
2021-12-10T01:18:26.354668Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-10T01:18:26.355814Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2021-12-10T01:18:26.355851Z 0 [ERROR] Aborting

提示資料目錄非空,什麼鬼,好吧,我們先執行一個 initContainers 看看目錄裡面到底有啥

      initContainers:
      - name: init-data-path
        image: hub.leffss.com/library/busybox:v1.28.4
        imagePullPolicy: IfNotPresent
        command: ["sh", "-c", "ls -l /var/lib/mysql"]
        securityContext:
          privileged: true
        volumeMounts:
        - name: mysql-pvc
          mountPath: /var/lib/mysql
# 檢視 initContainers 的日誌
kubectl -n devops logs mysql-679745f64f-4cdzc -c init-data-path
total 16
drwx------    2 root     root         16384 Dec 10 01:18 lost+found

原來是 lost+found,這個目錄啥東西,有啥用這裡就不介紹了,自己百度,反正以我工作這麼多年的經驗來說,沒啥用

為啥會有這個目錄呢?因為我們 pvc 使用的是 ceph 的 rbd,每次建立時都會格式化,就會產生這個,如果使用 cephfs 或者 nfs 的話,不會有這個目錄

好吧,既然沒用,那就盤它:

      initContainers:
      - name: init-data-path
        image: hub.leffss.com/library/busybox:v1.28.4
        imagePullPolicy: IfNotPresent
        command: ["sh", "-c"]
        args:
        - |
          if [[ -d /var/lib/mysql/lost+found ]];then
            echo "rm -rf /var/lib/mysql/lost+found"
            rm -rf /var/lib/mysql/lost+found
          else
            echo "/var/lib/mysql/lost+found not exist"
          fi
        securityContext:
          privileged: true
        volumeMounts:
        - name: mysql-pvc
          mountPath: /var/lib/mysql

刪除,整個世界清淨了!

$ kubectl -n devops get pod
NAME                     READY   STATUS    RESTARTS   AGE
mysql-774868b794-8d258   1/1     Running   0          13m
redis-7b4c48fd74-g99zg   1/1     Running   0          32m