简体   繁体   English

无法使用主目录中的.my.cnf从Node.js客户端连接到MySql

[英]Unable to connect to MySql from Node.js client using .my.cnf in home directory

I have the following in my .my.cnf file in my home directory . 我的主目录.my.cnf文件中包含以下内容。 Actually I will have more entries for other databases too with different suffixes to 'client'. 实际上,对于其他数据库,我也会有更多带有“ client”后缀的条目。 But, for now, to test, I have a simple .my.cnf file as in: 但是,到目前为止,要测试,我有一个简单的.my.cnf文件,如下所示:

   [client]
   database=db1
   user=user1 
   password=user123
   host=localhost

In my Nodejs program, I use the following connection parameters: 在我的Node.js程序中,我使用以下连接参数:

var dbConn  = { 
    user: 'user1',
    database: 'db1'
};

I expect it to connect because the password is in .my.cnf. 我希望它能够连接,因为密码在.my.cnf中。 It does not, gives the following error: 它没有,给出以下错误:

 code: 'ER_ACCESS_DENIED_ERROR',
 errno: 1045,
 sqlMessage: 'Access denied for user \'user1\'@\'localhost\' (using password: NO)',
  sqlState: '28000',

If I put the password in the connection object dbConn, it works. 如果我将密码放入连接对象dbConn中,它将起作用。 If I run mysql command line without specifying password, I am able to connect by using the password in .my.cnf file. 如果我在未指定密码的情况下运行mysql命令行,则可以使用.my.cnf文件中的密码进行连接。

Wait. 等待。 You just wrote the .cnf file separately. 您只是单独编写了.cnf文件。 This wouldn't work. 这行不通。 NodeJS wouldn't pick it up automatically. NodeJS不会自动将其拾取。 You need to use it with mysql connector. 您需要将其与mysql连接器一起使用。 For example: 例如:

var ini = require('node-ini');
var mysql= require('mysql');

var config = ini.parseSync('/path/to/your/.my.cnf');

mysql.createPool({
                user: config.user,
                password: config.password,
                database: 'db1'
});
  1. Source: https://github.com/mysqljs/mysql/issues/1676#issuecomment-289247509 来源: https : //github.com/mysqljs/mysql/issues/1676#issuecomment-289247509
  2. node mysql - https://www.npmjs.com/package/mysql 节点mysql- https://www.npmjs.com/package/mysql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM