How do I restore a dump file from mysqldump using

2019-08-25 11:25发布

问题:

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.

回答1:

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.



回答2:

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.