Copying mysql databases from one computer to another - mysql

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

Related

how to migrate a large database to new server

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.

Transfering mysql data from wamp enviroment to centOS

I have copy of .sql file that contains large data. I saved it from phpmyadmin while I was using WAMP for development. Now I am working with CentOS, and I have transferred the data to my VirtualBox running CentOs already.
So, the problem is not about transferring the file but running the .sql file using shell, so the data can be transferred to the new mysql server.
Does anyone know any commands?
Initially I thought moving this entire directory:
C:\wamp\bin\mysql\mysql5.5.24\data
To my new server environment would be a good idea, but I can't seem to find where the data folder is kepyt in centos-mysql.
whereis mysql gives mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
I have checked all this folders to find the data folder but to no avail.
if you created the .sql file with mysqldump or if it is otherwise a legal mysql script containing sql commands you can simply pipe this contents to your centOs mysql instance:
$ mysql -uroot -p dbname < dump.sql
where dbname is the name of your database and dump.sql your .sql file.
Follow these steps:
1- In your Windows Environment, from command line(CMD), go to the folder:
cd "C:\wamp\bin\mysql\mysql5.5.24\bin
2- Run: mysqldump -uroot -pYourPassword DataBaseName > myBackup.sql
3- On your centOs machine, open a terminal:
mysql -uroot -p
4- In mysql console:
create database DataBaseName;
exit;
5- Transfer the myBackup from your Windows System, to centOS, open a terminal in
the same directory where myBackup.sql lives:
mysql -uroot -p DataBaseName < myBackup.sql

retrieve a mysql database via SSH

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

How do I restore a dump file from mysqldump?

I was given a MySQL database file that I need to restore as a database on my Windows Server 2008 machine.
I tried using MySQL Administrator, but I got the following error:
The selected file was generated by
mysqldump and cannot be restored by
this application.
How do I get this working?
If the database you want to restore doesn't already exist, you need to create it first.
On the command-line, if you're in the same directory that contains the dumped file, use these commands (with appropriate substitutions):
C:\> mysql -u root -p
mysql> create database mydb;
mysql> use mydb;
mysql> source db_backup.dump;
It should be as simple as running this:
mysql -u <user> -p < db_backup.dump
If the dump is of a single database you may have to add a line at the top of the file:
USE <database-name-here>;
If it was a dump of many databases, the use statements are already in there.
To run these commands, open up a command prompt (in Windows) and cd to the directory where the mysql.exe executable is (you may have to look around a bit for it, it'll depend on how you installed mysql, i.e. standalone or as part of a package like WAMP). Once you're in that directory, you should be able to just type the command as I have it above.
You simply need to run this:
mysql -p -u[user] [database] < db_backup.dump
If the dump contains multiple databases you should omit the database name:
mysql -p -u[user] < db_backup.dump
To run these commands, open up a command prompt (in Windows) and cd to the directory where the mysql.exe executable is (you may have to look around a bit for it, it'll depend on how you installed mysql, i.e. standalone or as part of a package like WAMP). Once you're in that directory, you should be able to just type the command.
mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
look here - step 3: this way you dont need the USE statement
When we make a dump file with mysqldump, what it contains is a big SQL script for recreating the databse contents. So we restore it by using starting up MySQL’s command-line client:
mysql -uroot -p
(where root is our admin user name for MySQL), and once connected to the database we need commands to create the database and read the file in to it:
create database new_db;
use new_db;
\. dumpfile.sql
Details will vary according to which options were used when creating the dump file.
Run the command to enter into the DB
# mysql -u root -p
Enter the password for the user Then Create a New DB
mysql> create database MynewDB;
mysql> exit
And make exit.Afetr that.Run this Command
# mysql -u root -p MynewDB < MynewDB.sql
Then enter into the db and type
mysql> show databases;
mysql> use MynewDB;
mysql> show tables;
mysql> exit
Thats it ........ Your dump will be restored from one DB to another DB
Or else there is an Alternate way for dump restore
# mysql -u root -p
Then enter into the db and type
mysql> create database MynewDB;
mysql> show databases;
mysql> use MynewDB;
mysql> source MynewDB.sql;
mysql> show tables;
mysql> exit
If you want to view the progress of the dump try this:
pv -i 1 -p -t -e /path/to/sql/dump | mysql -u USERNAME -p DATABASE_NAME
You'll of course need 'pv' installed. This command works only on *nix.
I got it to work following these steps…
Open MySQL Administrator and connect to server
Select "Catalogs" on the left
Right click in the lower-left box and choose "Create New Schema"
MySQL Administrator http://img204.imageshack.us/img204/7528/adminsx9.th.gif enlarge image
Name the new schema (example: "dbn")
MySQL New Schema http://img262.imageshack.us/img262/4374/newwa4.th.gif enlarge image
Open Windows Command Prompt (cmd)
Windows Command Prompt http://img206.imageshack.us/img206/941/startef7.th.gif enlarge image
Change directory to MySQL installation folder
Execute command:
mysql -u root -p dbn < C:\dbn_20080912.dump
…where "root" is the name of the user, "dbn" is the database name, and "C:\dbn_20080912.dump" is the path/filename of the mysqldump .dump file
MySQL dump restore command line http://img388.imageshack.us/img388/2489/cmdjx0.th.gif enlarge image
Enjoy!
As a specific example of a previous answer:
I needed to restore a backup so I could import/migrate it into SQL Server. I installed MySql only, but did not register it as a service or add it to my path as I don't have the need to keep it running.
I used windows explorer to put my dump file in C:\code\dump.sql. Then opened MySql from the start menu item. Created the DB, then ran the source command with the full path like so:
mysql> create database temp
mysql> use temp
mysql> source c:\code\dump.sql
You can try SQLyog 'Execute SQL script' tool to import sql/dump files.
Using a 200MB dump file created on Linux to restore on Windows w/ mysql 5.5 , I had more success with the
source file.sql
approach from the mysql prompt than with the
mysql < file.sql
approach on the command line, that caused some Error 2006 "server has gone away" (on windows)
Weirdly, the service created during (mysql) install refers to a my.ini file that did not exist. I copied the "large" example file to my.ini
which I already had modified with the advised increases.
My values are
[mysqld]
max_allowed_packet = 64M
interactive_timeout = 250
wait_timeout = 250
./mysql -u <username> -p <password> -h <host-name like localhost> <database-name> < db_dump-file
You cannot use the Restore menu in MySQL Admin if the backup / dump wasn't created from there. It's worth a shot though. If you choose to "ignore errors" with the checkbox for that, it will say it completed successfully, although it clearly exits with only a fraction of rows imported...this is with a dump, mind you.
One-liner command to restore the generated SQL from mysqldump
mysql -u <username> -p<password> -e "source <path to sql file>;"
Assuming you already have the blank database created, you can also restore a database from the command line like this:
mysql databasename < backup.sql
You can also use the restore menu in MySQL Administrator. You just have to open the back-up file, and then click the restore button.
If you are already inside mysql prompt and assume your dump file dump.sql, then we can also use command as below to restore the dump
mysql> \. dump.sql
If your dump size is larger set max_allowed_packet value to higher. Setting this value will help you to faster restoring of dump.
How to Restore MySQL Database with MySQLWorkbench
You can run the drop and create commands in a query tab.
Drop the Schema if it Currently Exists
DROP DATABASE `your_db_name`;
Create a New Schema
CREATE SCHEMA `your_db_name`;
Open Your Dump File
Click the Open an SQL script in a new query tab icon and choose your db dump file.
Then Click Run SQL Script...
It will then let you preview the first lines of the SQL dump script.
You will then choose the Default Schema Name
Next choose the Default Character Set utf8 is normally a safe bet, but you may be able to discern it from looking at the preview lines for something like character_set.
Click Run
Be patient for large DB restore scripts and watch as your drive space melts away! 🎉
Local mysql:
mysql -u root --password=YOUR_PASS --database=YOUR_DB < ./dump.sql
And if you use docker:
docker exec -i DOCKER_NAME mysql -u root --password=YOUR_PASS --database=YOUR_DB < ./dump.sql