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
Related
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 migrate my database from my old server to my new server. I have a very big problem by transferring the database because I have a large database with 5gb. I tried to transfer using c panel transfer but I can't it is not useful. I need a more efficient way to transfer the data.
Can anyone guide me with the full transfer details? How to transfer using import and export or do I need to use any other method?
MySQL type is MyISAM and size is 5gb.
You can try command line if you have access to SSH for both server as command below if not you can try using Navicat application to sync databases
SSH commands
Take mysqldump of database
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
create tar ball of SQL dump file using
tar -zcvf db.tar.gz db.sql
now upload tar.gz file to other server using scp command
scp -Cp db.gz {username}#{server}:{path}
now login on other server using SSH
Untar file using linux cmd
tar -zxvf db.tar.gz
import to database
mysql -u{username} -p {database} < db.sql
Please have a look at syntax though syntax will work but consider this as direction only
Thanks..
For large databases I would suggest to use mysqldump if you have SSH access to the server.
From the manual:
Use mysqldump --help to see what options are available.
The easiest (although not the fastest) way to move a database between two machines is to run the following commands on the machine on which the database is located:
shell> mysqladmin -h 'other_hostname' create db_name
shell> mysqldump db_name | mysql -h 'other_hostname' db_name
If you want to copy a database from a remote machine over a slow network, you can use these commands:
shell> mysqladmin create db_name
shell> mysqldump -h 'other_hostname' --compress db_name | mysql db_name
You can also store the dump in a file, transfer the file to the target machine, and then load the file into the database there. For example, you can dump a database to a compressed file on the source machine like this:
shell> mysqldump --quick db_name | gzip > db_name.gz
Transfer the file containing the database contents to the target machine and run these commands there:
shell> mysqladmin create db_name
shell> gunzip < db_name.gz | mysql db_name
You can also use mysqldump and mysqlimport to transfer the database. For large tables, this is much faster than simply using mysqldump. In the following commands, DUMPDIR represents the full path name of the directory you use to store the output from mysqldump.
First, create the directory for the output files and dump the database:
shell> mkdir DUMPDIR
shell> mysqldump --tab=DUMPDIR db_name
Then transfer the files in the DUMPDIR directory to some corresponding directory on the target machine and load the files into MySQL there:
shell> mysqladmin create db_name # create database
shell> cat DUMPDIR/*.sql | mysql db_name # create tables in database
shell> mysqlimport db_name DUMPDIR/*.txt # load data into tables
Do not forget to copy the mysql database because that is where the grant tables are stored. You might have to run commands as the MySQL root user on the new machine until you have the mysql database in place.
After you import the mysql database on the new machine, execute mysqladmin flush-privileges so that the server reloads the grant table information.
I want to copy my mysql database from my computer to another computer. How can I do this?
How to copy Mysql database from one Computer to another / backup database using mysqldump
We can transfer a MySQL database from one PC to another PC using
mysqldump command.
We have to create dump file of database to transfer database from
one PC to another PC.
MySQL database is not portable database i.e. we cannot transfer it
from one PC to another PC by copying and pasting it.
We can use following method to transfer database.
Creating a dumpfile from database/ Taking backup of MySQL database:
Open command prompt.
Execute following commands to change directory
>c: “press enter”
>cd program files/MySQL/MySQL Server 5.1/ bin “press enter”
>mysqldump -u root -p database_name > database_name.sql “press enter”
Enter password: password of MySQL
Copy sql file and paste it in PC where you want to transfer database.
2. Dumping sql file into database:-
- Open MySQL command line client command prompt.
- Execute following command to create database.
create database database_name;
“press enter” Database name is must as that of your database_name.
Copy that sql file into location “c:/program files/MySQL/MySQL Server 5.1/bin”
*- Now open command prompt and execute following commands.*
>C: “press enter”
>cd program files/MySQL/MySQL Server5.1/bin “press enter”
>mysql –u root –p database_name < database_name.sql “press enter”
Your database is created on PC.
Now in MySQL command prompt check your database.
Another one:1
This best and the easy way is to use a db tools(SQLyog)
http://www.webyog.com/product/downloads
With this tools you can connect the 2 databases servers and just copy one database on server a to server b.
For more info
http://faq.webyog.com/content/12/32/en/mysql-5-objects-are-greyed-out-in-copy-db-to-other-host-dialogue.html
Another one:2
For a database named "lbry", try this:
mysqldump -u root -p lbry > dump-lbry.sql
Create a database of the same name ("lbry" in this example) on the computer to which you wish to copy the database contents
Then import it:
mysql -u root -p lbry < dump-lbry.sql
You can do by this process step-by-step using MySQL WorkBench.
Install MySQL Workbench
Connect to existing Database
Go to Navigator -> Management -> Data Export.
(this will dump queries of tables one by one in a separate folder, Workbench uses the same folder to import)
Create Database on target PC.
Connect to Target Database (would consist of 0 tables in DB)
Go to Navigator -> Management -> Data Import/Restore.
(this will use the dump folder and create tables in your target Database).
Hope this helps.
The only SAFE way to copy databases from one machine to another is to first quiesce the database (make sure no clients are modifying it), then use the mysqldump command to create a text representation of your schema and the contents of your tables. Then copy that text file over to the other machine and read it in by specifying it as the input to the mysql command.
Attempting to copy the actual mysql data directories over is asking for trouble, since they are dependent on the architecture of the machine that mysql is running on and likely on the version of mysql and whatever storage engine is in use.
This tutorial is in Ubuntu but will work on Redhat, Centos, Fedora, Suse
We can dump database, transfer it to another server, and restore it
It will show how to take care of things like modified credentials as a result and moving debain.cnf file
4 dump restore will slow down the serverHow it works
4.1 Run mysqldump on source server:this builds a MySQL executable script for the destination server.
During this time the MySQL server will queue queries
4.2 Copy dump file to the destination server
4.3 Empty destination server
4.4 Execute dump file on the destintion server
Server A(Source Server)
Server B (Destination Server)
Case 1:Server A
root#source$ mysql --defaults-file=/etc/mysql/debain.cnf
mysql>show databases;
mysql>use testdb;(The database to dump)
mysql>show tables;(To Check the tables)
mysql>^c
-- now dump the databses
root#surce$ mysql --defaults-file=/etc/mysql/debain.cnf --all-databses | gzip -c > dump.sql.gz
root#surce$ gzip -dc dump.sql.gz
To copy the files create a ssh key on the source server
root#surce$ ssh-keygen
root#surce$ cat /root/.ssh/id_rsa.pub
select and copy all the ssh key string
root#surce$ scp dump.sql.gz ubuntu#destination:
goto destination server
last step copy the contents of debain.cnf file
root#surce$ cat /etc/mysql/debain.cnf
[client]
host = localhost
user = debain-sys-maint
password = mysecret
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debain-sys-maint
password = mysecret
socket = /var/run/mysqld/mysqld.sock
basedir = /usr
select all and copy this file to detination server.
Note: The sockey path can be different in your machine .use locate command to find the exact path
Case 2. Server B
drop all databses
root#destination$ echo show databases | mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names | awk '{print "drop database "$1";"}'
if this command doesnot drop databses use it with -force option
root#destination$ echo show databases | mysql --defaults-file=/etc/mysql/debian.cnf --skip-column-names | awk '{print "drop database "$1";"}' | mysql --defaults-file=/etc/mysql/debian.cnf -f
copy the ssh key on the destination server
root#destination$ echo "paste the key here" >> /home/ubuntu/.ssh/authorised_keys
goto source Server and use scp command to move the dump on the destination server
(inject the file)
root#destination$ gzip -dc /home/ubuntu/dump.sql.gz | mysql --defaults-file=/etc/mysql/debain.cnf
root#destination$ > /etc/mysql/debain.cnf
root#destination$ nano /etc/mysql/debain.cnf
paste the contents of .cnf file from source server here and save the file
:x
root#destination$ mysql --defaults-file= /etc/mysql/debain.cnf
if you get the mysql prompt then everything should be working file
mysql>
I was able to restore a backup that was shared with me following this thread, specifically #jmail's answer, but, I thought that I could provide a bit more concise answer for future users. I received a dump file with a .sql extension, not a .dump extension as I would have expected.
I tried to place it in my project folder and restore it but I got error 22, referring to access privileges. I moved it to “c:/program files/MySQL/MySQL Server 5.1/bin” and then ran it by:
1) Starting MySQL in the command prompt.
2) Creating the new database that I wanted to restore to
3) Switching to the database
USE new_DB;
4) Running
source c:/program files/MySQL/MySQL Server 5.1/bin/backup.sql
I'm not sure how the backup.sql file was created but this worked for restoring it on my Windows 10 system.
mysqldump --databases dbname -hsource_server_ip -usource_server_userName -psource_server_passcode | mysql
-udest_server_user_name -pdest_server_user_passcode &
There are three general ways to invoke mysqldump:
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
If you do not name any tables following db_name or if you use the --databases or --all-databases option, entire databases are dumped.
mysqldump does not dump the INFORMATION_SCHEMA database by default. MariaDB dumps the INFORMATION_SCHEMA if you name it explicitly on the command line, although currently you must also use the --skip-lock-tables option.
To see a list of the options your version of mysqldump supports, execute mysqldump --help.
I just summarize jmail's answer:
Database to SQL file at computer 1:
mysqldump --user <user name> --password <database> > <output file> for example mysqldump --user root --password movie > movie.sql
SQL file to database at computer 2:
mysql --user <user name> --password <database> < <output file> for example mysql --user root --password movie < movie.sql
Is it possible to dump a database from a remote host through an ssh connection and have the backup file on my local computer.
If so how can this be achieved?
I am assuming it will be some combination of piping output from the ssh to the dump or vice versa but cant figure it out.
This would dump, compress and stream over ssh into your local file
ssh -l user remoteserver "mysqldump -mysqldumpoptions database | gzip -3 -c" > /localpath/localfile.sql.gz
Starting from #MichelFeldheim's solution, I'd use:
$ ssh user#host "mysqldump -u user -p database | gzip -c" | gunzip > db.sql
ssh -f user#server.com -L 3306:server.com:3306 -N
then:
mysqldump -hlocalhost > backup.sql
assuming you also do not have mysql running locally. If you do you can adjust the port to something else.
I have created a script to make it easier to automate mysqldump commands on remote hosts using the answer provided by Michel Feldheim as a starting point:
mysqldump-remote
The script allows you to fetch a database dump from a remote host with or without SSH and optionally using a .env file containing environment variables.
I plan to use the script for automated database backups. Feel free to create issues / contribute - hope this helps others as well!
I'm hoping I can use a shell script that will pull a sql dump down from my production site and into my local database. Ideally, I'd like to be able to run something like this:
sync_site example_com example_local
Where the first argument is the production database and the second is the local database. The remote database is always on the same server, behind SSH, with known MySQL credentials.
Figured it out:
ssh me#myserver.com mysqldump -u user -ppass remote_db | mysql -u user -ppass local_db