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"
Related
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 have created a bash script for the automation of my database restore. When I run the following commands, I get /my/sql/file/path.sql: No such file or directory.
ssh $USER#"$cloneMysqlHost" gunzip /path/file.sql.gz && MySQL -u root -p db_name < /path/file.sql
I did an ls -lrot on the host I ssh to, just to make sure the file exists the permissions are correct, and they are.
Any ideas what I'm doing wrong?
Thanks in advance!
The && is causing the local shell to split the command and run the MySQL command locally.
The < redirection is also being done locally (and the cause of your error).
The gunzip is being performed on the remote host though.
You need to quote the entire argument to ssh if you want it all run on the remote system.
ssh "$USER#$cloneMysqlHost" 'gunzip /path/file.sql.gz && MySQL -u root -p db_name < /path/file.sql'
Are you providing the password properly? Also, not sure what's going on with the &&, should use a pipe there. MySql is probably not valid, use mysql. See here for more details.
ssh $USER#"$cloneMysqlHost" gunzip /path/file.sql.gz | mysql -u root -p [password] db_name
I have created a dump file for my database and now I want to put that
file on my server to get its access. I will copy that .sql file on my
server, but I don't know how to access that dump file using commands.
Assuming you are using command line
cd /path/where/sql/file/is
And then
mysql -u username -p database_name < file.sql
If you don't have the mysql bin in your PATH you might want to run
/path/to/mysql/bin/mysql -u username -p database_name < file.sql
Or temporarily put the file.sql in the mysql bin directory (not recommended)
Another possible scenario is to point to the file in the filesystem like this
mysql -u username -p database_name < /path/where/sql/file/is/file.sql
If you're on windows you might wanna change the forward slashes to backslashes.
If you want to restore MySQL database on remote-server, then very first thing is that, the .sql file is not necessarily present on server.
We can restore the backup right from our development machine(using mysql.exe of local machine), provided that you have MySQL user having MySQL Server remote access.
Use "-h" option while restoring the backup. "-h" implies hostname where MySQL Database Server is present. It can be in local network or on remote server.
mysql -h www.yourserver.com -u username -p database_name < file.sql
Or with IP Address (use actual IP Address of your Database Server)
mysql -h 112.112.112.112 -u username -p database_name < file.sql
And if you do not have remote-access enabled user, then you need to upload .sql file to remote-server and restore it by putting host-name as localhost. Like,
mysql -h localhost -u username -p database_name < file.sql
Hope it helps, thanks.
I'm currently trying to make a copy of a site to run locally, and i'm having some difficulty in downloading a dump of the mySQL database using PhpMyAdmin. (In that it doesn't work). I was wondering (and I suspect) if there is an sql command I can execute that would dump out the database to a location that I can download it via FTP.
I've found the following:
mysqldump --opt --user=root --password password > /home/backup/db.sql
but the page says that this will only run via shell, and I don't have shel access to the server. Can anyone help?
mysqldump -u root -p -T/path/ database
Simple Three steps Export mysql DB
**step1 :** run windows command prompt
**step2 :** make where yours wamp mysql stored directory ex E:\wamp\bin\mysql\mysql5.5.24\bin
**step 3 :**
E:\wamp\bin\mysql\mysql5.5.24\bin> mysqldump -u UserName -p PassWord dbname >c:\dbname.sql
PERFORM MySQL DATABASE DUMP:
Having registered mysql.exe in the Paths of Environment Variables (On
Windows Os)
Open windows console
Type the following Query:
mysqldump [Your database Name] -u root -p > C:/[Your Destination Directory ]/[ new Backup Database Name.sql ]
You will be prompted for a database user password
Enter Password:*******
when the dump is over...the directory will go back to the current
user session in Windows console
For this to work well, replace all parameters in square braces "[ ]" with your own values without the square braces
Open the Command prompt from MySQL bin folder (Make sure you have Read/Write Access)
Use the below mysqldump command
mysqldump -h [hostname] -u [username] -p [your database] > [your destination path][your filename.extn]
Ex: mysqldump -h localhost -u root -p test_DB > D:\MySQL\Dumps\test_file.sql
***WARNING : In case, if your custom path has any spaces in between, the above query will throw an error.
Invalid Path Description
To avoid Invalid Path Description, Give your path in double-quotes.
Ex : mysqldump -h localhost -u root -p test_DB > D:\MySQL\"Dumps from ABC"\test_file.sql
What are you looking for is Sypex Dumper 2. It exports (backups) your database directly to disk, so you can download the backup via ftp. It is written on php, supports large databases and has a very nice interface.
You can go to the directory where you want store the file, then execute de mysqldump command.
~/Desktop$ sudo /Applications/XAMPP/xamppfiles/bin/mysqldump -u root -p publicaciones > publicaciones.sql
PD. Use sudo to grant full permissions.
PD. If you have your PATH setup, don`t need use the full route to your MySql commands.
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.