Search code examples
mysqlkubernetesinitializationinnodbmysql-operator

How to initialize InnoDB Cluster created via Kubernetes mysql-operator through simple script.sql


I've successfully deployed in Kubernetes a InnoDB Cluster via mysql-operator and I've also set up the scheduled backup. I'm also able to init the DB via the same backup created by InnoDB.

However I can't find a way to init the DB the first time by importing a simple logical backup which is defined by a simple .sql script.

Is it possible to achieve that via mysql-operator or via InnoDB kubernetes manifest? Possible alternatives?

Relevant documentation:

InnoDBCluster manifest:

apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
  name: mysql-cluster
  namespace: cr-platform
spec:
  secretName: mysql-root-user
  instances: 2
  tlsUseSelfSigned: true
  router:
    instances: 1
  backupSchedules:
    - name: daily-2300
      enabled: true
      schedule: "0 23 * * *"
      backupProfileName: s3-backup
  backupProfiles:
    - name: s3-backup
      dumpInstance:
        dumpOptions:
          chunking: false
        storage:
          s3:
            bucketName: al-mysql
            prefix: /backup
            config: s3-secret
            profile: default
            endpoint: https://s3-endpoint
  ## Restore from InnoDB dump but not from .sql script
  initDB:
    dump:
      dumpOptions:
        dryRun: false
      storage:
        # backup
        s3:
          bucketName: al-mysql
          prefix: /init
          config: s3-secret
          profile: default
          endpoint: https://s3-endpoint

Solution

  • The way I've found out is to execute a mysql client pod, do the login, and import the sql script.

    # 1. deploy mysql-client
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: mysql-init
    data:
      1-init.sql:
        # YOUR SQL INIT SCRIPT HERE
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: mysql-client
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        ports:
        - containerPort: 80
        env:
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "true"
        volumeMounts:
          - name: config-volume
            mountPath: /init/
      volumes:
        - name: config-volume
          configMap:
            # Provide the name of the ConfigMap containing the files you want
            # to add to the container
            name: mysql-init
    
    
    
    # 2. run the import:
    # enter the shell for the client pod and run the following commands by substituting:
    # <MYSQL-SERVER-HOST> with the service name exposing the mysql-cluster on kubernetes cluster and <USER>/<PASSWORD> credentials with the ones defined as kubernetes secret in the mysql-cluster namespace
    
    
    mysql -h <MYSQL-SERVER-HOST> -u<USER> -p<PASSWORD> -e 'SHOW databases;'
    mysql -h <MYSQL-SERVER-HOST> -u<USER> -p<PASSWORD> < init/1-init.sql