22

One thing I love about .NET is the ability to have a database file along with the project. I know that using a SQLite database, this can be done, but did someone achieve this with a MySQL database backend?

So for instance, if I run a java program, it should be able to start its own mini MySQL server and manipulate data. So essentially, I want the same flow as with a SQLite but I need the power of MySQL.

5
  • 1
    Apache Derby(JavaDB) is extremely easy to embed in a java application and it works quite well. Commented Nov 24, 2009 at 17:11
  • 1
    Thanks.. I'll look into that. I have a lot of scripts that work with MySQL currently. Would you happen to know if it is easy enough to convert JavaDB into a MySQL Database? Commented Nov 24, 2009 at 17:14
  • 1
    HSQLDB is also easy, for that matter. But the question is about MySQL :) Commented Nov 24, 2009 at 17:18
  • 1
    Do not forget the great H2 Database! Commented Nov 20, 2012 at 7:40
  • Now in 2018 there are some perfect options. Check my answer for more information about these. Commented Apr 3, 2018 at 14:23

4 Answers 4

24

If you don't mind using MariaDB (the open source variant of MySQL, basically works the same) MariaDB4j can be the perfect option for production enviroments.

MariaDB4j is a Java (!) "launcher" for MariaDB (the "backward compatible, drop-in replacement of the MySQL(R) Database Server", see FAQ and Wikipedia), allowing you to use MariaDB (MySQL(R)) from Java without ANY installation / external dependencies. Read again: You do NOT have to have MariaDB binaries installed on your system to use MariaDB4j!

As it works completely without any requirements that have to be on the users' pc it is probably the best option to get MySQL embedded. Converting a project that doesn't use an Embedded database into MariaDB4j is as easy as calling:

DB db = DB.newEmbeddedDB(3306);

Read the github page for more information. Maven central dependency is:

<dependency>
    <groupId>ch.vorburger.mariaDB4j</groupId>
    <artifactId>mariaDB4j</artifactId>
    <version>2.2.3</version>
</dependency>

You can combine this with the newest driver to get access to all functionality of MySQL 8.0 (win64/win32=windows, mac64=macos, linux64=linux):

<dependency>
  <groupId>org.craftercms.mariaDB4j</groupId>
  <artifactId>mariaDB4j-db-win64</artifactId>
  <version>10.4.6.2</version>
</dependency>

If you do mind using MariaDB, another option is Wix Embedded MySQL.

Wix Embedded MySQL is a library that provides a way to run real MySql within integration tests.

Why?

  • Your tests can run on production-like environments: match version, encoding, timezone, database/schema/user settings;
  • Its easy, much easier than installing right version by hand;
  • You can use different versions/configuration per project without any local set-up;
  • Supports multiple platforms: Windows, Linux, and OSX;
  • Provides constantly updated multiple versions of MySql - 5.5, 5.6, 5.7, 8.0;
  • Testing matrix for all supported OSes (x86/x64) and versions (5.5, 5.6, 5.7, 8.0).
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Simon, any tool which supports mysql 8.0 ?
@LasithaWeerasinghe Not that I know of, but mariadb4j can be combined with a newer driver version to get the same functionality. I edited my post to show what you should add to pom.xml to get this newer driver.
It looks like MySQL 8.0 is supported since version 4.5.0 (reference commit).
18

A quick search shows this: MySQL Connector/MXJ — for embedding MySQL server in Java applications on the MySQL Downloads page at:

http://dev.mysql.com/downloads/

3 Comments

For anyone else reading this answer long after it was given, MXJ stopped development at MySQL version 5.1.40, Connector/J version 5.0.11. See dev.mysql.com/doc/connector-mxj/en/connector-mxj-versions.html
@PeterDolberg are there alternative packages achieving a MySQL (embedded) to be run while JUnit testing?
@TheConstructor Check my answer. Wix Embedded MySQL could be your solution.
7

For future reference to anyone looking to embed mysql, there is a utility from the mysql guys that does this http://downloads.mysql.com/archives/c-mxj/

2 Comments

Please note that development of MySQL Connector/MXJ has been discontinued.
And what is the alternative for this ? is there any solution that can be freely embedded ?
6

It sounds like you want an embedded database. While MySQL Connector seems nice, it will launch a separate server process. If you want the database server to run in the Java virtual machine, there are several embedded databases for Java.

The two that I've seen used the most are:

  1. Apache Derby / JavaDB
  2. HSQL

1 Comment

I've had terrible experiences with depending on HSQL for testing a production system that uses Oracle. The differences in behavior results in test-specific hacks or worse, never-written test cases because it was too much trouble. I think it's more important than anything to create a test environment as close as possible to the real thing. Using some extra system resources is usually a very acceptable tradeoff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.