Can';t连接到Node.js中的MySQL Docker
在下面的代码中,我通过连接到新数据库并从SQL目录填充数据库来启动mysql Docker。不过,每次运行该函数时,我都会收到一个错误消息,表示服务器已关闭连接。我做错了什么Can';t连接到Node.js中的MySQL Docker,mysql,node.js,docker,Mysql,Node.js,Docker,在下面的代码中,我通过连接到新数据库并从SQL目录填充数据库来启动mysql Docker。不过,每次运行该函数时,我都会收到一个错误消息,表示服务器已关闭连接。我做错了什么 // Spin up a MySQL docker and populate it according to the files in the sql directory import fs from 'fs' import path from 'path' import { createPool } from 'mys
// Spin up a MySQL docker and populate it according to the files in the sql directory
import fs from 'fs'
import path from 'path'
import { createPool } from 'mysql2/promise'
import { GenericContainer, Wait } from 'testcontainers'
const sqlDir = `${__dirname}/../sql`
export const mysqlContainer = async () => {
const container = await new GenericContainer('mysql', '5.7')
.withExposedPorts(3306)
.withEnv('MYSQL_ALLOW_EMPTY_PASSWORD', '1')
.withEnv('MYSQL_DATABASE', 'testdb')
.withWaitStrategy(Wait.forLogMessage('mysqld: ready for connections.'))
.start()
console.log('Container started')
return container
}
export const setupDatabases = async (container) => {
const connection = await createPool({ host: 'localhost', user: 'root', password: '', port: container.getMappedPort(3306) })
console.log('Connected to database')
const dir = await fs.promises.opendir(sqlDir)
for await (const dirent of dir) {
console.log(dirent.name)
if (dirent.name.match(/\.sql$/)) {
const fileContents = await fs.promises.readFile(path.join(sqlDir, dirent.name))
await connection.query(fileContents.toString())
console.log(`process SQL file ${dirent.name}`)
}
}
}
看
原来.withWaitStrategy(Wait.forLogMessage('mysqld:ready for connections'))
导致MySQL服务器在启动后立即关闭
另外,我需要使用container.getContainerIpAddress()
而不是localhost
作为主机
更新草图:
const { createPool } = require('mysql2/promise')
const { GenericContainer, Wait } = require('testcontainers')
const mysqlContainer = async () => {
const container = await new GenericContainer('mysql', '5.7')
.withExposedPorts(3306)
.withEnv('MYSQL_ALLOW_EMPTY_PASSWORD', '1')
.withEnv('MYSQL_DATABASE', 'testdb')
.start()
console.log('Container started')
return container
}
(async () => {
const container = await mysqlContainer()
try {
const connection = await createPool({ host: container.getContainerIpAddress(), user: 'root', password: '', port: container.getMappedPort(3306) })
console.log('Connected to database')
console.log(await (connection.query('SELECT count(*) FROM information_schema.columns')))
} catch (e) {
console.error(e, e.stack)
}
await container.stop()
})()
看
原来.withWaitStrategy(Wait.forLogMessage('mysqld:ready for connections'))
导致MySQL服务器在启动后立即关闭
另外,我需要使用container.getContainerIpAddress()
而不是localhost
作为主机
更新草图:
const { createPool } = require('mysql2/promise')
const { GenericContainer, Wait } = require('testcontainers')
const mysqlContainer = async () => {
const container = await new GenericContainer('mysql', '5.7')
.withExposedPorts(3306)
.withEnv('MYSQL_ALLOW_EMPTY_PASSWORD', '1')
.withEnv('MYSQL_DATABASE', 'testdb')
.start()
console.log('Container started')
return container
}
(async () => {
const container = await mysqlContainer()
try {
const connection = await createPool({ host: container.getContainerIpAddress(), user: 'root', password: '', port: container.getMappedPort(3306) })
console.log('Connected to database')
console.log(await (connection.query('SELECT count(*) FROM information_schema.columns')))
} catch (e) {
console.error(e, e.stack)
}
await container.stop()
})()
您显示的两个函数是什么?您是否收到“容器启动”日志消息?是的,我收到“容器启动”消息。我一直在Jest中这样做,但它甚至在一个独立的脚本中也会发生?您是否收到“容器启动”日志消息?是的,我收到“容器启动”消息。我一直在开玩笑,但它发生在一个独立的脚本。