【MySQL】第三章:數據遷移

遷移庫
[root@wallet01 ~]# mysql -uroot -pabcd.1234
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tpcc100            |
+--------------------+
4 rows in set (0.12 sec)

mysql> use tpcc100
Database changed

mysql> show tables;
+-------------------+
| Tables_in_tpcc100 |
+-------------------+
| customer          |
| district          |
| history           |
| item              |
| new_orders        |
| order_line        |
| orders            |
| stock             |
| warehouse         |
+-------------------+
9 rows in set (0.00 sec)

[root@wallet01 ~]# yum install glib2-devel zlib-devel pcre-devel openssl-devel cmake

[root@wallet01 ~]# yum update -y nss curl libcurl
[root@wallet01 ~]# git clone https://github.com/maxbube/mydumper.git
Initialized empty Git repository in /root/mydumper/.git/
remote: Enumerating objects: 1185, done.
remote: Total 1185 (delta 0), reused 0 (delta 0), pack-reused 1185
Receiving objects: 100% (1185/1185), 983.01 KiB | 573 KiB/s, done.
Resolving deltas: 100% (724/724), done.

[root@wallet01 ~]# cd mydumper
[root@wallet01 mydumper]# cmake .
[root@wallet01 mydumper]# make
[root@wallet01 mydumper]# make install

[root@wallet01 ~]# su - mysql
[mysql@wallet01 ~]$ mydumper -V
mydumper 0.9.5, built against MySQL 5.6.43

[mysql@wallet01 ~]$ mydumper --help
-B, --database              Database to dump
-T, --tables-list           Comma delimited table list to dump
-o, --outputdir             Directory to output files to
-c, --compress              Compress output files
-m, --no-schemas            Do not dump table schemas with the data
-d, --no-data               Do not dump table data
-G, --triggers              Dump triggers
-E, --events                Dump events
-R, --routines              Dump stored procedures and functions
-W, --no-views              Do not dump VIEWs
-l, --long-query-guard      Set long query timer in seconds, default 60
-K, --kill-long-queries     Kill long running queries (instead of aborting)
-h, --host                  The host to connect to
-u, --user                  Username with the necessary privileges
-p, --password              User password
-a, --ask-password          Prompt For User password
-P, --port                  TCP/IP port to connect to
-S, --socket                UNIX domain socket file to use for connection
-t, --threads               Number of threads to use, default 4

[mysql@wallet01 ~]$ mydumper -u root -p abcd.1234 -B tpcc100 -o /home/mysql/backup

[mysql@wallet01 ~]$ cd backup
[mysql@wallet01 backup]$ ls -lh
total 3.8G
-rw-rw-r-- 1 mysql mysql  141 Feb 14 13:14 metadata
-rw-rw-r-- 1 mysql mysql 1.2K Feb 14 13:10 tpcc100.customer-schema.sql
-rw-rw-r-- 1 mysql mysql 853M Feb 14 13:11 tpcc100.customer.sql
-rw-rw-r-- 1 mysql mysql  684 Feb 14 13:10 tpcc100.district-schema.sql
-rw-rw-r-- 1 mysql mysql  55K Feb 14 13:10 tpcc100.district.sql
-rw-rw-r-- 1 mysql mysql  556 Feb 14 13:10 tpcc100.history-schema.sql
-rw-rw-r-- 1 mysql mysql  18M Feb 14 13:10 tpcc100.history.sql
-rw-rw-r-- 1 mysql mysql  360 Feb 14 13:10 tpcc100.item-schema.sql
-rw-rw-r-- 1 mysql mysql 7.9M Feb 14 13:10 tpcc100.item.sql
-rw-rw-r-- 1 mysql mysql  441 Feb 14 13:10 tpcc100.new_orders-schema.sql
-rw-rw-r-- 1 mysql mysql 5.6M Feb 14 13:10 tpcc100.new_orders.sql
-rw-rw-r-- 1 mysql mysql  902 Feb 14 13:10 tpcc100.order_line-schema.sql
-rw-rw-r-- 1 mysql mysql 1.3G Feb 14 13:14 tpcc100.order_line.sql
-rw-rw-r-- 1 mysql mysql  669 Feb 14 13:10 tpcc100.orders-schema.sql
-rw-rw-r-- 1 mysql mysql  79M Feb 14 13:10 tpcc100.orders.sql
-rw-rw-r-- 1 mysql mysql   66 Feb 14 13:10 tpcc100-schema-create.sql
-rw-rw-r-- 1 mysql mysql 1.1K Feb 14 13:10 tpcc100.stock-schema.sql
-rw-rw-r-- 1 mysql mysql 1.6G Feb 14 13:12 tpcc100.stock.sql
-rw-rw-r-- 1 mysql mysql  519 Feb 14 13:10 tpcc100.warehouse-schema.sql
-rw-rw-r-- 1 mysql mysql 5.3K Feb 14 13:10 tpcc100.warehouse.sql

[mysql@wallet01 backup]$ scp * 192.168.1.202:/home/mysql/backup

[root@wallet02 ~]# su - mysql
[mysql@wallet02 ~]$ myloader -V
myloader 0.9.5, built against MySQL 5.6.43

[mysql@wallet02 ~]$ myloader --help
-d, --directory                   Directory of the dump to import
-q, --queries-per-transaction     Number of queries per transaction, default 1000
-o, --overwrite-tables            Drop tables if they already exist
-B, --database                    An alternative database to restore into
-s, --source-db                   Database to restore
-e, --enable-binlog               Enable binary logging of the restore data
-h, --host                        The host to connect to
-u, --user                        Username with privileges to run the dump
-p, --password                    User password
-P, --port                        TCP/IP port to connect to
-S, --socket                      UNIX domain socket file to use for connection
-t, --threads                     Number of threads to use, default 4

[mysql@wallet02 ~]$ mysql -uroot -pabcd.1234
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

[mysql@wallet02 ~]$ myloader -u root -p abcd.1234 -o -B tpcc100 -q 10000 -d /home/mysql/backup

[mysql@wallet02 ~]$ mysql -uroot -pabcd.1234
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tpcc100            |
+--------------------+
4 rows in set (0.00 sec)

mysql> use tpcc100
Database changed

mysql> show tables;
+-------------------+
| Tables_in_tpcc100 |
+-------------------+
| customer          |
| district          |
| history           |
| item              |
| new_order         |
| order_line        |
| orders            |
| stock             |
| warehouse         |
+-------------------+
9 rows in set (0.00 sec)


表遷移(一)
源庫
[root@wallet01 ~]# su - mysql
[mysql@wallet01 ~]$ mysqldump -uroot -pabcd.1234 -d tpcc100 customer >customer.sql
[mysql@wallet01 ~]$ scp customer.sql 192.168.1.202:/home/mysql

[mysql@wallet01 ~]$ mysql -uroot -pabcd.1234
mysql> use tpcc100
Database changed

mysql> select count(*) from customer;
+----------+
| count(*) |
+----------+
|  1500000 |
+----------+
1 row in set (0.33 sec)

目標庫
[root@wallet02 ~]# su - mysql
[mysql@wallet02 ~]$ mysql -uroot -pabcd.1234
mysql> create database tpcc100;
Query OK, 1 row affected (0.05 sec)

mysql> use tpcc100
Database changed

mysql> source customer.sql
Query OK, 0 rows affected (0.01 sec)

mysql>  alter table customer discard tablespace;
Query OK, 0 rows affected (0.03 sec)

源庫
mysql> flush table customer for export;
Query OK, 0 rows affected (0.00 sec)

[mysql@wallet01 ~]$ cd /usr/local/mysql/data/tpcc100
[mysql@wallet01 tpcc100]$ scp customer.cfg 192.168.1.202:/usr/local/mysql/data/tpcc100
[mysql@wallet01 tpcc100]$ scp customer.ibd 192.168.1.202:/usr/local/mysql/data/tpcc100

mysql>  unlock tables;
Query OK, 0 rows affected (0.03 sec)

目標庫
mysql> alter table customer import tablespace; 
Query OK, 0 rows affected (11.52 sec)

mysql> select count(*) from customer;
+----------+
| count(*) |
+----------+
|  1500000 |
+----------+
1 row in set (0.37 sec)


遷移表(二)
源庫
[root@wallet01 ~]# su - mysql
[mysql@wallet01 ~]$ mysqldump -uroot -pabcd.1234 -d tpcc100 customer >customer.sql
[mysql@wallet01 ~]$ scp customer.sql 192.168.1.202:/home/mysql

[mysql@wallet01 ~]$ mysql -uroot -pabcd.1234
mysql> select count(*) from tpcc100.customer;
+----------+
| count(*) |
+----------+
|  1500000 |
+----------+
1 row in set (0.32 sec)

[mysql@wallet01 ~]$ xtrabackup --backup --user=xtrabackup --password=xtrabackup \
--databases="tpcc100.customer" --target-dir=/home/mysql/backup/`date '+%Y-%m-%d_%H-%M-%S'`

[mysql@wallet01 ~]$ cd backup/2019-02-14_14-30-01/tpcc100
[mysql@wallet01 tpcc100]$ ls -lh
total 965M
-rw-r----- 1 mysql mysql 9.2K Feb 14 14:30 customer.frm
-rw-r----- 1 mysql mysql 964M Feb 14 14:30 customer.ibd

[mysql@wallet01 ~]$ xtrabackup --prepare --apply-log --export \
--target-dir=/home/mysql/backup/2019-02-14_14-30-01

[mysql@wallet01 ~]$ cd backup/2019-02-14_14-30-01/tpcc100
[mysql@wallet01 tpcc100]$ ls -lh
total 965M
-rw-rw-r-- 1 mysql mysql 1.8K Feb 14 14:32 customer.cfg
-rw-r----- 1 mysql mysql  16K Feb 14 14:32 customer.exp
-rw-r----- 1 mysql mysql 9.2K Feb 14 14:30 customer.frm
-rw-r----- 1 mysql mysql 964M Feb 14 14:30 customer.ibd

目標庫
[root@wallet02 ~]# su - mysql
[mysql@wallet02 ~]$ mysql -uroot -pabcd.1234
mysql> create database tpcc100;
Query OK, 1 row affected (0.05 sec)

mysql> use tpcc100
Database changed

mysql> source customer.sql
Query OK, 0 rows affected (0.01 sec)

mysql>  alter table customer discard tablespace;
Query OK, 0 rows affected (0.03 sec)

[mysql@wallet01 ~]$ cd backup/2019-02-14_14-30-01/tpcc100
[mysql@wallet01 tpcc100]$ scp customer.cfg 192.168.1.202:/usr/local/mysql/data/tpcc100
[mysql@wallet01 tpcc100]$ scp customer.ibd 192.168.1.202:/usr/local/mysql/data/tpcc100

[mysql@wallet02 ~]$ mysql -uroot -pabcd.1234
mysql> alter table tpcc100.customer import tablespace; 
Query OK, 0 rows affected (30.52 sec)

mysql> select count(*) from tpcc100.customer;
+----------+
| count(*) |
+----------+
|  1500000 |
+----------+
1 row in set (0.41 sec)


遷移表(三)
源庫
[root@wallet01 ~]# su - mysql
[mysql@wallet01 ~]$ mysqldump -uroot -pabcd.1234 -d tpcc100 customer >customer.sql
[mysql@wallet01 ~]$ scp customer.sql 192.168.1.202:/home/mysql

[mysql@wallet01 ~]$ mysql -uroot -pabcd.1234
mysql> show variables like '%secure%';
+------------------+-------------------+
| Variable_name    | Value             |
+------------------+-------------------+
| secure_auth      | ON                |
| secure_file_priv | /home/mysql/data/ |
+------------------+-------------------+
2 rows in set (0.00 sec)

mysql> select count(*) from customer;
+----------+
| count(*) |
+----------+
|  1500000 |
+----------+
1 row in set (0.47 sec)

mysql> select * from customer into outfile '/home/mysql/data/customer.txt' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'; 
Query OK, 1500000 rows affected (17.52 sec)

[mysql@wallet01 ~]$ cd data
[mysql@wallet01 data]$ ls -lh
total 849M
-rw-rw-rw- 1 mysql mysql 849M Feb 15 09:39 customer.txt
[mysql@wallet01 data]$ scp customer.txt 192.168.1.202:/home/mysql/data

目標庫
[root@wallet02 ~]# su - mysql
[mysql@wallet02 ~]$ mysql -uroot -pabcd.1234
mysql> show variables like '%secure%';
+------------------+-------------------+
| Variable_name    | Value             |
+------------------+-------------------+
| secure_auth      | ON                |
| secure_file_priv | /home/mysql/data/ |
+------------------+-------------------+
2 rows in set (0.00 sec)

mysql> create database tpcc100;
Query OK, 1 row affected (0.05 sec)

mysql> use tpcc100
Database changed

mysql> source customer.sql
Query OK, 0 rows affected (0.01 sec)

mysql> set foreign_key_checks=0;
Query OK, 0 rows affected (0.00 sec)

mysql> load data infile '/home/mysql/data/customer.txt' into table customer fields terminated by ',' optionally enclosed by '"' lines terminated by '\n'; 
Query OK, 1500000 rows affected (1 min 31.75 sec)
Records: 1500000  Deleted: 0  Skipped: 0  Warnings: 0

mysql> set foreign_key_checks=1;
Query OK, 0 rows affected (0.04 sec)

mysql> select count(*) from customer;
+----------+
| count(*) |
+----------+
|  1500000 |
+----------+
1 row in set (0.42 sec)




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章