1. 程式人生 > >擁有較大資料的mysql遷移方案

擁有較大資料的mysql遷移方案

方案1

In this case, normally, the best solution is a mysqldump using the --tab option like this:

mysqldump --tab=/path/to/serverlocaldir --single-transaction <database> table_a

tab option produce 2 file, one file -table_a.sql- that contains only the table create statement and the oher file -table_a.txt- contains tab-separated data.

Now you can create your new table

create table table_b like table_a;

Then you simply load data in your new table via LOAD DATA without care for the table's name.

LOAD DATA INFILE '/path/to/serverlocaldir/table_a.txt' 
  INTO TABLE table_b FIELDS TERMINATED BY '\t' ...

LOAD DATA is usually 20 times faster than using INSERT statements.

LOAD DATA速度比INSERT語句要快,這裡其實倒入也可以使用mysqlimport命令

方案2

I recently moved a 30GB database with the following stragegy:

Old Server

  • Stop mysql server
  • Copy contents of datadir to another location on disk (~/mysqldata/*)
  • Start mysql server again (downtime was 10-15 minutes)
  • compress the data (tar -czvf mysqldata.tar.gz ~/mysqldata
    )
  • copy the compressed file to new server

New Server

  • install mysql (don't start)
  • unzip compressed file (tar -xzvf mysqldata.tar.gz)
  • move contents of mysqldata to the datadir
  • Make sure your innodb_log_file_size is same on new server, or if it's not, don't copy the old log files (mysql will generate these)
  • Start mysql
這種方法就是直接複製資料庫的檔案結構,要求必須相同的mysql版本,和相同的配置才可以用這種方法

方案3

If you are considering migrating to another DB Server with the exact same version of MySQL, you may want torsync the datadir from the old server to the new server.

This will work regardless of InnoDB file layout or even the presence of MyISAM tables.

  1. install the same version of mysql on ServerB that ServerA has
  2. On ServerA, run RESET MASTER; to erase all binary logs before the rsycn process. If binary logging is not enabled, you can skip this step.
  3. On ServerA, run SET GLOBAL innodb_max_dirty_pages_pct = 0; from mysql and about 10 minutes (This purges dirty pages from the InnoDB Buffer Pool. It also helps perform a mysql shutdown faster) If your database is all MyISAM, you can skip this step.
  4. rsync /var/lib/mysql of ServerA to /var/lib/mysql on ServerB
  5. Repeat Step 3 until an rsync takes less than 1 minute
  6. service mysql stop on ServerA
  7. Perform one more rsync
  8. scp ServerA:/etc/my.cnf to ServerB:/etc/.
  9. service mysql start on ServerB
  10. service mysql start on ServerA (optional)

Essentially, here is what such a script would like this

其實這個跟方案2是一樣的。只是操作方法不同而已,當然要求也是一樣的

reference

http://serverfault.com/questions/445489/how-to-efficiently-dump-a-huge-mysql-innodb-database

http://dba.stackexchange.com/questions/174/how-can-i-move-a-database-from-one-server-to-another

http://dba.stackexchange.com/questions/24116/mysql-perform-a-big-data-migration-between-tables

https://matthew.mceachen.us/blog/faster-mysql-dumps-and-loads-with-tab-and-use-threads-1047.html