Java、MySQL:有没有办法用 Java 程序嵌入 MySQL 服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1791417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:00:07  来源:igfitidea点击:

Java, MySQL: Is there a way to embed a MySQL server with a Java program?

javamysqlembedded-database

提问by Legend

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?

我喜欢 .NET 的一件事是能够将数据库文件与项目一起使用。我知道使用 SQLite 数据库可以做到这一点,但是有人用 MySQL 数据库后端实现了这一点吗?

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.

例如,如果我运行一个 java 程序,它应该能够启动它自己的迷你 MySQL 服务器并操作数据。所以本质上,我想要与 SQLite 相同的流程,但我需要 MySQL 的强大功能。

回答by Legend

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

快速搜索显示: MySQL 连接器/MXJ — 用于在 MySQL 下载页面上的 Java 应用程序中嵌入 MySQL 服务器:

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

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

回答by Simon Baars

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

如果您不介意使用 MariaDB(MySQL 的开源变体,基本相同),MariaDB4j可能是生产环境的完美选择。

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!

MariaDB4j 是 MariaDB 的 Java (!)“启动器”(“MySQL(R) 数据库服务器的“向后兼容、直接替换”,请参阅常见问题解答和维基百科),允许您从Java 没有任何安装/外部依赖项。再次阅读:您不必在系统上安装 MariaDB 二进制文件即可使用 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:

由于它完全不需要任何必须在用户电脑上的要求,因此它可能是嵌入 MySQL 的最佳选择。将不使用嵌入式数据库的项目转换为 MariaDB4j 就像调用一样简单:

DB db = DB.newEmbeddedDB(3306);

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

阅读 github 页面了解更多信息。Maven 中心依赖是:

<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):

您可以将其与最新的驱动程序结合使用,以访问 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.

如果您确实介意使用 MariaDB,另一个选择是Wix Embedded MySQL

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

Wix Embedded MySQL是一个库,它提供了一种在集成测试中运行真正的 MySql 的方法。

Why?

  • Your tests can run on production-like environment: 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;
  • Testing matrix for all supported OSes (x86/x64) and versions (5.5, 5.6, 5.7).

为什么?

  • 您的测试可以在类似生产的环境中运行:匹配版本、编码、时区、数据库/模式/用户设置;
  • 它很容易,比手动安装正确的版本要容易得多;
  • 您可以在没有任何本地设置的情况下为每个项目使用不同的版本/配置;
  • 支持多种平台:Windows、Linux和OSX;
  • 提供不断更新的MySql多个版本——5.5、5.6、5.7;
  • 所有支持的操作系统 (x86/x64) 和版本(5.5、5.6、5.7)的测试矩阵。

回答by zcourts

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/

为了将来给希望嵌入 mysql 的任何人参考,mysql 人员提供了一个实用程序来执行此操作http://downloads.mysql.com/archives/c-mxj/

回答by Steve K

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.

听起来您想要一个嵌入式数据库。虽然 MySQL Connector 看起来不错,但它会启动一个单独的服务器进程。如果您希望数据库服务器在 Java 虚拟机中运行,则有几个用于 Java 的嵌入式数据库。

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

我见过用得最多的两个是:

  1. Apache Derby/ JavaDB
  2. HSQL
  1. Apache Derby/ JavaDB
  2. HSQL