Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing MySQL database from one server to another

I have got two dedicated servers with root access. Both are running Linux. I want to import database from Server1 to Server2. I have already created an empty database on Server2. I want to know Linux command through which I can import database directly? Is there such a feature? Can I use mysqldump? I want to avoid first taking database backup on server1, then moving that file to server2, and then importing that file. Can import be done directly using some command?

Thanks

like image 405
Ali Avatar asked Dec 10 '10 08:12

Ali


People also ask

Can I copy MySQL data directory to another server?

There are broadly two options. Transfer the /var/lib/mysql dir to the new server as it is or do an export and import process. Copying the whole mysql directory will mean less data being transferred and an exact replication of the database from one machine to the other.

Can I import SQL Server database in MySQL?

The process will be to export the data directly to OCI Object Storage from the MS SQL Server and then import it to MySQL Database Service using MySQL Shell importTable() utility reading directly from the Object Storage Bucket.


2 Answers

If you want to avoid creating a file, transferring it, and loading it, you can just pipe mysqldump into either an mysql running on the other server, or an ssh mysql on the other server.

Using mysql to connect to the remote box:

mysqldump --all-databases | mysql -h yourserver.com 

Using ssh to connect to the other server

mysqldump --all-databases | ssh [email protected] mysql 

Use the mysqldump --all-databases to transfer them all, or just specify database names. Refer to the mysqldump documentation for more options.

You can also use the MySQL "Replication" feature, although that will take a bit more time to setup, and is rather tricky. Probably not worth all the time and trouble just for one single migration.

like image 200
Konerak Avatar answered Sep 18 '22 06:09

Konerak


Stop mysqld on the first server, copy the data directory (usually /var/lib/mysql) from server 1 to server 2, start mysqld on the second server, and it will now be identical to the first.

You do not have to use the import/export tools if you can stop the server while you copy the data files. Especially if you can compress the files before copying them, this will be the fastest way.

like image 33
Dan Grossman Avatar answered Sep 19 '22 06:09

Dan Grossman