1

So I have been working on a web app with local mysql workbench. I recently moved the database to Azure Database for MySQL. Everything was working properly before I moved away from local. All my webpages were working properly, now only 2 out of the 4 pages work and I am running into the error below when I click on the broken pages. Below is how I am connecting to the database, I am not sure if the second connection is working.

Do I need to do something like this?
https://learn.microsoft.com/en-us/azure/mysql/howto-configure-ssl

Thank you for any help!

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
    });

enter image description here enter image description here enter image description here enter image description here

EDIT HERE ------------------------

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); 

enter image description here

williswin
  • 133
  • 13

1 Answers1

1

Newest

I suggest you use sequelize to connect multiple databases. Only need to configure the database in config.js. Not the way you used 7003 and 7004 to define multiple databases.

My sample code demonstrates connecting two databases in mysql and sqlsever.The following figure is the operation result. It just demo code, if you want to use it in your project, you need learn it.

enter image description here

The structure of my demo directory is as follows.

enter image description here

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 })
   }
}

PRIVIOUS

I think the root cause is that when you publish your webapp to azure, there is only one port that table_routes_1and2.js finally listens to, http_80 and https_443, there should be only one db that finally takes effect, this should be the root cause.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • I am running into this error now: Error: UNKNOWN_CODE_PLEASE_REPORT: SSL connection is required. Please specify SSL options and retry. I followed the article above. – williswin Jun 17 '20 at 15:27
  • @williswin Ok, I have updated my answer and share my demo in github, you can download it. – Jason Pan Jun 18 '20 at 01:19
  • Thanks for the help. So one of my webpage works and when I click on another this error occurs: This site can’t be reached127.0.0.1 refused to connect. Try: Checking the connection Checking the proxy and the firewall ERR_CONNECTION_REFUSED If I disable SSL requirement on azure database for mysql, then it works. – williswin Jun 18 '20 at 18:20
  • @williswin You're welcome, so now this problem is all solved? If there is anything else that needs help, I am happy to help. – Jason Pan Jun 19 '20 at 01:18
  • @williswin The root cause of this problem is because the settings of mysql in the portal, if one of them is valid, it is recommended that the mysql settings of the two applications be configured to be the same. – Jason Pan Jun 19 '20 at 01:20
  • i am using Azure Database for MySQL server, where do I make changes on the portal? – williswin Jun 19 '20 at 14:36
  • @williswin Make sure that each of your mysql server configurations are the same and are available. Then your problem should be solved. – Jason Pan Jun 22 '20 at 07:50
  • Hey Jason, I have the same settings on the server. Only a few of my pages work while the others don't, they are all coming from this same database though. Yup I will, thanks! – williswin Jun 22 '20 at 14:11
  • @williswin Are those pages random problems? If possible, it is recommended to put the source code of the problematic page in the post to facilitate the root cause. – Jason Pan Jun 22 '20 at 14:21
  • The problem only occurs when i enforce ssl connection, if I turn it off, they all run properly. If I turn it on, 2 out of 4 of my pages work while the other 2 errors out - This site can’t be reached 127.0.0.1 refused to connect. Try: Checking the connection Checking the proxy and the firewall ERR_CONNECTION_REFUSED – williswin Jun 22 '20 at 14:24
  • 1
    @williswin I just read your code carefully, and I think the problem should be on `7003` and `7004`. You should not manage your db connections like this. – Jason Pan Jun 26 '20 at 01:38
  • 1
    @williswin You can try Sequelize. https://medium.com/unetiq/using-multiple-databases-with-nodejs-and-sequelize-59e0abcbbe6f – Jason Pan Jun 26 '20 at 01:39
  • 1
    @williswin I think the root cause is that when you publish your webapp to azure, there is only one port that `table_routes_1and2.js` finally listens to, http_80 and https_443, there should be only one db that finally takes effect, this should be the root cause. – Jason Pan Jun 26 '20 at 01:42
  • If only one db takes effect, do I need to create a second SQL server? I won't be able to connect multiple schemas - even though it works when SSL is disabled? – williswin Jun 26 '20 at 04:25
  • @williswin You only need to define the database configuration in config.json. When your business needs to use it, just use below code. `const Sequelize = require('Sequelize');const userDb = new Sequelize(/* ... */);const contentDb = new Sequelize(/* ... */); `. – Jason Pan Jun 26 '20 at 05:22
  • @williswin You can carefully read the link URL I provided to you, which is very useful to you. Encounter other problems that need help, I will be happy to continue to help you. – Jason Pan Jun 26 '20 at 07:26
  • @williswin Okey, I will upload my demo code tomorrow. It just a demo, if you want use it in your program, you need study it. I have use it in `koa` project priviously. – Jason Pan Jun 29 '20 at 13:08
  • @williswin https://github.com/Jason446620/NodeMultiSqlInstanceConn – Jason Pan Jun 30 '20 at 01:30