Extracting a remote MySQL Database without MyODBC? - 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

Related

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

How to transfer my Dump(.sql) file on server?

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.

MYSQL Restoring large DB into Remote Server

Hi All,
I am trying to restore nearly 8GB DB into remote server using mysql command in command prompt. It is been 8 hours since i started the process. But it still restores the DB. I tried with the command
> mysql -h hostname -u username -p dbname < location of the dump file
My questions are,
Does it take these much hours time to restore these amount of DB?
Is it possible to restore 8GB database?
Am i doing in correct way?
Is there any other better way to restore the DB?
In my opinion the answer of #Ferri is good, in cases like this the CLI is always the best option.
The only improvement that I suggest is to use gzip to reduce the weight of the script.
Dump the db like so:
mysqldump --host yourhost -u root --port 3306 -p yourdb | gzip -9 > yourdb.sql.gz
Restore the db like so:
gzip -cd yourdb.sql.gz | mysql -h yourhost -u root -p yourdb
Command
mysql -h IP -u Username -p schema < file
Example
mysql -h 192.168.10.122 -u root -p mydatabase < /tmp/20160628_test_minificated.sql
Does it take these much hours time to restore these amount of DB?
Depends of size of dumpfile and connection speed.
Is it possible to restore 8GB database?
Yes, by this way you can restore big databases.
Am i doing in correct way?
For me this is the best way when you are working from command line interface and destination also is a command line interface.
Is there any other better way to restore the DB?
Yes you have multiple options, like phpmyadmin, workbrench, heidisql and many others but each one have their own limitations.

Is there a way to copy all the data in a mysql database to another? (phpmyadmin)

I want to copy all the tables, fields, and data from my local server mysql to my hosting sites mysql. Is there a way to copy all the data? (It's only 26kb, very small)
In phpMyAdmin, just export a dump (using the export) tab and re-import it on the other server using the sql tab.
Make sure you compare the results, I have had phpMyAdmin screw up the import more than once.
If you have shell access to both servers, a combination of
mysqldump -u username -p databasename > dump.sql
and a
mysql -u username -p databasename < dump.sql
on the target server is the much more fast and reliable alternative in my experience.
Have a look at
Copying MySQL Databases to Another Machine
Copy MySQL database from one server to another remote server
Please follow the following steps:
Create the target database using MySQLAdmin or your preferred method. In this example, db2 is the target database, where the source database db1 will be copied.
Execute the following statement on a command line:
mysqldump -h [server] -u [user] -p[password] db1 | mysql -h [server]
-u [user] -p[password] db2
Note: There is NO space between -p and [password]
I copied this from Copy/duplicate database without using mysqldump.
It works fine. Please ensure that you are not inside mysql while running this command.
If you have the same version of mysql on both systems (or versions with compatible db file sytsem), you may just copy the data files directly. Usually files are kept in /var/lib/mysql/ on unix systems.

mysql import sql via cli from remote server

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.