How do I restore a dump file from mysqldump using kubernetes?

6/23/2017

I know how to restore a dump file from mysqldump. Now, I am attempting to do that using kubernetes and a docker container. The database files are in persistent (nfs) mount. The docker cannot be accessed outside of the cluster as there is no need for anything external to touch it.

I tried:

kubectl run -i -t dbtest --image=mariadb --restart=Never --rm=true --command -- mysql -uroot -ps3kr37 < dump.sql

and

kubectl exec mariadb-deployment-3614069618-mn524 -i -t -- mysql -u root -p=s3kr37 < dump.sql

But neither commands worked -- errors about TTY, sockets, and other things hinting that I am missing something vital here.

What am I not understanding here?

I could just stop the deployment, scp the database files, and restart the container and hope for the best. However, what can go right?


The question Install an sql dump file to a docker container with mariaDB sure looks like a duplicate but is not: first, I am on Linux not Windows and more importantly the answers all are about initialising with a dump. I want to be able to trash the data and revert to the dump data. This is a test system that will eventually be the "live" so I need to restore from many potential dumps.

-- Sardathrion - against SE abuse
docker
kubernetes
mariadb
mysql

3 Answers

1/6/2019

This should work:

kubectl  --kubeconfig=k8s-XXXXXXX-kubeconfig.yaml exec -i ddevdb-XXXXX -- mysql -u root -h mysqlservice -proot drupal < you-dump.sql

kubeconfig is optional, digitalocean for examples provides that so you can run your commands from your local.

To see if everything looks good:

kubectl --kubeconfig=k8s-XXXXXXX-kubeconfig.yaml run -it --rm --image=mariadb:10.4 --restart=Never mysql -- mysql -h mysqlservice -proot

After which you'll have a terminal inside mysql.

-- Alejandro Moreno
Source: StackOverflow

11/2/2019

As described in here you can use the following command to restore a DB on kubernetes pod from a dump in your machine

$ kubectl exec -it {{podName}} -n {{namespace}} -- mysql -u {{dbUser}} -p{{password}} {{DatabaseName}} < <scriptName>.sql
Example :
$ kubectl exec -it mysql-58 -n sql -- mysql -u root -proot USERS < dump_all.sql
-- Madusha
Source: StackOverflow

6/27/2017

What I did was this:

  • Create an NFS mount with two sub0drectories: mysql and initd.
  • In initd, I added several ,sql files, including the dump.
  • Mount initd as /docker-entrypoint-initdb.d in the deployment.This causes all the files to be read at initialisation time provided that it is the first time we run.
  • The mysql directory is mounted as /var/lib/mysql and contains all the mariaDB files.

If I need to revert, I trash all the contents of the mysql directory and re-create the deployment.

-- Sardathrion - against SE abuse
Source: StackOverflow