Linux 来自远程主机的 mysqldump

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13594895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:54:12  来源:igfitidea点击:

mysqldump from remote host

mysqllinuxssh

提问by Marty Wallace

Is it possible to dump a database from a remote host through an ssh connection and have the backup file on my local computer.

是否可以通过 ssh 连接从远程主机转储数据库并将备份文件保存在我的本地计算机上。

If so how can this be achieved?

如果是这样,如何实现?

I am assuming it will be some combination of piping output from the ssh to the dump or vice versa but cant figure it out.

我假设它将是从 ssh 到转储的管道输出的某种组合,反之亦然,但无法弄清楚。

采纳答案by Michel Feldheim

This would dump, compress and stream over ssh into your local file

这将通过 ssh 转储、压缩和流式传输到您的本地文件中

ssh -l user remoteserver "mysqldump -mysqldumpoptions database | gzip -3 -c" > /localpath/localfile.sql.gz 

回答by Michael Irey

ssh -f [email protected] -L 3306:server.com:3306 -N

ssh -f [email protected] -L 3306:server.com:3306 -N

then:

然后:

mysqldump -hlocalhost > backup.sql

mysqldump -hlocalhost > backup.sql

assuming you also do not have mysql running locally. If you do you can adjust the port to something else.

假设您也没有在本地运行 mysql。如果这样做,您可以将端口调整为其他内容。

回答by Pierre de LESPINAY

Starting from @MichelFeldheim's solution, I'd use:

从@MichelFeldheim 的解决方案开始,我会使用:

$ ssh user@host "mysqldump -u user -p database | gzip -c" | gunzip > db.sql

回答by Logan

I have created a scriptto make it easier to automate mysqldumpcommands on remote hosts using the answer provided by Michel Feldheimas a starting point:

我创建了一个脚本,mysqldump使用Michel Feldheim提供的答案作为起点,更轻松地在远程主机上自动执行命令:

mysqldump-remote

mysqldump-远程

The script allows you to fetch a database dump from a remote host with or without SSHand optionally using a .envfile containing environment variables.

该脚本允许您使用或不SSH使用.env包含环境变量的文件从远程主机获取数据库转储。

I plan to use the script for automated database backups. Feel free to create issues/ contribute- hope this helps others as well!

我计划将该脚本用于自动数据库备份。随意创建问题/贡献- 希望这也能帮助其他人!