Azure Database for MySQL - webapp nodejs

Posted

技术标签:

【中文标题】Azure Database for MySQL - webapp nodejs【英文标题】: 【发布时间】:2020-10-05 13:51:22 【问题描述】:

所以我一直在使用本地 mysql 工作台开发一个 Web 应用程序。我最近将数据库移至 Azure Database for MySQL。在我离开当地之前,一切正常。我所有的网页都可以正常工作,现在 4 个页面中只有 2 个可以工作,当我点击损坏的页面时,我遇到了下面的错误。下面是我连接数据库的方式,我不确定第二个连接是否正常。

我需要这样做吗?https://docs.microsoft.com/en-us/azure/mysql/howto-configure-ssl

感谢您的帮助!

var connection = mysql.createConnection(
       host: 'host',
       user: 'user',
       password: "password",
       database: 'schema_1',
       ssl: true
    );
var connection = mysql.createConnection(
       host: 'host',
       user: 'user',
       password: "password",
       database: 'schema_2',
       ssl: true
    );

在这里编辑------------

table_routes.js

var express = require('express')
     , http = require('http')
     , mysql = require('mysql'); // <---- HERE
   
    var app = express();
    const fs = require('fs');
    const path = require('path');
   
    
   
    app.use(function(req, res, next) 
       res.header("Access-Control-Allow-Origin", "http://127.0.0.1:3000");
       res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
       next();
     );
   
    var connection = mysql.createConnection(
       host: 'host',
       user: 'root',
       password: "password",
       database: 'db1',
       ssl: 
         ca: fs.readFileSync(path.resolve(__dirname, 'BaltimoreCyberTrustRoot.crt.pem'))
      
      
    );

connection.connect(); // <---- AND HERE
   
    // all environments
    app.set('port', process.env.PORT || 7003);

table_routes2.js

var express = require('express')
     , http = require('http')
     , mysql = require('mysql'); // <---- HERE
   
    var app = express();
    const fs = require('fs');
    const path = require('path');
   
    
   
    app.use(function(req, res, next) 
       res.header("Access-Control-Allow-Origin", "http://127.0.0.1:3000");
       res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
       next();
     );
   
    var connection = mysql.createConnection(
       host: 'host',
       user: 'user',
       password: "password",
       database: 'db2',
       ssl: 
         ca: fs.readFileSync(path.resolve(__dirname, 'BaltimoreCyberTrustRoot.crt.pem'))
       
    );
   
    connection.connect(); // <---- AND HERE
   
    // all environments
    app.set('port', process.env.PORT || 7004); 

【问题讨论】:

【参考方案1】:

最新

我建议你使用 sequelize 连接多个数据库。只需要在config.js中配置数据库。不是您使用 7003 和 7004 定义多个数据库的方式。

我的示例代码演示了在mysql和sqlsever中连接两个数据库。下图是运行结果。它只是演示代码,如果你想在你的项目中使用它,你需要学习它。

我的demo目录结构如下。

config.js

const fs = require('fs');
const path = require('path');

module.exports = 

/**Declaration of databases for my development environment**/
  "development": 
      "databases": 
          "dbinmysql": 
              "database": "mysql", //you should always save these values in environment variables
              "username": "***@p***mysql",  //only for testing purposes you can also define the values here
              "password":  "Ja***",
              "host": "*****mysql.mysql.database.azure.com",
              "port": 3306,
              "ssl":true,
              "dialect": "mysql",  //here you need to define the dialect of your databse, in my case it is Postgres
              "dialectOptions": 
                ssl: 
                    ca: fs.readFileSync(path.resolve(__dirname, 'BaltimoreCyberTrustRoot.crt.pem'))
                  
              ,
          ,
          "dbinsqlserver": 
              "database": "pa*****db", 
              "username": "pa***i",  
              "password":  "J*****0",
              "host": "***sqlserver.database.windows.net",
              "port": 1433,
              "dialect": "mssql",  //second database can have a different dialect
              "dialectOptions": 
                options: 
                    encrypt: true,
                
            
          ,
      ,
  
  

test.js

const Sequelize = require('sequelize');
const env = process.env.NODE_ENV || 'development';
const  QueryTypes  = require('sequelize');

//Load the configuration from the config.js
const config = require(`./config.js`)[env];

//Create an empty object which can store our databases
const db = ;

//Extract the database information into an array
const databases = Object.keys(config.databases);

//Loop over the array and create a new Sequelize instance for every database from config.js
for(let i = 0; i < databases.length; ++i) 
    let database = databases[i];
    let dbPath = config.databases[database];
    console.log("try to connecting "+database);
    //Store the database connection in our db object
    db[database] = new Sequelize( dbPath.database, dbPath.username, dbPath.password, dbPath );
    if (database == 'dbinmysql') 
        const results = db[database].query("SELECT *FROM USER",  type: QueryTypes.SELECT )
     else if (database == 'dbinsqlserver') 
        const results = db[database].query("SELECT *FROM TEST",  type: QueryTypes.SELECT )
   

私人

我认为根本原因是当你将你的webapp发布到azure时,table_routes_1and2.js最终监听的端口只有一个,http_80和https_443,应该只有一个db最终生效,这应该是根本原因。

【讨论】:

我现在遇到了这个错误:错误:UNKNOWN_CODE_PLEASE_REPORT:需要 SSL 连接。请指定 SSL 选项并重试。我按照上面的文章。 @williswin 好的,我已经更新了我的答案并在github上分享了我的演示,你可以下载它。 @williswin 我刚刚仔细阅读了您的代码,我认为问题应该出在70037004 上。你不应该这样管理你的数据库连接。 @williswin 你可以试试 Sequelize。 medium.com/unetiq/… @williswin 我认为根本原因是当您将 webapp 发布到 azure 时,table_routes_1and2.js 最终只监听一个端口,http_80 和 https_443,最终应该只有一个 db生效了,这应该是根本原因。

以上是关于Azure Database for MySQL - webapp nodejs的主要内容,如果未能解决你的问题,请参考以下文章

迁移到 Azure App Service 和 Azure Database for MySQL 灵活服务器后,Wordpress 网站速度极慢

从 Ubuntu 20.04 连接到“Azure Database for MySQL-Server”

MySQL Database on Azure 支持 5.7 版本啦!

MySQL Database on Azure服务在中国正式商用

如何将 MySQL 数据库转移到 SQL Server 与 Azure SQL Database

Create a database in mysql for mac