# Migrating grafana data to new database

**URL:** <https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454>\
**Category:** Configuration\
**Tags:** migration, database, sqlite\
**Created:** [August 25, 2017, 4:42pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454 "2017-08-25T16:42:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![retzkek](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/retzkek/32/120_2.png) [@retzkek](https://community.grafana.com/u/retzkek)\
**Post date:** [August 25, 2017, 4:42pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/1 "2017-08-25T16:42:55Z")

</div>

We’re moving our Grafana instances to new HA servers, so we need to move the data from sqlite to a MySQL/MariaDB (Galera) cluster. I’m curious if anyone has developed any handy tools for doing such a migration?

Using the API and tools like wizzy is clean, but won’t move user data, so I think some sort of direct SQL migration is required. However, I’m thinking it’s best to let Grafana create the schema in MySQL, rather than trying to translate the schema from a database dump. Maybe it won’t be too bad to just extract the INSERTs from a dump and load them into MySQL?

Any thoughts?

Thanks,  
Kevin

---

<div class="post-metadata">

**Author:** ![dcech](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dcech/32/49_2.png) [@dcech](https://community.grafana.com/u/dcech)\
**Post date:** [August 25, 2017, 5:11pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/2 "2017-08-25T17:11:57Z")

</div>

This is the LATEST version of a small script I’ve used successfully to dump data from a grafana sqlite db in a format that works with mysql for migrating instances from sqlite to mysql:

```auto
#!/bin/bash
DB=$1
TABLES=$(sqlite3 $DB .tables | sed -r 's/(\S+)\s+(\S)/\1\n\2/g' | grep -v migration_log)
for t in $TABLES; do
    echo "TRUNCATE TABLE $t;"
done
for t in $TABLES; do
    echo -e ".mode insert $t\nselect * from $t;"
done | sqlite3 $DB

```

Use it like `sqlitedump.sh grafana.db > grafana.sql` to create the script to insert data into the MySQL database.

Start Grafana and let it set up the db structure in MySQL, then you can run the sql to populate the content (be warned it truncates the tables first, so any existing data in mysql will be lost)  
`mysql grafana < grafana.sql`

---

<div class="post-metadata">

**Author:** ![retzkek](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/retzkek/32/120_2.png) [@retzkek](https://community.grafana.com/u/retzkek)\
**Post date:** [August 25, 2017, 6:34pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/3 "2017-08-25T18:34:30Z")

</div>

Thanks @dcech, worked great! I had to make a couple tweaks, notably sqlite .tables printed in columns, so grep was removing several other tables from the list.

```
  #!/bin/sh
 DB=$1
-TABLES=$(sqlite3 $DB .tables | grep -v migration_log)
+TABLES=$(sqlite3 $DB .tables | sed -r 's/\s+/\n/g' | grep -v -e migration_log -e '^$')
 for t in $TABLES; do
     echo "TRUNCATE TABLE $t;"
 done
 for t in $TABLES; do
- echo ".mode insert $t\nselect * from $t;"
+ echo -e ".mode insert $t\nselect * from $t;"
 done | sqlite3 $DB
```

---

<div class="post-metadata">

**Author:** ![ingenieria5](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@ingenieria5](https://community.grafana.com/u/ingenieria5)\
**Post date:** [November 21, 2017, 2:56pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/4 "2017-11-21T14:56:10Z")

</div>

Hi dcech!

I test the script and it worked well, but it doesn’t migrate the user table of the sqlite3. Is there a solution for this! Thank you!

---

<div class="post-metadata">

**Author:** ![dcech](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dcech/32/49_2.png) [@dcech](https://community.grafana.com/u/dcech)\
**Post date:** [November 21, 2017, 3:13pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/5 "2017-11-21T15:13:58Z")

</div>

Did you use the updated script @retzkek posted?

---

<div class="post-metadata">

**Author:** ![ingenieria5](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@ingenieria5](https://community.grafana.com/u/ingenieria5)\
**Post date:** [November 21, 2017, 3:25pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/6 "2017-11-21T15:25:55Z")

</div>

Yes! I use both scripts.

Just truncate this tables:

TRUNCATE TABLE alert  
TRUNCATE TABLE dashboard\_tag  
TRUNCATE TABLE playlist  
TRUNCATE TABLE star  
TRUNCATE TABLE alert\_notification  
TRUNCATE TABLE dashboard\_version  
TRUNCATE TABLE playlist\_item  
TRUNCATE TABLE temp\_user  
TRUNCATE TABLE annotation  
TRUNCATE TABLE data\_source  
TRUNCATE TABLE plugin\_setting  
TRUNCATE TABLE test\_data  
TRUNCATE TABLE dashboard  
TRUNCATE TABLE org  
TRUNCATE TABLE quota  
TRUNCATE TABLE dashboard\_snapshot  
TRUNCATE TABLE org\_user  
TRUNCATE TABLE session

Thank you!

---

<div class="post-metadata">

**Author:** ![dcech](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dcech/32/49_2.png) [@dcech](https://community.grafana.com/u/dcech)\
**Post date:** [November 21, 2017, 3:33pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/7 "2017-11-21T15:33:18Z")

</div>

What is the output of `sqlite3 grafana.db .tables`?

---

<div class="post-metadata">

**Author:** ![ingenieria5](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@ingenieria5](https://community.grafana.com/u/ingenieria5)\
**Post date:** [November 21, 2017, 3:35pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/8 "2017-11-21T15:35:44Z")

</div>

![imagen](https://us1.discourse-cdn.com/grafana/original/2X/d/d1325b2c4892e35e5332d4dd2413d2596b744bd3.png)

---

<div class="post-metadata">

**Author:** ![dcech](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dcech/32/49_2.png) [@dcech](https://community.grafana.com/u/dcech)\
**Post date:** [November 21, 2017, 3:38pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/9 "2017-11-21T15:38:29Z")

</div>

Try the updated script in my post above.

---

<div class="post-metadata">

**Author:** ![ingenieria5](https://avatars.discourse-cdn.com/v4/letter/i/e495f1/32.png) [@ingenieria5](https://community.grafana.com/u/ingenieria5)\
**Post date:** [November 21, 2017, 4:59pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/10 "2017-11-21T16:59:31Z")

</div>

Thanks @dcech it works!

---

<div class="post-metadata">

**Author:** ![mkfort](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mkfort](https://community.grafana.com/u/mkfort)\
**Post date:** [December 5, 2017, 4:22pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/11 "2017-12-05T16:22:31Z")

</div>

I had to change the first line to  
#!/bin/bash  
to get this to work. It was breaking on the -e option to echo

---

<div class="post-metadata">

**Author:** ![thachnv92](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/thachnv92/32/4183_2.png) [@thachnv92](https://community.grafana.com/u/thachnv92)\
**Post date:** [July 6, 2018, 4:13am UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/12 "2018-07-06T04:13:42Z")

</div>

Hi @dcech  
I try import database to mysql, but i have a error about all tables.

> ERROR 1146 (42S02) at line 1 in file: ‘grafana.sql’: Table ‘grafana.alert’ doesn’t exist  
> ERROR 1146 (42S02) at line 2 in file: ‘grafana.sql’: Table ‘grafana.dashboard\_tag’ doesn’t exist  
> ERROR 1146 (42S02) at line 3 in file: ‘grafana.sql’: Table ‘grafana.playlist’ doesn’t exist  
> ERROR 1146 (42S02) at line 4 in file: ‘grafana.sql’: Table ‘grafana.star’ doesn’t exist  
> ERROR 1146 (42S02) at line 5 in file: ‘grafana.sql’: Table ‘grafana.alert\_notification’ doesn’t exist

Please help me to resolve it.  
Thanks.

---

<div class="post-metadata">

**Author:** ![thachnv92](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/thachnv92/32/4183_2.png) [@thachnv92](https://community.grafana.com/u/thachnv92)\
**Post date:** [July 6, 2018, 6:10am UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/13 "2018-07-06T06:10:17Z")

</div>

Hi @dcech @retzkek @ingenieria5 @mkfort  
I have a issue.  
I can’t import data from table: data\_soure and dashboard to mysql.  
Error:

> ERROR 1136 (21S01) at line 797 in file: ‘grafana.sql’: Column count doesn’t match value count at row 1

Please help me to resolve it.  
Thanks.

---

<div class="post-metadata">

**Author:** ![dcech](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/dcech/32/49_2.png) [@dcech](https://community.grafana.com/u/dcech)\
**Post date:** [July 6, 2018, 11:40am UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/14 "2018-07-06T11:40:14Z")

</div>

You need to make sure that the database you’re importing into was created by the same version of grafana that the export was from. That error indicates that you created the database with a newer version, so it has columns that are not in the dump.

---

<div class="post-metadata">

**Author:** ![thachnv92](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/thachnv92/32/4183_2.png) [@thachnv92](https://community.grafana.com/u/thachnv92)\
**Post date:** [July 7, 2018, 12:11am UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/15 "2018-07-07T00:11:02Z")

</div>

Yes.  
I did it.  
I must upgrade my server to new version.

---

<div class="post-metadata">

**Author:** ![abandt](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@abandt](https://community.grafana.com/u/abandt)\
**Post date:** [July 12, 2018, 8:03pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/16 "2018-07-12T20:03:40Z")

</div>

Hello,

I’m attempting to use dcech’s script to migrate from grafana 4.2.0/sqllite to grafana 5.1.3/mysql (percona 5.7.18 to be exact). The steps I’ve taken are:

- copied the existing sqllite db to a new 4.x instance,
- installed the 5.x rpm and restarted grafana-server
- dumped the sqllite DB via the script
- Re-point the new instance to use the mysql server so it creates all the grafana tables

but when I go to import the dump into mysql I’m getting this:

```
# mysql < upgrade.sql
ERROR 1366 (HY000) at line 176: Incorrect string value: '\xFF\xFF\xFF\xC2\xFF\xFF...' for column 'data' at row 1

```

Everything I’ve read says this is a UTF8 encoding problem and to ensure that the DB is utf8mb4, and it is:

```
mysql> use grafana;
mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_general_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
10 rows in set (0.01 sec)

mysql>

```

I’ve tried removing the offending rows (to the tune of hundreds of rows) and even removing all the insert statements for the dashboard\_version table altogether (this is the table it’s trying to insert into on line 176, but the error just moves to another line.

If anyone has any thoughts on where I’m failing on this, I’d appreciate it, thanks!

---

<div class="post-metadata">

**Author:** ![thachnv92](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/thachnv92/32/4183_2.png) [@thachnv92](https://community.grafana.com/u/thachnv92)\
**Post date:** [July 19, 2018, 6:33am UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/17 "2018-07-19T06:33:29Z")

</div>

Hi  
You must upgrade from 4.5 to 5.2 (You should backup grafana.db to new directory)  
Then you use this script to convert to sqlite ([Migrating grafana data to new database](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/2?u=thachnv92))  
Good luck ^^

---

<div class="post-metadata">

**Author:** ![abandt](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@abandt](https://community.grafana.com/u/abandt)\
**Post date:** [July 20, 2018, 1:33am UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/18 "2018-07-20T01:33:11Z")

</div>

Hi There,

Thanks for taking the time to reply. I did upgrade from the grafana version of 4 that I’m running (4.2.0) to 5.1.3 before doing the dump (the first two bullet points in my first message). Are you saying that I explicitly must go from version 4.5 -\> to version 5.2 in order to make this work?

---

<div class="post-metadata">

**Author:** ![thachnv92](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/thachnv92/32/4183_2.png) [@thachnv92](https://community.grafana.com/u/thachnv92)\
**Post date:** [August 15, 2018, 7:48am UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/19 "2018-08-15T07:48:43Z")

</div>

Yes.  
You may upgrade to newest version.

---

<div class="post-metadata">

**Author:** ![raja4frnz](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@raja4frnz](https://community.grafana.com/u/raja4frnz)\
**Post date:** [November 19, 2018, 4:14pm UTC](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454/20 "2018-11-19T16:14:33Z")

</div>

Hi ,

I am trying to migrate sqlite3 to mysql database using the scripts written by @dcech @retzkek. I am able to see all the data in mysql, but Dashboards dont have any content. like in below .

 ![grafana](https://us1.discourse-cdn.com/grafana/original/2X/d/d860ab9708c42f38bdcfe05430889b26b4c09dbe.png)

which is similar problem like – [https://community.grafana.com/t/migration-from-sqlite3-to-mysql-database/5319](https://community.grafana.com/t/migration-from-sqlite3-to-mysql-database/5319)

but , I can’t upload db to [Convert SQLite to MySQL online - RebaseData](https://www.rebasedata.com/convert-sqlite-to-mysql-online) because of internal restrictions.

Grafana version : Grafana v5.1.4 (a5fe24fcc)  
sqlite version: sqlite-3.7.17-8.el7.x86\_64  
mysql version: 5.7.24-0ubuntu0.18.04.1

can any one help me out to fix this issue ??

[Next page](https://community.grafana.com/t/migrating-grafana-data-to-new-database/2454.md?page=2)
