我有一些数据存储在H2Database文件中,我想将其转换为MySQL .sql
数据库文件。我可以采用哪些方法?
回答Thomas Mueller,对我来说SquirrelSQL运行良好。 以下是在Windows上转换H2数据库的过程:
进入“驱动程序列表”,默认情况下所有内容都显示为红色。
选择“H2”驱动程序,并在“额外类路径”中指定“h2-1.3.173.jar”的完整路径(例如)。H2驱动程序应在列表中显示为蓝色复选框。
选择目标驱动程序(PostgreSQL、MySQL),并执行同样的操作,例如对于PostgreSQL,在额外的类路径中指定“postgresql-9.4-1201.jdbc41.jar”的完整路径。
进入“别名”,然后单击H2上的“+”:配置JDBC链,例如复制/粘贴启动H2时获得的jdbc链,然后对目标数据库执行相同的操作:单击“+”,配置并“测试”。
当您双击别名时,您应该在新选项卡中看到数据库中的所有内容。进入源数据库中的表格,多选所有表格并右键单击:“复制表格”。
进入别名中的目标数据库,然后执行“粘贴表格”操作。当所有表格一起复制时,外键引用也会生成。
检查您的主键:从H2到PostgreSQL,我丢失了主键约束和自增功能。 您还可以通过右键单击进行重命名列和表格:“重构”。我在完全复制后使用它来重命名保留字列,方法是在选项中禁用名称检查。
对我来说这很有效。
我创建了一个Groovy脚本,用于从h2迁移到mysql。从那里你可以做一个mysqldump。它要求Mysql数据库中存在表。稍作修改,它应该也适用于其他DBMS。
@Grapes(
[
@Grab(group='mysql', module='mysql-connector-java', version='5.1.26'),
@Grab(group='com.h2database', module='h2', version='1.3.166'),
@GrabConfig(systemClassLoader = true)
])
import groovy.sql.Sql
def h2Url='jdbc:h2:C:\\Users\\xxx\\Desktop\\h2\\sonardata\\sonar'
def h2User='sonar'
def h2Passwd='sonar'
def mysqlUrl='jdbc:mysql://10.56.xxx.xxx:3306/sonar?useunicode=true&characterencoding=utf8&rewritebatchedstatements=true'
def mysqlUser='sonar'
def mysqlPasswd='xxxxxx'
def mysqlDatabase='sonar'
sql = Sql.newInstance(h2Url, h2User, h2Passwd, 'org.h2.Driver' )
def tables = [:]
sql.eachRow("select * from information_schema.columns where table_schema='PUBLIC'") {
if(!it.TABLE_NAME.endsWith("_MY")) {
if (tables[it.TABLE_NAME] == null) {
tables[it.TABLE_NAME] = []
}
tables[it.TABLE_NAME] += it.COLUMN_NAME;
}
}
tables.each{tab, cols ->
println("processing $tab")
println("droppin $tab"+"_my")
sql.execute("DROP TABLE IF EXISTS "+tab+"_my;")
sql.execute("create linked table "+tab+"_my ('com.mysql.jdbc.Driver', '"+mysqlUrl+"', '"+mysqlUser+"', '"+mysqlPasswd+"', '"+mysqlDatabase+"."+tab.toLowerCase()+"');")
sql.eachRow("select count(*) as c from " + tab + "_my"){println("deleting $it.c entries from mysql table")}
result = sql.execute("delete from "+tab+"_my")
colString = cols.join(", ")
sql.eachRow("select count(*) as c from " + tab){println("starting to copy $it.c entries")}
sql.execute("insert into " + tab + "_my ("+colString+") select "+colString+" from " + tab)
}