Homen
Homen

Reputation: 1212

Copying mysql databases from one computer to another

I want to copy my mysql database from my computer to another computer. How can I do this?

Upvotes: 25

Views: 116704

Answers (7)

Cloud Cho
Cloud Cho

Reputation: 1774

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

Upvotes: 0

jmail
jmail

Reputation: 6134

How to copy Mysql database from one Computer to another / backup database using mysqldump

  1. We can transfer a MySQL database from one PC to another PC using mysqldump command.

  2. We have to create dump file of database to transfer database from one PC to another PC.

  3. MySQL database is not portable database i.e. we cannot transfer it from one PC to another PC by copying and pasting it.

  4. We can use following method to transfer database.

  5. Creating a dumpfile from database/ Taking backup of MySQL database:

  6. Open command prompt.

  7. 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

look here

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

Upvotes: 29

Rajat Verma
Rajat Verma

Reputation: 2590

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.

Upvotes: 0

Casivio
Casivio

Reputation: 363

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.

Upvotes: 0

Abhilab-Systematix
Abhilab-Systematix

Reputation: 181

You can do by this process step-by-step using MySQL WorkBench.

  1. Install MySQL Workbench
  2. Connect to existing Database
  3. 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)
  4. Create Database on target PC.
  5. Connect to Target Database (would consist of 0 tables in DB)
  6. Go to Navigator -> Management -> Data Import/Restore. (this will use the dump folder and create tables in your target Database).

Hope this helps.

Upvotes: 18

amarjit singh
amarjit singh

Reputation: 461

  1. This tutorial is in Ubuntu but will work on Redhat, Centos, Fedora, Suse
  2. We can dump database, transfer it to another server, and restore it
  3. 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>

Upvotes: 3

cvkline
cvkline

Reputation: 454

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.

Upvotes: 4

Related Questions