如何将我的应用程序与oracle云基础设施上的MySQL数据库作为服务连接



我想将处于开发阶段的web应用程序连接到MySQL数据库,作为oracle云基础设施中的一项服务。目前,我已经能够将MySQL数据库配置为一项服务,并且能够通过服务器CLI以及安装在远程电脑上的Workbench连接到它,并从CLI创建和查询数据库。数据库更改(新数据库、表创建、更新(在Workbench以及我用来连接到数据库服务的任何其他远程环境中表示。

因此,我相信数据库的部署是正确的。当我试图从任何应用程序中连接到MySQL DBS时,我现在面临的问题是,我遇到了一些连接超时错误,无法从代码中连接到它。我是新手,我不明白这背后的原因。我花了好几天的时间来解决这个问题。如有任何帮助,我们将不胜感激。

我正在使用Sequelize ORM和mysql2

下面是我的config.json文件,用于连接远程部署的数据库:

{
"development": {
"username": "johndoe",
"host": "10.0.1.3",
"dialect": "mysql",
"password": "Oraclecloud1@",
"database": "test"
},

host是MySQL在远程130.61.183.23实例地址上运行的地方。

Server.js文件,用于在我的机器上设置本地节点js服务器以运行应用程序

const express=require('express')
const app=express()
app.use(express.json()) 
app.use(express.urlencoded({extended:true}))
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); 
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header('Access-Control-Allow-Methods', "POST, GET, PUT, DELETE, OPTIONS");
next();
});
var models=require('./models')
models.sequelize.sync().then(function(){
console.log('Database looks fine')
}).catch(function(err){
console.log(err,'Something went wrong with the db')
})
require('./routers/index')(app)
port=process.env.PORT || 3003
app.listen(port,console.log('server up'))

模型中的Index.js文件

'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;

下面是我得到的超时错误

ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT    at ConnectionManager.connect (D:NotesNodeservertestnode_modulessequelizelibdialectsmysqlconnection-manager.js:126:17)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async ConnectionManager._connect (D:NotesNodeservertestnode_modulessequelizelibdialectsabstractconnection-manager.js:318:24)
at async D:NotesNodeservertestnode_modulessequelizelibdialectsabstractconnection-manager.js:250:32
at async ConnectionManager.getConnection (D:NotesNodeservertestnode_modulessequelizelibdialectsabstractconnection-manager.js:280:7)
at async D:NotesNodeservertestnode_modulessequelizelibsequelize.js:613:26
at async MySQLQueryInterface.createTable (D:NotesNodeservertestnode_modulessequelizelibdialectsabstractquery-interface.js:225:12)
at async Function.sync (D:NotesNodeservertestnode_modulessequelizelibmodel.js:1300:5)
at async Sequelize.sync (D:NotesNodeservertestnode_modulessequelizelibsequelize.js:793:35) {
parent: Error: connect ETIMEDOUT
at Connection._handleTimeoutError (D:NotesNodeservertestnode_modulesmysql2libconnection.js:178:17)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
},
original: Error: connect ETIMEDOUT
at Connection._handleTimeoutError (D:NotesNodeservertestnode_modulesmysql2libconnection.js:178:17)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7) {
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true
}
} Something went wrong with the db

仔细观察,可能会发生一些事情,包括数据库没有运行或防火墙不允许访问。我看到的另一件事包括在配置中添加一个池选项——Node.js应用程序中的SequelizeConnectionError

相关内容

  • 没有找到相关文章

最新更新