ER_ACCESS_DENIED_ERROR:当我尝试连接到远程服务器上的数据库时-Node.js [英] ER_ACCESS_DENIED_ERROR: When i try to connect to the database on remote server - Node.js

查看:595
本文介绍了ER_ACCESS_DENIED_ERROR:当我尝试连接到远程服务器上的数据库时-Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试连接到远程服务器上的数据库时遇到问题。



我的代码:

  const mysql = require('mysql'); 
const数据库= mysql.createPool({
主机:'localhost',
用户:'user',
密码:'pass',
数据库:'db '
});

database.getConnection(function(err,connection){
if(!err){
console.log('数据库已连接...');
} else {
console.log('连接数据库时出错...');
}
});

用于连接代码的密码是伪造的。使用正确的凭据,我在要连接的日期数据库上成功登录了远程服务器上的phpMyAdmin。凭据很好。



运行脚本时,返回此错误:


I have issue when i try to connect to the database on remote server.

My code:

const mysql = require('mysql');
const database = mysql.createPool({
    host: 'localhost',
    user: 'user',
    password: 'pass',
    database: 'db'
});

database.getConnection(function (err, connection) {
    if (!err) {
        console.log('Database is connected ...');
    } else {
        console.log('Error connecting database ...');
    }
});

The credentials for connection in code is faked. With the right credentials I have, I login successfully on phpMyAdmin on remote server, on datebase that I want to connect. Credentials is good.

When I run script, return this error: view error

Also, when I input credentials for connection with my local database, everything work perfect.

解决方案

As pointed out by Luuk, you need to replace the localhost with the actual IP address of the remote database server and the port on which the database server is running.

For example -

const database = mysql.createPool({
    host: '123.234.121.234',
    port : '3306',
    user: 'user',
    password: 'pass',
    database: 'db'
}); 

Also, make sure the port is whitelisted and can be accessed over the network. Heres a tiny little diagram for explanation.

这篇关于ER_ACCESS_DENIED_ERROR:当我尝试连接到远程服务器上的数据库时-Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆