i know how to import an sql file via the cli:
mysql -u USER -p DBNAME < dump.sql
but that's if the dump.sql file is local. how could i use a file on a remote server?
You didn't say what network access you have to the remote server.
Assuming you have SSH access to the remote server, you could pipe the results of a remote mysqldump to the mysql command. I just tested this, and it works fine:
ssh remote.com "mysqldump remotedb" | mysql localdb
I put stuff like user, password, host into .my.cnf so I'm not constantly typing them -- annoying and bad for security on multiuser systems, you are putting passwords in cleartext into your bash_history! But you can easily add the -u -p -h stuff back in on both ends if you need it:
ssh remote.com "mysqldump -u remoteuser -p'remotepass' remotedb" | mysql -u localuser -p'localpass' localdb
Finally, you can pipe through gzip to compress the data over the network:
ssh remote.com "mysqldump remotedb | gzip" | gzip -d | mysql localdb
Just thought I'd add to this as I was seriously low on space on my local VM, but if the .sql file exists already on the remote server you could do;
ssh <ip-address> "cat /path/to/db.sql" | mysql -u <user> -p<password> <dbname>
I'd use wget to either download it to a file or pipe it in.
Related
I want to copy a database from one server to another (Germany and China), since they are both hosted servers by two different service providers I can't just replicate them because they won't allow me to change the configfiles of the servers. I came to the conclusion that I need to setup a 3rd Server at my location from which I then can copy the data towards the other server so I have a "Master" in Germany a "Slave" in China and a "Messenger" at my location. All the Commands must be executed on the "messenger" by a bash script. The script works fine until the file should be sent into the database there it gives me the error that the file doesn't exist, but it exists.
mysqldump -h [host] -u [user] -p[mysqlpassword] databasename > filename.sql
sshpass -p [mypassword] ssh [ChineseServerIp] -l [user] sshpass -p [mypassword] scp user#GermanServerIP:filename.sql /home
sshpass -p [mypassword] ssh [ChineseServerIP] -l [user] mysql -u [mysqlUser] -p[mysqlpassword] Databese < /home/filename.sql
I can't just copy the file from the "messenger" Server into the Chinese one, it just would take to long because of the "great Chinese firewall" (I gzip the .sql file and then Transfer it when its on the Chinese one I unzip and upload it).
its solved, I just had to set the inserting of the sql file in ""
sshpass -p [mypassword] ssh [ChineseServerIP] -l [user] "mysql -u [mysqlUser] -p[mysqlpassword] Databese < /home/filename.sql"
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
I'm looking for downloading my sql database from a remote server to my local machine.
I used scp command like that :
scp -r user#ip:/var/lib/mysql/mydatabase .
It works, but I have lot of files (related to tables in my DB I suppose) : db.opt, files in .frm, .MYD, and .MYI
I tryed to copy my files to a new database folder in MAMP (to test the db), but when I watch my db in PhpMyAdmin I can see only one table :/
Is there a better way to download directly a sql database from server to local with terminal ?
Thanks a lot for your help guys
I suggest you use command, mysqldump --all-databases.
Over ssh work like this:
ssh user#ip mysqldump --all-databases > dump.sql
Download the dump with scp, and restore it whit php-myadmin.
simply use the mysqldump command over ssh, and redirect the output, so, you will have a dump of the DB
something like
ssh user#ip 'mysqldump -u dbuser -pdbassword database_name' > database_dump.sql
The you can restore the database using mysql on your local machine:
mysql -uusername -ppassword db_name < database_dump.sql
If the database is big, you can pipe a gzip like below:
ssh user#ip 'mysqldump -u dbuser -pdbassword database_name | gzip ' > database_dump.sql.gz
to restore on your local machine
gzip -dc database_dump.sql.gz | mysql -uusername -ppassword
I need to copy an entire database from a mysql installation on a remote machine via SSH to my local machines mysql.
I know the SSH and both local and remote MYSQL admin user and password.
Is this enough information, and how is it done?
From remote server to local machine
ssh {ssh.user}#{remote_host} \
'mysqldump -u {remote_dbuser} --password={remote_dbpassword}
{remote_dbname} | bzip2 -c' \ | bunzip2 -dc | mysql -u {local_dbuser}
--password={local_dbpassword} -D {local_dbname}
That will dump remote DB in your local MySQL via pipes :
ssh mysql-server "mysqldump --all-databases --quote-names --opt --hex-blob --add-drop-database" | mysql
You should take care about users in mysql.users
Moreover, to avoid typing users and passwords for mysqldump and mysql on local and remote hosts, you can create a file ~/.my.cnf :
[mysql]
user = dba
password = foobar
[mysqldump]
user = dba
password = foobar
See http://dev.mysql.com/doc/refman/5.1/en/option-files.html
Try reading here:
Modified from http://www.cyberciti.biz/tips/howto-copy-mysql-database-remote-server.html - modified because I prefer to use .sql as the extension for SQL files:
Usually you run mysqldump to create a database copy and backups as
follows:
$ mysqldump -u user -p db-name > db-name.sql
Copy db-name.out file using sftp/ssh to remote MySQL server:
$ scp db-name.sql user#remote.box.com:/backup
Restore database at remote server (login over ssh):
$ mysql -u user -p db-name < db-name.sql
Basically you'll use mysqldump to generate a dump of your database, copy it to your local machine, then pipe the contents into mysql to regenerate the DB.
You can copy the DB files themselves, rather than using mysqldump, but only if you can shutdown the MySQL service on the remote machine.
I would recommend the Xtrabackup tool by Percona. It has support for hot copying data via SSH and has excelent documentation. Unlike using mysqldump, this will copy all elements of the MySQL instance including user permissions, triggers, replication, etc...
ssh into the remote machine
make a backup of the database using mysqldump
transfer the file to local machine using scp
restore the database to your local mysql
Is there a way to get a MySQL Dump of a database that is not stored locally. I have the connection string to the database located on another server, but it doesn't seem like MySQLDump wants to do anything if the server is remote.
MySQLDump has a -h parameter to connect to a remote host.
First try the mysql client application:
mysql -h your.server.com -uYourUser -pYourPass
If that works, use the same format for MySQLDump
mysqldump -h your.server.com -uYourUser -pYourPass --all-databases
Edit for ajreal:
By default, mysqld (the MySQL server) will run on 3306, and mysql (the client application) will connect using that port. However, if you changed your configuration, update your command accordingly. For example for port 3307, use
mysql -h your.server.com -P 3307 -uYourUser -pYourPass
Check your MySQL configfile to see how you can connect to your MySQL server.
Here is example, how to extract mysql db named 'abc123' direct to zip, w/o super big text dump file on disk.
mysqldump -u root --opt --databases abc123 | gzip > /tmp/abc123.export.sql.gz