Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iamcryptoki/f65cea21280c19bf016456acefb675dc to your computer and use it in GitHub Desktop.
Save iamcryptoki/f65cea21280c19bf016456acefb675dc to your computer and use it in GitHub Desktop.
Export MySQL database from Kubernetes pod.
# Export dump particular database.
$ kubectl exec {{podName}} -n {{namespace}} -- mysqldump -u {{dbUser}} -p{{password}} {{DatabaseName}} > <scriptName>.sql
# Export dump all databases.
$ kubectl exec {{podName}} -n {{namespace}} -- mysqldump -u {{dbUser}} -p{{password}} --all-databases > <scriptName>.sql
# Restore a database from a dump.
$ kubectl exec -it {{podName}} -n {{namespace}} -- mysql -u {{dbUser}} -p{{password}} {{DatabaseName}} < <scriptName>.sql
@margach
Copy link

margach commented May 20, 2020

When I was trying to export, I ran into a permission denied issue and this is how I worked around it, hope this helps someone:

  1. Login into the pod
    kubectl exec -it <pod> -n <namespace> -- /bin/bash

  2. run mysqldump from within the pod and use tmp to write the file
    mysqldump <-u user> -p <db> > /tmp/file.sql

  3. Copy the file from the pod
    kubectl cp <namespace>/<pod>:/tmp/file.sql file.sql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment