I made a windows desktop application using electron. I connected my program to mysql. However, in order to connect to the database, I had to specify the account credentials such as username, password, host, port, and database name. I read through mysql official website concerning security, turns out it is more secure to store these credentials in an encrypted option file (mylogin.cnf). So, I actually did that using mysql_config_editor and created the file with the necessary credentials.
However, when I try to connect to the database from javascript using that file, it generates an error. Here is the code:
const mysql = require('mysql2');
const connection = mysql.createConnection({
loginPath: 'mylogin.cnf'
});
function setupDatabase() {
connection.connect((err) => {
if (err) {
console.error('Error connecting to database:', err);
return;
}
console.log('Connected to the database');
});
// Additional database setup code...
};
it gave me this error:
Ignoring invalid configuration option passed to Connection: loginPath. 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 connecting to database: Error: Access denied for user ”@’localhost’ (using password: NO)
at Packet.asError (C:UsersLenovoDesktopVisual StudioNCDProjectnode_modulesmysql2libpacketspacket.js:728:17)
at ClientHandshake.execute (C:UsersLenovoDesktopVisual StudioNCDProjectnode_modulesmysql2libcommandscommand.js:29:26)
at Connection.handlePacket (C:UsersLenovoDesktopVisual StudioNCDProjectnode_modulesmysql2libconnection.js:478:34)
at PacketParser.onPacket (C:UsersLenovoDesktopVisual StudioNCDProjectnode_modulesmysql2libconnection.js:97:12)
at PacketParser.executeStart (C:UsersLenovoDesktopVisual StudioNCDProjectnode_modulesmysql2libpacket_parser.js:75:16)
at Socket. (C:UsersLenovoDesktopVisual StudioNCDProjectnode_modulesmysql2libconnection.js:104:25)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.push (node:internal/streams/readable:234:10) {
code: ‘ER_ACCESS_DENIED_ERROR’,
errno: 1045,
sqlState: ‘28000’,
sqlMessage: "Access denied for user ”@’localhost’ (using password: NO)",
sql: undefined
}
note: the function setupDatabase() is called when the program is started
I would really appreciate it if someone can help, thank you!
2
Answers
Nothing in the documentation for mysql2 suggests that it supports a
loginPath
parameter or encrypted option files.Don’t assume that MySQL’s documentation about client programs applies equally to client programs not written by the MySQL team.
You can use eletron-store and MySQL credntials.