Connection failed, fetch failed.

I am trying to use drizzle (with planetscale drivers) to connect to a local mysql server but i consistantly get errors like HANDSHAKE_NO_SSL_SUPPORT & ERR_SSL_WRONG_VERSION_NUMBER
⨯ TypeError: fetch failed
at async login (./src/lib/auth/actions.ts:64:26)
Cause: [Error: 405CB710AC720000:error:0A00010B:SSL routines:tls_validate_record_header:wrong version number:ssl/record/methods/tlsany_meth.c:80:
] {
library: 'SSL routines',
reason: 'wrong version number',
code: 'ERR_SSL_WRONG_VERSION_NUMBER'
}
⨯ TypeError: fetch failed
at async login (./src/lib/auth/actions.ts:64:26)
Cause: [Error: 405CB710AC720000:error:0A00010B:SSL routines:tls_validate_record_header:wrong version number:ssl/record/methods/tlsany_meth.c:80:
] {
library: 'SSL routines',
reason: 'wrong version number',
code: 'ERR_SSL_WRONG_VERSION_NUMBER'
}
Do note that this issue persists no matter what SQL db i use. MariaDB, Mysql, Sqlite, Sqlite3 doesn't matter. I hate discords new formatting.
23 Replies
Mark
MarkOP10mo ago
This is my drizzle configuration.
import { type Config } from "drizzle-kit";

import { env } from "@/env";
import { DATABASE_PREFIX } from "@/lib/constants";

export default {
schema: "./src/server/db/schema.ts",
out: "./drizzle",
driver: "mysql2",
dbCredentials: {
connectionString: env.DATABASE_URL,
},
tablesFilter: [`${DATABASE_PREFIX}_*`],
} satisfies Config;
import { type Config } from "drizzle-kit";

import { env } from "@/env";
import { DATABASE_PREFIX } from "@/lib/constants";

export default {
schema: "./src/server/db/schema.ts",
out: "./drizzle",
driver: "mysql2",
dbCredentials: {
connectionString: env.DATABASE_URL,
},
tablesFilter: [`${DATABASE_PREFIX}_*`],
} satisfies Config;
Connection looks like this.
import { drizzle } from "drizzle-orm/planetscale-serverless";
import { Connection } from "@planetscale/database";

import { env } from "@/env";
import * as schema from "./schema";

export const connection = new Connection({ url: env.DATABASE_URL });

export const db = drizzle(connection, { schema });
import { drizzle } from "drizzle-orm/planetscale-serverless";
import { Connection } from "@planetscale/database";

import { env } from "@/env";
import * as schema from "./schema";

export const connection = new Connection({ url: env.DATABASE_URL });

export const db = drizzle(connection, { schema });
And the conncetionstring looks like this. mysql://MariaInterface:MARIADB@localhost:3306/lucia?ssl={"rejectUnauthorized":false}
Mykhailo
Mykhailo10mo ago
Hello, @Mark! I did research and I think this issue is not related to drizzle. It might be with your server setup. https://github.com/lbdremy/solr-node-client/issues/362
GitHub
{ library: 'SSL routines', function: 'ssl3_get_record', reaso...
I am using solr 8.11. behingd a nginx server at digitalocean on ubuntu 20.00. trying to run the code: // Use var solr = require('solr-client') in your code var solr = require('solr-clie...
Mark
MarkOP10mo ago
Weird, this shouldn't matter though, unless drizzle or the db for some reason wants a TLS / HTTPS connection. I have previously been able to use drizzle in other projects for DB access without issues, nor this TLS issue. I got kind of confused with that issue, are they implying that the mysql (MariaDB in this case) server got incorrectly set up? I combed through the project, there is no mentions of HTTPS / TLS connections in the code, i am unsure what the cause is anymore.
Mykhailo
Mykhailo10mo ago
Am I right that you are trying to connect to MariaDB using planetscale drivers?
Mark
MarkOP10mo ago
Yes. The example project i use has a planetscale config.
Mykhailo
Mykhailo10mo ago
Honestly, I am not familiar with MariaDB, but as far as I know, drizzle doesn't support MariaDB now. https://github.com/drizzle-team/drizzle-orm/issues/203
Mark
MarkOP10mo ago
MariaDB and Mysql are pretty similar, MariaDB has all the stuff that mysql has for dependancy reasons. Either way that doesn't matter as stated in my first message. I've used Mysql, Maria, sqlite, no difference. I looked for issues about this for both maria and mysql, both turned up nearly nothing. I saw a post that said to use Host, username and password in the connect code. However that turned up nothing, it works sure (No errors about the code itself) but the SSL issues still occur.
Mykhailo
Mykhailo10mo ago
Ok, did you test with other drivers? For example mysql2?
Mark
MarkOP10mo ago
With drizzle db:push drizzle-kit push:mysql I get the error "HANDSHAKE_NO_SUPPORT" which is unlikely to happen in the first place. There is SSL support but is likely a bug with something else. When i run SHOW VARIABLES WHERE "%SSL%"; I get this:
+---------------------+---------------------------+
| Variable_name | Value |
+---------------------+---------------------------+
| have_openssl | YES |
| have_ssl | DISABLED |
| ssl_ca | |
| ssl_capath | |
| ssl_cert | |
| ssl_cipher | |
| ssl_crl | |
| ssl_crlpath | |
| ssl_key | |
| version_ssl_library | OpenSSL 3.2.1 30 Jan 2024 |
+---------------------+---------------------------+
+---------------------+---------------------------+
| Variable_name | Value |
+---------------------+---------------------------+
| have_openssl | YES |
| have_ssl | DISABLED |
| ssl_ca | |
| ssl_capath | |
| ssl_cert | |
| ssl_cipher | |
| ssl_crl | |
| ssl_crlpath | |
| ssl_key | |
| version_ssl_library | OpenSSL 3.2.1 30 Jan 2024 |
+---------------------+---------------------------+
Give me a sec, i will answer your question in a bit Yes, i have tried mysql2. that is how i get the HANDSHAKE_NO_SUPPORT error.
import mysql from "mysql2/promise";
import { drizzle } from "drizzle-orm/mysql2";
import { migrate } from "drizzle-orm/mysql2/migrator";

import { env } from "@/env";
import * as schema from "./schema";

export async function runMigrate() {
const connection = await mysql.createConnection(env.DATABASE_URL);
const db = drizzle(connection, { schema, mode: "planetscale" });

console.log("⏳ Running migrations...");

const start = Date.now();

await migrate(db, { migrationsFolder: "drizzle" });

const end = Date.now();

console.log(`✅ Migrations completed in ${end - start}ms`);

process.exit(0);
}

runMigrate().catch((err) => {
console.error("❌ Migration failed");
console.error(err);
process.exit(1);
});
import mysql from "mysql2/promise";
import { drizzle } from "drizzle-orm/mysql2";
import { migrate } from "drizzle-orm/mysql2/migrator";

import { env } from "@/env";
import * as schema from "./schema";

export async function runMigrate() {
const connection = await mysql.createConnection(env.DATABASE_URL);
const db = drizzle(connection, { schema, mode: "planetscale" });

console.log("⏳ Running migrations...");

const start = Date.now();

await migrate(db, { migrationsFolder: "drizzle" });

const end = Date.now();

console.log(`✅ Migrations completed in ${end - start}ms`);

process.exit(0);
}

runMigrate().catch((err) => {
console.error("❌ Migration failed");
console.error(err);
process.exit(1);
});
I am not 100% sure if the "have_ssl" can be a cause, but i would assume so. I have used a db explorer like dbeaver and it connects without any issues without TLS, SSL or anything like that.
Mykhailo
Mykhailo10mo ago
interesting, so you have this issue will all db (MySQL,SQLite)
Mark
MarkOP10mo ago
Yes. sqlite i am a bit unsure of, i will have to check if i actually did use it. It will be weird if that one woud fail considering its not meant to really be on the web due to the fact that you cannot secure it with user auth (at least to my knowledge) Ima see if i can change the use_ssl to yes and if it resolves anything.
Mykhailo
Mykhailo10mo ago
I will try to investigate tomorrow, I haven’t faced this issue before
Mark
MarkOP10mo ago
It doesn't seem to be very common.
Mark
MarkOP10mo ago
Okay, moment of truth.
No description
Mark
MarkOP10mo ago
Nope, same issue. Wait hold on, i am getting a breakthrough. wait so. i go back to:
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: "host",
user: "user",
database: "database",
...
});

const db = drizzle(connection);
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: "host",
user: "user",
database: "database",
...
});

const db = drizzle(connection);
It kind of semi works but it complains about the await? ⨯ SyntaxError: await is only valid in async functions and the top level bodies of modules at (action-browser)/./src/server/db/index.ts (/home/mark/next-lucia-auth/.next/server/app/(auth)/login/page.js:893:1) at __webpack_require__ (/home/mark/next-lucia-auth/.next/server/webpack-runtime.js:33:42) at eval (./src/lib/auth/index.ts:10:68) at (action-browser)/./src/lib/auth/index.ts (/home/mark/next-lucia-auth/.next/server/app/(auth)/login/page.js:772:1) at __webpack_require__ (/home/mark/next-lucia-auth/.next/server/webpack-runtime.js:33:42) at eval (./src/lib/auth/actions.ts:21:67) at (action-browser)/./src/lib/auth/actions.ts (/home/mark/next-lucia-auth/.next/server/app/(auth)/login/page.js:761:1) at Function.__webpack_require__ (/home/mark/next-lucia-auth/.next/server/webpack-runtime.js:33:42) printing the connection results in this.
Connection {
session: null,
config: {
url: 'mysql://MariaInterface:[email protected]:3306/lucia?ssl=strict',
fetch: [AsyncFunction (anonymous)] {
__nextGetStaticStore: [Function (anonymous)],
__nextPatched: true
},
username: 'MariaInterface',
password: 'MARIADB',
host: '127.0.0.1'
},
url: 'https://127.0.0.1:3306/lucia'
}
Connection {
session: null,
config: {
url: 'mysql://MariaInterface:[email protected]:3306/lucia?ssl=strict',
fetch: [AsyncFunction (anonymous)] {
__nextGetStaticStore: [Function (anonymous)],
__nextPatched: true
},
username: 'MariaInterface',
password: 'MARIADB',
host: '127.0.0.1'
},
url: 'https://127.0.0.1:3306/lucia'
}
So it seems to be valid, problem is that still says wrong version number. Seems like drizzle gets all of the required info like username, password and so on from the conncetionurl, which is nice to know but confusing on why it fails.
Mykhailo
Mykhailo10mo ago
so, you get the same error or it`s something new?
Mark
MarkOP10mo ago
This gives a new error. But it seems to connect properly, it gives query is not a valid function despite it being one. (That is if you fix the await problems)
Mykhailo
Mykhailo10mo ago
Can you show the query please?
Mark
MarkOP10mo ago
Give me a minute, i just got on my computer.
Unhandled Runtime Error

Error: Cannot read properties of undefined (reading 'users')
Unhandled Runtime Error

Error: Cannot read properties of undefined (reading 'users')
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

import { env } from "@/env";
import * as schema from "./schema";

async function connect() {
const connection = await mysql.createConnection({
host: "127.0.0.1",
user: "MariaInterface",
password: "MARIADB",
port: 3306,
database: "lucia",
});
return drizzle(connection, {schema, mode: "planetscale"})
}

export const db = connect();
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

import { env } from "@/env";
import * as schema from "./schema";

async function connect() {
const connection = await mysql.createConnection({
host: "127.0.0.1",
user: "MariaInterface",
password: "MARIADB",
port: 3306,
database: "lucia",
});
return drizzle(connection, {schema, mode: "planetscale"})
}

export const db = connect();
const { email, password } = parsed.data;

const existingUser = await db.query.users.findFirst({
where: (table, { eq }) => eq(table.email, email),
});

if (!existingUser) {
return {
formError: "Incorrect email or password",
};
}
const { email, password } = parsed.data;

const existingUser = await db.query.users.findFirst({
where: (table, { eq }) => eq(table.email, email),
});

if (!existingUser) {
return {
formError: "Incorrect email or password",
};
}
Property 'query' does not exist on type 'Promise<MySql2Database<typeof import("/home/mark/next-lucia-auth/src/server/db/schema")>>'.ts(2339)
actions.ts(57, 33): Did you forget to use 'await'?
Property 'query' does not exist on type 'Promise<MySql2Database<typeof import("/home/mark/next-lucia-auth/src/server/db/schema")>>'.ts(2339)
actions.ts(57, 33): Did you forget to use 'await'?
Vscode's intellisense.
Mykhailo
Mykhailo10mo ago
try this
export const db = await connect(); // + await
export const db = await connect(); // + await
Mark
MarkOP10mo ago
will be surprising if it wont start complaining about async functions Same error Wait no, it gave a different error that was resolved with a restart of the project that later went to
Unhandled Runtime Error

Error: await is only valid in async functions and the top level bodies of modules
Unhandled Runtime Error

Error: await is only valid in async functions and the top level bodies of modules
Mykhailo
Mykhailo10mo ago
you should rewrite your connect function or use await connect() in async function. Because db has type Promise<...>
Mark
MarkOP10mo ago
Interesting It's kinda annoying though because now i have look through all of my imports and and initialize an awaited db. so i have to look all over the place Regardless that seems to be a fix, although an annoying one because i just have to expect the code to error somewhere sometime about db. I don't think we can return an awaited db type, the code will just complain, thanks for your help! 😁
Want results from more Discord servers?
Add your server