I'm attempting to link my Discord bot with a MySQL database that is on another server. However, this example is apparently insecure:
const mysql = require('mysql');
const connection = mysql.createConnection({
host : 'hostname',
port : 'portnum',
user : 'db_user',
password : 'db_user_password',
database : 'db_name',
charset : 'utf8mb4'
});
How would I go about establishing a (more) secure connection?
If its a hosted database server and you have the secured endpoint URL (meaning https:// host) then just use that as the hostname host: 'hostname'
and that should be enough.
If it's a private server that you own and you have the SSL certificates then you can use the following:
const fs = require('fs');
const mysql = require('mysql');
var connection = mysql.createConnection({
host: '127.0.0.1',
port: '3306',
user: 'root',
password: 'passw0rd',
database: 'test',
ssl: {
ca: fs.readFileSync(__dirname + '/certs/ca.pem'),
key: fs.readFileSync(__dirname + '/certs/client-key.pem'),
cert: fs.readFileSync(__dirname + '/certs/client-cert.pem')
}
});
connection.connect();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With