简体   繁体   中英

Running mysql commands with Kubectl

I have mariadb-gallera deployed on my Kubernetes cluster and once the pod is deployed and it is up and running I want to get into the pod and login to mysql as root and after that create a database if it is not present over there.

I tried to do so but it fails with errors:

 name: "Run command on every Pod labelled app=glaera"
 command: >
   kubectl exec -i "{{ item }}" --namespace backend -- mysql -u'root' -p'YWRtaW4xMjM=' -- 'CREATE DATABASE IF NOT EXISTS aves';
 with_items: "{{ pod_names }}"

Please help me with a solution.

Error:

failed: [localhost] (item=galera-mariadb-galera-0) => {"ansible_loop_var": "item", "changed": true, "cmd": ["kubectl", "exec", "-i", "galera-mariadb-galera-0", "--namespace", "backend", "--", "mysql", "-uroot", "-pYWRtaW4xMjM=", "--", "CREATE DATABASE IF NOT EXISTS aves;"], "delta": "0:00:00.190892", "end": "2021-02-02 11:57:37.806727", "item": "galera-mariadb-galera-0", "msg": "non-zero return code", "rc": 1, "start": "2021-02-02 11:57:37.615835", "stderr": "ERROR 1049 (42000): Unknown database 'CREATE DATABASE IF NOT EXISTS aves;'\ncommand terminated with exit code 1", "stderr_lines": ["ERROR 1049 (42000): Unknown database 'CREATE DATABASE IF NOT EXISTS aves;'", "command terminated with exit code 1"], "stdout": "", "stdout_lines": []}

You have a typo in your command -

    - name: "Run command on every Pod labelled app=glaera"
      command: >
        kubectl exec -i "{{ item }}" --namespace backend -- mysql -u'root' -p'YWRtaW4xMjM=' -e 'CREATE DATABASE IF NOT EXISTS aves';
      with_items: "{{ pod_names }}"

Notice that before 'CREATE there should be -e .

Also, please use k8s_exec module instead of the command module.

For example,

    - community.kubernetes.k8s_exec:
        pod: mysql-deployment-5b68bb45bc-n98lp
        namespace: default
        command: mysql --defaults-file=client.conf -h 127.0.0.1 studentdb -e 'CREATE DATABASE IF NOT EXISTS aves'

You have a typo in your command -

    - name: "Run command on every Pod labelled app=glaera"
      command: >
        kubectl exec -i "{{ item }}" --namespace backend -- mysql -u'root' -p'YWRtaW4xMjM=' -e 'CREATE DATABASE IF NOT EXISTS aves';
      with_items: "{{ pod_names }}"

Notice that before 'CREATE there should be -e .

Also, please use k8s_exec module instead of the command module.

For example,

    - community.kubernetes.k8s_exec:
        pod: mysql-deployment-5b68bb45bc-n98lp
        namespace: default
        command: mysql --defaults-file=client.conf -h 127.0.0.1 -e 'CREATE DATABASE IF NOT EXISTS aves'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM