简体   繁体   English

将 mysql dump 从本地导入到 SSH

[英]Import mysql dump from local to SSH

I cannot find a solution to this particular demand.我找不到这个特殊需求的解决方案。 I have a mysql dump on my computer and I want to import it in a web server using SSH.我的计算机上有一个 mysql 转储,我想使用 SSH 将它导入到 Web 服务器中。 How do I do that ?我该怎么做? Can I add the ssh connection to the mysql command ?我可以将 ssh 连接添加到 mysql 命令吗?

Edit : I did it with SCP编辑:我用 SCP 做到的

scp -r -p /Users/me/files/dump.sql user@server:/var/www/private
mysql -hxxx -uxxx -pxxx dbname < dump.sql

As the comment above says, the simplest solution is to scp the whole dump file up to your server, and then restore it normally.正如上面的评论所说,最简单的解决方案是将整个转储文件 scp 到您的服务器,然后正常恢复。 But that means you have to have enough free disk space to store the dump file on your webserver.但这意味着您必须有足够的可用磁盘空间来将转储文件存储在您的网络服务器上。 You might not.你可能不会。

An alternative is to set up a temporary ssh tunnel to your web server.另一种方法是设置一个临时的 ssh 隧道到您的 Web 服务器。 Read https://www.howtogeek.com/168145/how-to-use-ssh-tunneling/ for full instructions, but it would look something like this:阅读https://www.howtogeek.com/168145/how-to-use-ssh-tunneling/以获取完整说明,但它看起来像这样:

nohup ssh -L 8001:localhost:3306 -N user@webserver >/dev/null 2>&1 &

This means when I connect to port 8001 on my local host (you can pick any unused port number here), it's really being given a detour through the ssh tunnel to the webserver, where it connects to port 3306, the MySQL default port.这意味着当我连接到本地主机上的端口 8001(您可以在此处选择任何未使用的端口号)时,它实际上是通过 ssh 隧道绕过网络服务器,在那里它连接到端口 3306,即 MySQL 默认端口。

In the example above, your user@webserver is just a placeholder, so you must replace it with your username and your webserver hostname.在上面的示例中,您的user@webserver只是一个占位符,因此您必须将其替换为您的用户名和您的网络服务器主机名。

Then restore your dump file as if you're restoring to a hypothetical MySQL instance running on port 8001 on the local host.然后恢复您的转储文件,就像您要恢复到在本地主机上的端口 8001 上运行的假设 MySQL 实例一样。 This way you don't have to scp the dump file up to your webserver.这样您就不必将转储文件发送到您的网络服务器。 It will be streamed up to the webserver via the ssh tunnel, and then applied to your database directly.它将通过 ssh 隧道传输到网络服务器,然后直接应用于您的数据库。

pv -pert mydumpfile.sql | mysql -h 127.0.0.1 -P 8001

You have to specify 127.0.0.1, because the MySQL client uses "localhost" as a special name for a non-network connection.您必须指定 127.0.0.1,因为 MySQL 客户端使用“localhost”作为非网络连接的特殊名称。

I like to use pv to read the dumpfile, because it outputs a progress bar.我喜欢用pv来读取转储文件,因为它输出一个进度条。

You can try this solution for your problem :您可以针对您的问题尝试此解决方案:

Login using SSH details :-使用 SSH 详细信息登录:-

SSH Host name : test.com
SSH User : root
SSH Password : 123456

Connect SSH :-连接 SSH :-

ssh root@test.com
enter password : 123456

Login MySQL :-登录 MySQL :-

mysql -u [MySQL User] -p
Enter Password :- MySQL Password

Used following command for Import databases :-使用以下命令导入数据库:-

show databases; // List of Databased
use databasedname; // Enter You databased name to Import databased
source path;  // Set path for Import databased for ex : /home/databased/import.sql 

I hope this will helps you.我希望这会帮助你。

Yes, you can do it with one command, just use 'Pipeline' or 'Process Substitution'是的,您可以使用一个命令来完成,只需使用“管道”或“流程替换”

For your example with 'Pipeline':对于您的“管道”示例:

ssh user@server "cat /Users/me/files/dump.sql" | mysql -hxxx -uxxx -pxxx dbname

or use 'Process Substitution':或使用“过程替换”:

mysql -hxxx -uxxx -pxxx dbname < <(ssh user@server "cat /Users/me/files/dump.sql")

Example 2, get database dump from remote server1 and restore on remote server2 with 'Pipeline':示例 2,从远程 server1 获取数据库转储并使用“Pipeline”在远程 server2 上还原:

ssh user@server1 "mysqldump -uroot -p'xxx' dbname" | ssh user@server2 "mysql -uroot -p'xxx' dbname"

or 'Process Substitution':或“过程替换”:

ssh user@server2 "mysql -uroot -p'xxx' dbname" < <(ssh user@server1 "mysqldump -uroot -p'xxx' dbname")

Additional links:附加链接:

what is 'Process Substitution':什么是“过程替换”:

http://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html http://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html

what is 'Pipeline':什么是“管道”:

http://www.gnu.org/software/bash/manual/html_node/Pipelines.html http://www.gnu.org/software/bash/manual/html_node/Pipelines.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM