1

所以我一直在使用本地 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); 

在此处输入图像描述

4

1 回答 1

1

最新

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

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

在此处输入图像描述

我的演示目录结构如下。

在此处输入图像描述

配置.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,
                }
            }
          },
      },
  }
  }

测试.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最终生效,这应该是根本原因。

于 2020-06-17T09:24:57.707 回答