Supported container runtimes
MySQL
登录后可跨设备保存划线和私人笔记登录
MySQL
Install
1npm install @testcontainers/mysql --save-devExamples
These examples use the following libraries:
-
1npm install mysql2
Choose an image from the container registry and substitute IMAGE.
Execute a query
1
2
3
4
5
6
7
8
9
10
11
12
13
14await using container = await new MySqlContainer(image).start();
const client = await createConnection({
host: container.getHost(),
port: container.getPort(),
database: container.getDatabase(),
user: container.getUsername(),
password: container.getUserPassword(),
});
const [rows] = await client.execute("SELECT 1 as res");
expect(rows).toEqual([{ res: 1 }]);
await client.end();Execute a query inside the container
1
2
3
4await using container = await new MySqlContainer(image).start();
const result = await container.executeQuery("SELECT 1 as res");
expect(result).toEqual(expect.stringContaining("res\n1\n"));With credentials
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22const username = "testUser";
const password = "testPassword";
const database = "testDB";
await using container = await new MySqlContainer(image)
.withUsername(username)
.withUserPassword(password)
.withDatabase(database)
.start();
expect(container.getConnectionUri()).toEqual(
`mysql://${username}:${password}@${container.getHost()}:${container.getPort()}/${database}`
);
await using rootContainer = await new MySqlContainer(image)
.withRootPassword(password)
.withDatabase(database)
.start();
expect(rootContainer.getConnectionUri(true)).toEqual(
`mysql://root:${password}@${rootContainer.getHost()}:${rootContainer.getPort()}/${database}`
);评论
登录后参与评论
正在加载评论…
KnowForge