mysql-operator icon indicating copy to clipboard operation
mysql-operator copied to clipboard

Create initial DB and user

Open jan-g opened this issue 8 years ago • 1 comments

At the moment I'm using a helm chart to make a job that initialises the cluster once it's up. This feels like a bit of a hack:

apiVersion: batch/v1
kind: Job
metadata:
  name: example-mysql-init
spec:
  template:
    metadata:
      name: example-mysql-init
      labels:
        role: mysql-init
    spec:
      containers:
      - name: init
        image: mysql
        command: 
        - "bash"
        - "-c"
        - |
          set -ex
          mysqladmin -h {{ .Release.Name }}-mysql --user=root --password=$DB_ROOT_PASSWD ping
          mysql -h {{ .Release.Name }}-mysql --user=root --password=$DB_ROOT_PASSWD --batch --skip-column-names <<-EOF
          	create database if not exists $DB_DATABASE;
          	grant all privileges on $DB_DATABASE.* to '$DB_USER'@'%' identified by '$DB_PASSWD';
          	flush privileges;
          	EOF
        env:
          - name: DB_PASSWD
            valueFrom:
              secretKeyRef:
                name: cluster-mysql
                key: mysql-password
          - name: DB_ROOT_PASSWD
            valueFrom:
              secretKeyRef:
                name: cluster-mysql-root-password
                key: password
          - name: DB_HOST
            value: cluster-mysql
          - name: DB_DATABASE
            value: my_db_name
          - name: DB_USER
            value: my_db_user
      restartPolicy: Never
  # the default of 6 might not be enough
  backoffLimit: 600

jan-g avatar Jan 12 '18 11:01 jan-g

Thanks @jan-g this feels like a useful feature and a problem that will likely come up often. We'll investigate some ideas around this one. 👍

owainlewis avatar Jan 12 '18 12:01 owainlewis