简体   繁体   English

将表数据从MySQL数据库迁移到另一个MySQL数据库

[英]Migrating table data from a MySQL database to another MySQL database

I'm having some trouble understanding how this works. 我在理解其工作方式时遇到了一些麻烦。 I know you can easily migrate a new database to MySQL through some of workbench's tools, but what I want to do here, is to get certain data from tables of another database to insert it into my own database. 我知道您可以通过一些工作台的工具轻松地将新数据库迁移到MySQL,但是我在这里要做的是从另一个数据库的表中获取某些数据,然后将其插入到我自己的数据库中。 The issue is that this other database does not have the same tables or the same amount of data. 问题是该另一个数据库没有相同的表或相同数量的数据。 Some of it is useless to me, some of it doesn't fit with my tables. 有些对我没用,有些不适合我的桌子。

So how does one regularly get around to doing this? 那么,一个人如何定期去做呢? I basically need something that can capture data, no matter how it may have been arranged in the past, and place it into my database. 基本上,无论过去如何安排,我都需要可以捕获数据的东西并将其放入我的数据库中。 It sounds hard to do when the program doesn't know what to expect, so basically that is my issue. 当程序不知道会发生什么时,听起来很难做到,所以基本上这是我的问题。 I'd appreciate it if someone could point me in the right direction. 如果有人能指出我正确的方向,我将不胜感激。

Can't you do? 不行吗 Perhaps I missed somthing 也许我错过了什么

-- First create your new table, avoid the columns that you don't care about.    
CREATE TABLE newTable......

-- select from the old table only the columns that you need and insert them in the new table
    INSERT INTO newTable(col1, col2, col3)
SELECT col1, col2, col3
FROM oldTable
WHERE .....

And if you want to merge data between multiple columns use CONCAT_WS 如果要合并多个列之间的数据,请使用CONCAT_WS

   INSERT INTO newTable(col1, col2, col3)
SELECT CONCAT_WS(' ', col1, col2) AS col1, col3
FROM oldTable
WHERE .....

What you want to do is an import program. 您要执行的是导入程序。 I'm not sure there an application out there that does this kind of thing automatically. 我不确定那里是否有应用程序可以自动执行这种操作。

Basically, you write a program that connects to you first database, extract the data, connect to the second database and generate insert statement from the extracted data. 基本上,您编写了一个程序来连接到第一个数据库,提取数据,连接到第二个数据库,并从提取的数据生成insert语句。

Here is a simple python pseudo code that coudl inspire you: 这是一个可激发您灵感的简单python伪代码:

import mysql.connector

cnx = mysql.connector.connect(user='username', password='password',
                              host='127.0.0.1',
                              database='yourdatabase')
cnx2 = mysql.connector.connect(user='username2', password='password2',
                              host='127.0.0.1',
                              database='yourdatabase2')
cursor = cnx.cursor()
cursor.execute("SELECT * FROM ...")
for (data) in cursor:
  cursor2 = cnx2.cursor()
  cursor2.execute("INSERT INTO ...")       // Use the data variables
  cursor2.close()
cursor.close()
cnx.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将约9000万条记录的MySQL表中的数据迁移到另一个数据库 - Migrating data from a ~90 million record MySQL table to another database mysql 将数据从一个数据库迁移到另一个数据库 - Migrating data from one database to another database in mysql 使用talend将数据从一个mysql数据库迁移到另一个 - Migrating data from one mysql database to another using talend 将数据库数据从mssql迁移到mysql - migrating database data from mssql to mysql 使用不同的架构将数据从MySQL数据库迁移到PostgreSQL数据库 - Migrating Data from a MySQL database to a PostgreSQL database with a different schema 如何将表从一个mysql数据库复制到另一个mysql数据库 - How to copy a table from one mysql database to another mysql database 每5秒将数据从一个表复制到另一个@mysql数据库 - copy data from one table to another @mysql database every 5 secs 从另一个MySQL数据库中的表访问数据和引用ID? - Access data and reference ID from a table in another MySQL database or? PHP / MySQL:将表和数据从一个数据库复制到另一个数据库 - PHP/MySQL: Copy Table and Data from one Database to another 将mysql数据库中的数据触发到另一个表中 - Trigger data in mysql database into another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM