-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathEmbeddedMysqlServerMariaDB.java
117 lines (99 loc) · 4.59 KB
/
EmbeddedMysqlServerMariaDB.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright 2022-2024 Ponfee (http://www.ponfee.cn/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
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);
}
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...");
Runtime.getRuntime().addShutdownHook(new Thread(ThrowingRunnable.toCaught(db::stop)));
db.start();
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);
System.out.println("Embedded maria db started!");
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;
Files.cleanOrMakeDir(dataDir);
return dataDir;
}
}