Migrating h2 to MySQL error

Now I want to migrate h2 to MySQL, and I am getting “Target DB is already populated!” error. I read this GitHub Issue, but it didn’t help.

I wrote a startup script like this.

#!/bin/sh
export MB_DB_TYPE=mysql
export MB_DB_DBNAME=metabase
export MB_DB_PORT=3306
export MB_DB_USER=user
export MB_DB_PASS=password
export MB_DB_HOST=localhost
export MB_JETTY_PORT=3000
export MB_DB_FILE=/opt/metabase/metabase.db
java -jar /opt/metabase/metabase.jar load-from-h2

But I got the following exception:

$ sudo run.sh
05-27 03:31:43 DEBUG plugins.classloader :: Using NEWLY CREATED classloader as shared context classloader: clojure.lang.DynamicClassLoader@470a9030
05-27 03:31:44 INFO metabase.util :: Loading Metabase...
05-27 03:31:44 INFO metabase.util :: Maximum memory available to JVM: 409.9 MB
05-27 03:32:49 INFO util.encryption :: Saved credentials encryption is DISABLED for this Metabase instance. 🔓 
 For more information, see https://metabase.com/docs/latest/operations-guide/encrypting-database-details-at-rest.html
05-27 03:33:28 WARN metabase.core :: WARNING: You have enabled namespace tracing, which could log sensitive information like db passwords.
05-27 03:33:28 INFO metabase.db :: Verifying mysql Database Connection ...
05-27 03:33:29 INFO driver.impl :: Registered abstract driver :sql  🚚
Load driver :sql took 908.5 ms
05-27 03:33:30 INFO driver.impl :: Registered abstract driver :sql-jdbc (parents: [:sql]) 🚚
Load driver :sql-jdbc took 940.2 ms
05-27 03:33:30 INFO driver.impl :: Registered driver :mysql (parents: [:sql-jdbc]) 🚚
Load driver :mysql took 1.5 s
05-27 03:33:30 INFO driver.impl :: Initializing driver :sql...
05-27 03:33:30 INFO driver.impl :: Initializing driver :sql-jdbc...
05-27 03:33:30 INFO driver.impl :: Initializing driver :mysql...
05-27 03:33:30 INFO metabase.db :: Successfully verified MySQL 5.7.30-0ubuntu0.16.04.1 application database connection. ✅
05-27 03:33:30 INFO metabase.db :: Running Database Migrations...
05-27 03:33:30 INFO metabase.db :: Setting up Liquibase...
05-27 03:33:35 INFO metabase.db :: Liquibase is ready.
05-27 03:33:35 INFO db.liquibase :: Checking if Database has unrun migrations...
05-27 03:33:49 INFO metabase.db :: Database Migrations Current ...  ✅
Database setup took 21.8 s
Testing if target DB is already populated...
java.lang.AssertionError: Assert failed: Target DB is already populated!
(not (mb-db-populated? target-db-conn))
	at metabase.cmd.load_from_h2$load_from_h2_BANG_$fn__68060.invoke(load_from_h2.clj:249)
	at clojure.java.jdbc$db_transaction_STAR_.invokeStatic(jdbc.clj:799)
	at clojure.java.jdbc$db_transaction_STAR_.invoke(jdbc.clj:769)
	at clojure.java.jdbc$db_transaction_STAR_.invokeStatic(jdbc.clj:834)
	at clojure.java.jdbc$db_transaction_STAR_.invoke(jdbc.clj:769)
	at clojure.java.jdbc$db_transaction_STAR_.invokeStatic(jdbc.clj:782)
	at clojure.java.jdbc$db_transaction_STAR_.invoke(jdbc.clj:769)
	at metabase.cmd.load_from_h2$load_from_h2_BANG_.invokeStatic(load_from_h2.clj:245)
	at metabase.cmd.load_from_h2$load_from_h2_BANG_.invoke(load_from_h2.clj:234)
	at clojure.lang.Var.invoke(Var.java:384)
	at metabase.cmd$load_from_h2.invokeStatic(cmd.clj:37)
	at metabase.cmd$load_from_h2.invoke(cmd.clj:30)
	at metabase.cmd$load_from_h2.invokeStatic(cmd.clj:33)
	at metabase.cmd$load_from_h2.invoke(cmd.clj:30)
	at clojure.lang.AFn.applyToHelper(AFn.java:152)
	at clojure.lang.AFn.applyTo(AFn.java:144)
	at clojure.core$apply.invokeStatic(core.clj:665)
	at clojure.core$apply.invoke(core.clj:660)
	at metabase.cmd$run_cmd$fn__67316.invoke(cmd.clj:127)
	at metabase.cmd$run_cmd.invokeStatic(cmd.clj:127)
	at metabase.cmd$run_cmd.invoke(cmd.clj:123)
	at clojure.lang.Var.invoke(Var.java:388)
	at metabase.core$run_cmd.invokeStatic(core.clj:133)
	at metabase.core$run_cmd.invoke(core.clj:131)
	at metabase.core$_main.invokeStatic(core.clj:155)
	at metabase.core$_main.doInvoke(core.clj:150)
	at clojure.lang.RestFn.applyTo(RestFn.java:137)
	at metabase.core.main(Unknown Source)
Command failed with exception: Assert failed: Target DB is already populated!
(not (mb-db-populated? target-db-conn))

How can I solve this? Thank you in advance.

Hi @miya10
Make sure that the MySQL database metabase is empty (no tables). You’re seeing the error because there’s currently tables already existing in your database, and Metabase will stop the migration process, so it does not overwrite existing data.

@flamber Thank you for your reply.
Actually I have already tried to migrate after deleting all tables in database metabase, but I got the same error.

@miya10
The code, where this error is coming from, makes a check for a table called setting in the database.

Please run this to verify:
mysql -hlocalhost -P3306 -uuser -p -e "show tables;" metabase

Or perhaps it has something to do with the privileges/grants of the MySQL user? Could you check that or try with a different user?

@flamber
Problem has been solved. It seemed to be character set problem.
I forgot to set character as utf8, so I run:
CREATE DATABASE metabase DEFAULT CHARACTER SET utf8;
instead of:
CREATE DATABASE metabase

Thank you very much!

1 Like

@miya10 Excellent! Which charset where you using before?
You should be using utf8mb4. For reference, it’s also noted in the documentation:
https://www.metabase.com/docs/latest/operations-guide/migrating-from-h2.html#notes

1 Like

@flamber I used latin1 before, and now I am using utf8mb4 instead of utf8.
Thank you for your help!

1 Like