在两个不同的服务器之间创建表

时间:2017-06-19 14:12:14

标签: mysql

我想执行CREATE TABLE tbl LIKE tb2语句。但我的问题是我的原始表位于远程服务器上,我需要在本地系统上创建一个类似于它的表。基本上我想执行类似的东西  CREATE TABLE --source mysql.hostname.com:3306:db_name1:tb1 LIKE --dest 127.0.0.1:3306:db_name2:tb2"。我想执行LIKE语句,因为我想保留索引。

如果以上不可行,那么我可以使用哪些其他方法来保存在原始表上创建的索引?

1 个答案:

答案 0 :(得分:0)

一个选项是执行以下脚本:

$ mysql --host=hostname -u $mysqluser -p$mysqlpass -e "DROP TABLE IF EXISTS \`db_name1\`.\`_tb1_to_local\`; CREATE TABLE \`db_name1\`.\`_tb1_to_local\` LIKE \`db_name1\`.\`tb1\`" \
&& mysqldump --host=hostname --opt ``db_name1`` ``_tb1_to_local`` -u $mysqluser -p$mysqlpass \
| mysql -u $mysqluser -p$mysqlpass ``db_name2`` \
&& mysql -u $mysqluser -p$mysqlpass -e "DROP TABLE IF EXISTS \`db_name2\`.\`tb2\`; RENAME TABLE \`db_name2\`.\`_tb1_to_local\` TO \`db_name2\`.\`tb2\`" \
&& mysql --host=hostname -u $mysqluser -p$mysqlpass -e "DROP TABLE IF EXISTS \`db_name1\`.\`_tb1_to_local\`"

一步一步脚本:

REMOTE(MySQL服务器):

mysql> USE `db_name1`;
Database changed

mysql> SHOW TABLES;
Empty set (0.00 sec)

mysql> DROP TABLE IF EXISTS `db_name1`.`tb1`,
    ->                      `db_name1`.`tb1_B`,
    ->                      `db_name1`.`tb1_A`;
Query OK, 0 rows affected, 3 warnings (0.00 sec)

mysql> CREATE TABLE `tb1_A` (
    ->   `col0` INT NOT NULL,
    ->   `col1` INT NOT NULL,
    ->   `col2` DECIMAL,
    ->   PRIMARY KEY(`col0`, `col1`)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `tb1_B` (
    ->     `col3` INT NOT NULL,
    ->     PRIMARY KEY (`col3`)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `tb1` (
    ->   `col4` INT NOT NULL AUTO_INCREMENT,
    ->   `col5` INT NOT NULL,
    ->   `col6` INT NOT NULL,
    ->   `col7` INT NOT NULL,
    -> 
    ->   PRIMARY KEY(`col4`),
    ->   INDEX (`col5`, `col6`),
    ->   INDEX (`col7`),
    -> 
    ->   FOREIGN KEY (`col5`, `col6`)
    ->     REFERENCES tb1_A(`col0`, `col1`)
    ->     ON UPDATE CASCADE ON DELETE RESTRICT,
    -> 
    ->   FOREIGN KEY (`col7`)
    ->     REFERENCES tb1_B(`col3`)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> USE `db_name1`; SHOW TABLES;
Database changed
+--------------------+
| Tables_in_db_name1 |
+--------------------+
| tb1                |
| tb1_A              |
| tb1_B              |
+--------------------+
3 rows in set (0.00 sec)

mysql> SHOW CREATE TABLE `db_name1`.`tb1`\G
*************************** 1. row ***************************
       Table: tb1
Create Table: CREATE TABLE `tb1` (
  `col4` int(11) NOT NULL AUTO_INCREMENT,
  `col5` int(11) NOT NULL,
  `col6` int(11) NOT NULL,
  `col7` int(11) NOT NULL,
  PRIMARY KEY (`col4`),
  KEY `col5` (`col5`,`col6`),
  KEY `col7` (`col7`),
  CONSTRAINT `tb1_ibfk_1` FOREIGN KEY (`col5`, `col6`) REFERENCES `tb1_A` (`col0`, `col1`) ON UPDATE CASCADE,
  CONSTRAINT `tb1_ibfk_2` FOREIGN KEY (`col7`) REFERENCES `tb1_B` (`col3`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

LOCAL(终端):

$ mysql --host=hostname -u $mysqluser -p$mysqlpass -e "DROP TABLE IF EXISTS \`db_name1\`.\`_tb1_to_local\`; CREATE TABLE \`db_name1\`.\`_tb1_to_local\` LIKE \`db_name1\`.\`tb1\`"

REMOTE(MySQL服务器):

mysql> USE `db_name1`; SHOW TABLES;
Database changed
+--------------------+
| Tables_in_db_name1 |
+--------------------+
| _tb1_to_local      |
| tb1                |
| tb1_A              |
| tb1_B              |
+--------------------+
4 rows in set (0.00 sec)

mysql> SHOW CREATE TABLE `db_name1`.`_tb1_to_local`\G
*************************** 1. row ***************************
       Table: _tb1_to_local
Create Table: CREATE TABLE `_tb1_to_local` (
  `col4` int(11) NOT NULL AUTO_INCREMENT,
  `col5` int(11) NOT NULL,
  `col6` int(11) NOT NULL,
  `col7` int(11) NOT NULL,
  PRIMARY KEY (`col4`),
  KEY `col5` (`col5`,`col6`),
  KEY `col7` (`col7`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

LOCAL(MySQL服务器):

mysql> USE `db_name2`; SHOW TABLES;
Database changed
Empty set (0.00 sec)

LOCAL(终端):

$ mysqldump --host=hostname --opt ``db_name1`` ``_tb1_to_local`` -u $mysqluser -p$mysqlpass \
| mysql -u $mysqluser -p$mysqlpass ``db_name2``

LOCAL(MySQL服务器):

mysql> USE `db_name2`; SHOW TABLES;
Database changed
+--------------------+
| Tables_in_db_name2 |
+--------------------+
| _tb1_to_local      |
+--------------------+
1 row in set (0.00 sec)

mysql> SHOW CREATE TABLE `db_name2`.`_tb1_to_local`\G
*************************** 1. row ***************************
       Table: _tb1_to_local
Create Table: CREATE TABLE `_tb1_to_local` (
  `col4` int(11) NOT NULL AUTO_INCREMENT,
  `col5` int(11) NOT NULL,
  `col6` int(11) NOT NULL,
  `col7` int(11) NOT NULL,
  PRIMARY KEY (`col4`),
  KEY `col5` (`col5`,`col6`),
  KEY `col7` (`col7`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

LOCAL(终端):

$ mysql -u $mysqluser -p$mysqlpass -e "DROP TABLE IF EXISTS \`db_name2\`.\`tb2\`; RENAME TABLE \`db_name2\`.\`_tb1_to_local\` TO \`db_name2\`.\`tb2\`"

LOCAL(MySQL服务器):

mysql> USE `db_name2`; SHOW TABLES;
Database changed
+--------------------+
| Tables_in_db_name2 |
+--------------------+
| tb2                |
+--------------------+
1 row in set (0.00 sec)

LOCAL(终端):

$ mysql --host=hostname -u $mysqluser -p$mysqlpass -e "DROP TABLE IF EXISTS \`db_name1\`.\`_tb1_to_local\`"

REMOTE(MySQL服务器):

mysql> USE `db_name1`; SHOW TABLES;
Database changed
+--------------------+
| Tables_in_db_name1 |
+--------------------+
| tb1                |
| tb1_A              |
| tb1_B              |
+--------------------+
3 rows in set (0.00 sec)