D
Dokploy11mo ago
juanlul

Help needed, mysql2 driver cannot connect to the mysqldatabase (nextjs, drizzle,mysql)

I have project which contains mysql and nextjs app. The mysql container is running fine. When i try to deploy my nextjs it fails on the build because it cannot find the hostname here's the error message: Error: getaddrinfo ENOTFOUND schoolput-database-112c11 at e.createConnection (/app/.next/server/chunks/5516.js:5:60532) at /app/.next/server/chunks/1323.js:6:6804 at t.a (/app/.next/server/webpack-runtime.js:1:918) at 90469 (/app/.next/server/chunks/1323.js:6:6710) at t (/app/.next/server/webpack-runtime.js:1:143) at /app/.next/server/chunks/1323.js:6:12167 at t.a (/app/.next/server/webpack-runtime.js:1:918) at 28695 (/app/.next/server/chunks/1323.js:6:12115) at t (/app/.next/server/webpack-runtime.js:1:143) at /app/.next/server/app/(auth)/signup/page.js:1:3791 { code: 'ENOTFOUND', errno: -3008, sqlState: undefined } and here's my create connection file
import { drizzle } from "drizzle-orm/mysql2";
import * as schema from "./schema/schema";
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});

export const db = drizzle(connection, {
schema,
mode: "default",
logger: true,
});
import { drizzle } from "drizzle-orm/mysql2";
import * as schema from "./schema/schema";
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: process.env.DB_HOST,
port: 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});

export const db = drizzle(connection, {
schema,
mode: "default",
logger: true,
});
here's my env DB_HOST=schoolput-database-112c11 Thanks
No description
3 Replies
Siumauricio
Siumauricio11mo ago
It should work, can you share the envs via DM? I have different projects running with drizzle, check this
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";
import { env } from "@/env";

/**
* Cache the database connection in development. This avoids creating a new connection on every HMR
* update.
*/
const globalForDb = globalThis as unknown as {
conn: postgres.Sql | undefined;
};

const conn = globalForDb.conn ?? postgres(env.DATABASE_URL);
if (env.NODE_ENV !== "production") globalForDb.conn = conn;

export const db = drizzle(conn, { schema });
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";
import { env } from "@/env";

/**
* Cache the database connection in development. This avoids creating a new connection on every HMR
* update.
*/
const globalForDb = globalThis as unknown as {
conn: postgres.Sql | undefined;
};

const conn = globalForDb.conn ?? postgres(env.DATABASE_URL);
if (env.NODE_ENV !== "production") globalForDb.conn = conn;

export const db = drizzle(conn, { schema });
and in Dokploy app I just set this env
DATABASE_URL="postgresql://postgres:s6zSv314MBMOJB99@database-test-dc43be:5432/postgres"
DATABASE_URL="postgresql://postgres:s6zSv314MBMOJB99@database-test-dc43be:5432/postgres"
juanlul
juanlulOP11mo ago
Thank for responding, lemme boot my laptop
Siumauricio
Siumauricio11mo ago
Solverd created an example of T3 Application with mysql database https://github.com/Dokploy/examples/tree/main/t3
GitHub
examples/t3 at main · Dokploy/examples
Examples to deploy on Dokploy. Contribute to Dokploy/examples development by creating an account on GitHub.

Did you find this page helpful?