简体   繁体   English

如何连接到远程服务器上的数据库

[英]How to connect to a database on a remote server

I have a server, my-website.com, on a shared hodting plan.我有一个服务器 my-website.com,在共享 hodting 计划中。 I created a database on the server.我在服务器上创建了一个数据库。 From the webadmin, the db details are :从 webadmin,数据库详细信息是:

From phpMyAdmin :从 phpMyAdmin :

Server: localhost:3306服务器:本地主机:3306


Database server数据库服务器
Server: Localhost via UNIX socket服务器:本地主机通过 UNIX 套接字
Server type: Percona Server服务器类型:Percona 服务器
Server version: 5.6.39-83.1 - Percona Server (GPL), Release 83.1, Revision da5a1c2923f服务器版本:5.6.39-83.1 - Percona Server (GPL),83.1 版,修订 da5a1c2923f
Protocol version: 10协议版本:10
User: user@localhost用户:user@localhost
Server charset: UTF-8 Unicode (utf8)服务器字符集:UTF-8 Unicode (utf8)

I try to connect to it via node :我尝试通过节点连接到它:

const express = require('express')
const mysql = require('mysql')

const PORT = process.env.PORT || 5000

const app = express();
app.use(express.json());

const courses = [];

/** DB **/


app.get('/api/db_connect', function (req, res) {

    var myConStatus = "undef  conn";

    var con = mysql.createConnection({
        host: "191.181.181.81",  // FAKE
        Port: "3306",
        user: "user@localhost",
        database: "user_test_db",
        password: "password123"
    });

    con.connect(function (err) {
        myConStatus = "Connecting to db";
        if (err) {
            myConStatus = "ERR Conn";
            throw err;
        }

        myConStatus = "Success Connection";
    });

    var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
    con.query(sql, function (err, result) {
        myConStatus = "Creating table";

        if (err) {
            myConStatus = "ERR DB TABLE CREATE";
            throw err;
        }
        myConStatus = "Table created";
    });

    res.send(myConStatus);

});

/** END DB **/

This is never successful and no table gets created.这永远不会成功,也不会创建表。

What am I missing?我错过了什么?

Not sure if I understand fully, but mySQL can be configured for connections from local processes or from remote.不确定我是否完全理解,但可以为来自本地进程或远程进程的连接配置 mySQL。 If you are trying to connect remotely to a server that is configured for local it will not connect.如果您尝试远程连接到为本地配置的服务器,它将无法连接。

You did not say if you got connections errors or not.你没有说你是否有连接错误。

You can configure connections for standard TCP/IP or ssh TCP/IP as opposed to local socket/pipe - which maybe you have done.您可以为standard TCP/IPssh TCP/IP配置连接,而不是local socket/pipe - 您可能已经这样做了。

See here https://www.percona.com/doc/percona-xtrabackup/LATEST/howtos/enabling_tcp.html and this quote请参阅此处https://www.percona.com/doc/percona-xtrabackup/LATEST/howtos/enabling_tcp.html和此报价

"Most of the Linux distributions do not enable by default to accept TCP/IP connections from outside in their MySQL or Percona Server packages" “默认情况下,大多数 Linux 发行版都不允许在其 MySQL 或 Percona 服务器包中接受来自外部的 TCP/IP 连接”

If this is off the mark - just comment me and I will delete.如果这不合时宜——请评论我,我会删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM