oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.3k stars 2.78k forks source link

Connections To MySQL Fail Using The mysql2 Package #5855

Closed jabarihunt closed 4 days ago

jabarihunt commented 1 year ago

What version of Bun is running?

What platform is your computer?

Darwin 22.6.0 arm64 arm

What steps can reproduce the bug?

const mysql = require('mysql2');

const getMySQLPool = (credentials) => {

    // SET INITIAL CONFIGURATION PARAMETERS

        let config = {
            user: credentials.username,
            password: credentials.password,
            database: credentials.database,
            connectionLimit: 1,
            waitForConnections: true,
            charset : 'utf8mb4'
        };

    // SET PARAMETERS FOR A SOCKET OR TCP CONNECTION

        if (credentials.socket) {
            config.socketPath = credentials.socket;
        } else {
            config.host = credentials.host;
            config.port = credentials.port;
        }

    // RETURN CONNECTION POOL

        return mysql.createPool(config);

};

const db = getMySQLPool({
    host: "xx.xxx.xxx.xxx",
    port: 3306,
    database: "database_name",
    username: "user",
    password: "xxxxxxxxxxx"
});

const queryDb = async (db, query) => {
    return new Promise(async (resolve) => {
        db.query(query.trim(), (error, results) => {

            if (error) {
                console.error(`DATABASE QUERY ERROR: ${error} | QUERY: ${query}`);
                resolve(null);
                 return;
            }

            resolve(results);

        });
    });
};

const results = await queryDb(db, "SELECT id FROM some_table LIMIT 1");

What is the expected behavior?

  1. A successful connection to the database is made via a connection pool.
  2. The query is executed and a result returned.

What do you see instead?

I have double checked the credentials for both instances. I can connect with my credentials using local GUI tools as well (Beekeper, TablePlus, etc).

Additional information

jabarihunt commented 1 year ago

Began working with Bun ~1.5~ 1.0.5.

balazshevesi commented 10 months ago

bro is living in the future. we're on bun v1.0.22, he's already on v.1.5.

nah but fr, im on the latest verison of bun and mysql2 and im getting the same error. it's strange asf, im migrating a nextjs api to bun (with hono and drizzle orm). mysql2 connects fine when running in nextjs, but not on my dedicated bun backend. i've checked the environment variables, and there's nothing wrong there.

could it be something with the database server (im hosting it on aws rds)? are there any weird sneaky headers that are being included that rds doesn't like or what?

jabarihunt commented 10 months ago

@Balazs-topg bro is living in the future. we're on bun v1.0.22, he's already on v.1.5.

😆 Corrected! Not sure why it's not working for you, maybe it was fixed then broken again in a later update.

andreigoea commented 10 months ago

I'm getting the same issue on latest version whilst trying to use mysql2 in a SvelteKit application. Works fine if I just run it with Node.

balazshevesi commented 10 months ago

i fixed it by moving my database from AWS RDS to railway.app, im still not sure what the actual root cause was though

abdoufma commented 4 months ago

I ran into this problem recently, not only using mysql2 package, but using mysql as well. With mysql2, the connection times out. With mysql it hangs indefinitely when establishing the connection.

MarcoHidalgo commented 4 months ago

having the same issue right here. But at the same time not. Im using bun v1.1.13 and mysql2 to make an api with hono dev. The thing here is its works when i run the whole api, but if i try to run a single ts script, the console show this: image

abdoufma commented 4 months ago

UPDATE : So, I was testing on ubuntu 22.04 / MySQL 8.0.37

But after testing on macOS 14.5 / MySQL 5.7.24 8.3.0, it worked fine:

Screenshot 2024-07-20 at 12 35 10

EDIT : both systems running Bun v1.1.18 EDIT 2: it appears the MySQL server on my macOS system is actually 8.3.0 (for whatever reason mysql -V reported a different version string than SELECT @@version;)

cirospaciari commented 4 days ago

Cannot reproduce this in the currently version of bun if you still facing this issue please reopen.