1 Star 0 Fork 76

天然喵/disjob

forked from dromara/disjob 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
EmbeddedMysqlServerMariaDB.java 4.74 KB
一键复制 编辑 原始数据 按行查看 历史
ponfee 提交于 2023-11-16 21:56 . rename Throwables method name
/* __________ _____ *\
** \______ \____ _____/ ____\____ ____ Copyright (c) 2017-2023 Ponfee **
** | ___/ _ \ / \ __\/ __ \_/ __ \ http://www.ponfee.cn **
** | | ( <_> ) | \ | \ ___/\ ___/ Apache License Version 2.0 **
** |____| \____/|___| /__| \___ >\___ > http://www.apache.org/licenses/ **
** \/ \/ \/ **
\* */
package cn.ponfee.disjob.test.db;
import ch.vorburger.mariadb4j.DB;
import ch.vorburger.mariadb4j.DBConfiguration;
import ch.vorburger.mariadb4j.DBConfigurationBuilder;
import cn.ponfee.disjob.common.exception.Throwables.ThrowingRunnable;
import cn.ponfee.disjob.common.util.Files;
import cn.ponfee.disjob.common.util.MavenProjects;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.file.PathUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;
import static cn.ponfee.disjob.test.db.DBUtils.*;
/**
* <pre>
* MariaDB Server
* SELECT VERSION() -> 10.2.11-MariaDB
* MacOS如果启动失败报"未找到openssl"错误,参考安装步骤:“/disjob-test/src/main/DB/MariaDB/MariaDB.md”
*
* username: root
* password: 无需密码
*
*
* mysql:
* Maven GAV -> com.mysql:mysql-connector-j:8.0.33
* jdbc-url -> jdbc:mysql://localhost:3306/disjob
* driver-class -> com.mysql.cj.jdbc.Driver
*
* MariaDB:
* Maven GAV -> org.mariadb.jdbc:mariadb-java-client:3.1.4
* jdbc-url -> jdbc:mariadb://localhost:3306/disjob
* driver-class -> org.mariadb.jdbc.Driver
* </pre>
*
* @author Ponfee
*/
public class EmbeddedMysqlServerMariaDB {
public static void main(String[] args) throws Exception {
DB db = start(3306);
Runtime.getRuntime().addShutdownHook(new Thread(ThrowingRunnable.toCaught(db::stop)));
}
public static DB start(int port) throws Exception {
DBConfiguration configuration = DBConfigurationBuilder.newBuilder()
.setPort(port) // OR, default: setPort(0); => autom. detect free port
.setBaseDir(createDirectory("base"))
.setDataDir(createDirectory("data"))
.addArg("--user=root")
.addArg("--character-set-server=utf8mb4")
.addArg("--collation-server=utf8mb4_bin")
//.addArg("--default-character-set=utf8mb4")
//.addArg("--skip-grant-tables") // 默认就是skip-grant-tables
.build();
DB db = DB.newEmbeddedDB(configuration);
System.out.println("Embedded maria db starting...");
db.start();
System.out.println("Embedded maria db started!");
db.source(IOUtils.toInputStream(loadScript(DISJOB_ADMIN_SCRIPT_CLASSPATH), StandardCharsets.UTF_8));
db.source(IOUtils.toInputStream(loadScript(DISJOB_SCRIPT_CLASSPATH), StandardCharsets.UTF_8));
String jdbcUrl = "jdbc:mysql://localhost:" + port + "/" + DB_NAME;
JdbcTemplate jdbcTemplate = DBUtils.createJdbcTemplate(jdbcUrl, USERNAME, PASSWORD);
System.out.println("\n--------------------------------------------------------testDatabase");
DBUtils.testNativeConnection("com.mysql.cj.jdbc.Driver", jdbcUrl, USERNAME, PASSWORD);
System.out.println("\n--------------------------------------------------------testMysql");
DBUtils.testMysqlVersion(jdbcTemplate);
System.out.println("\n--------------------------------------------------------testJdbcTemplate");
DBUtils.testJdbcTemplate(jdbcTemplate);
System.out.println("\n--------------------------------------------------------testQuerySql");
DBUtils.testQuerySchedJob(jdbcTemplate);
return db;
}
private static String loadScript(String scriptPath) throws Exception {
return Arrays.stream(DBUtils.loadScript(scriptPath).split("\n"))
// fix error: The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
.filter(s -> !StringUtils.startsWithAny(s, "DROP USER ", "CREATE USER ", "GRANT ALL PRIVILEGES ON ", "FLUSH PRIVILEGES;"))
.collect(Collectors.joining("\n"));
}
private static String createDirectory(String name) throws IOException {
String dataDir = MavenProjects.getProjectBaseDir() + "/target/mariadb/" + name + "/";
File file = new File(dataDir);
if (file.exists()) {
PathUtils.deleteDirectory(file.toPath());
}
Files.mkdir(file);
return dataDir;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/natural-meow/disjob.git
git@gitee.com:natural-meow/disjob.git
natural-meow
disjob
disjob
master

搜索帮助

Cb406eda 1850385 E526c682 1850385