【问题标题】:Create pool using connection string nodejs mysql使用连接字符串 nodejs mysql 创建池
【发布时间】:2021-06-29 23:46:06
【问题描述】:

我一直在寻找一种使用 mysql 连接字符串来创建池而不是使用凭据的方法,这样我就可以使用 azure 应用程序设置中的连接字符串,但到目前为止还没有找到解决方案。

我可以这样创建一个池:

pool = mysql.createPool({
    connectionLimit: 20,
    host: HOST,
    user: USER,
    password: PASSWORD,
    database: DATABASE,
    debug: false
});

但我想要实现的是这样的:

pool = mysql.createPool(process.env.MYSQLCONNSTR_connectionstring);

我正在使用模块felixge/node-mysql

【问题讨论】:

    标签: mysql node.js azure


    【解决方案1】:

    我正在尝试使用 azure mysql db 提供的连接字符串,其格式为:

    Database=DATABASE;Data Source=HOST;User Id=USERID;Password=PASSWORD
    

    但事实证明这种格式不受支持,当我使用它时它可以工作:

    mysql://USERID:PASSWORD@HOST/DATABASE
    

    【讨论】:

      【解决方案2】:

      基于node-mysql的来源,遗憾的是没有办法用连接字符串创建池。

      参考:PoolConfig.js

      我认为您应该解析连接字符串,其中包含所有必要的信息。

      例如:

      let connectionString = 'mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700';
      
      // remove mysql://
      connectionString = connectionString.substring(8, connectionString.length)
      
      // Get user and password
      let userAndPassw = connectionString.split('@')[0];
      let user = userAndPassw.split(':')[0];
      let password = userAndPassw.split(':')[1];
      
      // Host
      let host = connectionString.split('@')[1];
      host = host.split('?')[0];
      

      【讨论】:

      • 感谢您的建议,我也想过这样做,但事实证明可以使用连接字符串创建一个池,但格式与我尝试过的不同。跨度>
      【解决方案3】:

      由于答案没有写清楚,我想澄清一下。我在查询字符串中指定了 connectionLimit 值,现在它可以工作了:

      let pool = mysql.createPool('mysql://xxx@xxx/xxx?charset=utf8mb4&connectionLimit=10');
      

      【讨论】:

        猜你喜欢
        • 2016-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-08
        • 2017-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多