bun
bun copied to clipboard
Connections To MySQL Fail Using The mysql2 Package
What version of Bun is running?
- 1.0.2+37edd5a6e389265738e89265bcbdf2999cb81a49
- 1.0.3+25e69c71e70ac8a0a88f9cf15b4057bd7b2a633a
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?
- A successful connection to the database is made via a connection pool.
- The query is executed and a result returned.
What do you see instead?
- When connecting to my localhost, the connection times out.
- When connecting to a MySQL instance hosted on GCP Cloud SQL, I receiving the following error:
Error: Access denied for user 'user'@'xx.xxx.xx.xxx' (using password: YES)
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
- mysql2 version 3.6.1
- I forgot to mention, this is within an Express app.
- UPDATE: Also happeing in Bun 1.0.3