MySQL 的 Azure 数据库 - webapp nodejs

Azure Database for MySQL - webapp nodejs

所以我一直在使用本地 mysql workbench 开发网络应用程序。我最近将 MySQL 的数据库移至 Azure 数据库。在我离开本地之前,一切正常。我所有的网页都正常工作,现在 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); 

最新

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

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

我的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生效,这应该是根本原因。