Automate mysqldump to local Windows computer

℡╲_俬逩灬. 提交于 2019-12-22 12:37:11

问题


I'm trying to use plink on Windows to create a tunnel to a Linux machine and have the dump file end up on the Windows machine. It would appear that this answer would work and is the basis of my question. But trying it out and looking at other answers I find that the dump file is still on the Linux machine. I'm trying this out in my local environment with Windows and Ubuntu 14.04 before moving to production. In Windows 8.1:

 plink sam@192.168.0.20 -L 3310:localhost:3306

 mysqldump --port=3310 -h localhost -u sam -p --all-databases > outfile.sql

I've tried swapping localhost in the second with 127.0.0.1, adding -N to the tail of the tunnel setup, using one table in the dump command but despite my tunnel, it's as if the first command is ignored. Other answers indicate to add more commands to the script so that I can use pscp to copy the file. That also means to re-connect to trash this outfile.sql. Not ideal for getting other dumps on other servers. If that's the case, why use the first command at all?

What am I overlooking? In plink, the output of the first is to open up the Linux server where I can run the mysqldump command. But there seems to be ignoring the first command. What do you think?


回答1:


You have several options:

  • Dump the database remotely to a remote file and download it to your machine afterwards:

    plink sam@192.168.0.20 "mysqldump -u sam -p --all-databases > outfile.sql"
    pscp sam@192.168.0.20:outfile.sql .
    

    The redirect > is inside the quotes, so you are redirecting mysqldump on the remote machine to the remote file.

    This is probably the easiest solution. If you compress the dump before downloading, it would be even the fastest, particularly if you connect over a slow network.

  • Execute mysqldump remotely, but redirect its output locally:

    plink sam@192.168.0.20 "mysqldump -u sam -p --all-databases" > outfile.sql
    

    Note that the redirect > is outside of the quotes, comparing to the previous case, so you are redirecting an output of plink, i.e. output of the remote shell, which contains output of a remote mysqldump.

  • Tunnel connection to the remote MySQL database and dump the database locally using a local installation of MySQL (mysqldump):

    plink sam@192.168.0.20 -L 3310:localhost:3306
    

    In a separate local console (cmd.exe):

    mysqldump --port=3310 -h localhost -u sam -p --all-databases > outfile.sql
    

    In this case nothing is running remotely (except for a tunnel end).



来源:https://stackoverflow.com/questions/28461841/automate-mysqldump-to-local-windows-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!