简体   繁体   中英

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. How do I do that ? Can I add the ssh connection to the mysql command ?

Edit : I did it with 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. 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. Read https://www.howtogeek.com/168145/how-to-use-ssh-tunneling/ for full instructions, but it would look something like this:

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.

In the example above, your user@webserver is just a placeholder, so you must replace it with your username and your webserver hostname.

Then restore your dump file as if you're restoring to a hypothetical MySQL instance running on port 8001 on the local host. 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.

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.

I like to use pv to read the dumpfile, because it outputs a progress bar.

You can try this solution for your problem :

Login using SSH details :-

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

Connect SSH :-

ssh root@test.com
enter password : 123456

Login 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':

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

what is 'Pipeline':

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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