Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: SSL Connection Support for MySQL (@databases/mysql) #315

Open
juvasquezg opened this issue Jan 24, 2024 · 1 comment
Open

Comments

@juvasquezg
Copy link

Feature Request: SSL Connection Support for MySQL

Description:

I would like to request support for SSL connections in the @databases/mysql package similar to how it is implemented in the mysql2 package. Currently, I need to use SSL connections for secure database connections, and having this feature in @databases/mysql would greatly benefit my use case.

Additional Context:

I am testing the code from the documentation:

const createConnectionPool = require('@databases/mysql');
const { sql } = require('@databases/mysql');

async function run() {
  // N.B. you will need to replace this connection
  // string with the correct string for your database.
  const db = createConnectionPool(
    "mysql://db_user:db_password@db_host:db_port/db_name",
  );

  const results = await db.query(sql`
    SELECT 1 + 1 as result;
  `);

  console.log(results);
  // => [{result: 2}]

  await db.dispose();
}

run().catch((err) => {
  console.error(err);
  process.exit(1);
});

However, I encountered the following error:

{
  uri: 'mysql://db_user:db_password@db_host:db_port/db_name,
  multipleStatements: true,
  timezone: 'local',
  typeCast: [Function: typeCast]
}
{
  maxSize: 10,
  maxUses: Infinity,
  idleTimeoutMilliseconds: 30000,
  queueTimeoutMilliseconds: 60000
}
Ignoring invalid configuration option passed to Connection: schema. This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection
Error: Access denied for user 'db_user'@'db_host' (using password: YES)
...
{
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlState: '28000'
}

Providing the Certificate Authority (CA) in the configuration for mysql2 resolves any SSL-related issues.

```javascript
const mysql = require('mysql2/promise');

async function run() {
  const connection = await mysql.createConnection({
    host: 'db_host',
    user: 'db_user',
    password: 'db_password',
    database: 'db_name',
    port: 'db_port',
    ssl: {
      ca: require('fs').readFileSync('path/to/ca.pem'), // Provide the CA certificate path
    },
  });

  try {
    const [rows, fields] = await connection.execute('SELECT 1 + 1 as result');
    console.log(rows); // => [{result: 2}]
  } finally {
    connection.end();
  }
}

run().catch((err) => {
  console.error(err);
  process.exit(1);
});
@tot-ra
Copy link
Contributor

tot-ra commented Sep 8, 2024

+1 encountered similar issue when trying to connect to DigitalOcean from nodejs

  • it worked fine when I connected from mysql CLI
  • it worked fine if I used test script with nodejs and pure mysql2 connection and full connection object
  • but it did not work with DSN string for some reason

So I wish I could use full mysql configuration object.

Unfortunately I can't do that because there is this check in between

if (!connectionString) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants