yarn add @fastify/mysql
import Fastify from "fastify";
import db from "@fastify/mysql";
const fastify = Fastify({
logger: false,
});
// db
fastify.register(db, {
promise: true,
connectionString: "mysql://root@localhost/mysql",
});
// 연결 확인
fastify.get("/test", async (request, reply) => {
const connection = await fastify.mysql.getConnection();
const result = await connection.query(`SELECT * FROM age`);
connection.release();
return result;
});
const start = async () => {
try {
await fastify.listen({ port: 5000, host: "0.0.0.0" });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();
connectionString: "mysql://유저이름:db비밀번호@127.0.0.1/db이름",
* localhost 사용시 app crash에러가 발생하여 127.0.0.1로 변경해주니 해결됨
🌼 참고