远程设置和使用 mysql 服务器时遇到问题

时间:2021-04-12 01:34:28

标签: mysql node.js linux remote-access vps

我对开发有点陌生,我想在 vps 上托管一个 mysql 服务器来使用。

所以我已经在我的 vps 上设置了我的 apache mysql 和所有我需要的东西。一切都在本地运行,我可以使用浏览器上的 IP 访问它。

但是当我尝试从我的计算机访问 mysql 服务器时,它从不工作......

在终端中,当我尝试使用 $ mysql -h 82.165.117.19 -u kataiga -p 登录时,出现以下错误:

“错误 2003 (HY000):无法连接到 MySQL 服务器上的 '82.165.117.19' (113)”

当我尝试使用以下代码使用节点应用程序(在我的电脑上本地运行)访问数据库时

import mysql from 'mysql';
console.log('Get connection ...');

const con = mysql.createConnection({
    host: "82.165.117.19",
    user: "kataiga",
    password: "psw",
    database : "node_chat"
  });
 
   con.connect(function(err) {
    if (err) throw err;
    console.log("Connecté à la base de données MySQL!");
    con.query("SELECT eleves.id as 'eleve_id', eleves.nom as 'eleve_nom', eleves.cours_id, cours.nom as 'cours_nom', cours.date as 'cours_date' FROM eleves JOIN cours on eleves.cours_id = cours.id", function (err, result) {
        if (err) throw err;
        console.log(result);
      });
  });

我遇到了这个错误

Error: connect EHOSTUNREACH 82.165.117.19:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
    --------------------
    at Protocol._enqueue (/home/kataiga/project/chat_node/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/home/kataiga/project/chat_node/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/home/kataiga/project/chat_node/node_modules/mysql/lib/Connection.js:116:18)
    at file:///home/kataiga/project/chat_node/config/mysql.js:13:8
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5) {
  errno: -113,
  code: 'EHOSTUNREACH',
  syscall: 'connect',
  address: '82.165.117.19',
  port: 3306,
  fatal: true
}

我已经尝试删除绑定地址并将其设置为 0.0.0.0 在 mysql 配置文件中这两个都不起作用,我试图在 mysql 上连接的用户拥有所有权限,他的主机名是%

我的 vps 防火墙在 TCP 协议的端口 22、80、443、3306 上打开,允许所有 ip,运行的版本是 ubuntu 20.04

我希望这已经足够清楚了,我只是不知道该尝试/搜索什么。

将我的 mysqld.cnf 编辑成这样

#
# The MySQL database server configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld]
#
# * Basic Settings
#
 user           = mysql
 pid-file       = /var/run/mysqld/mysqld.pid
 socket = /var/run/mysqld/mysqld.sock
 port           = 3306
 datadir        = /var/lib/mysql
#bind-address   = 82.165.117.119
#skip-networking
#skip-bind-address
# If MySQL is running as a replication slave, this should be
# changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_tmpdir
 tmpdir         = /tmp
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#
# * Fine Tuning
#
key_buffer_size         = 16M
# max_allowed_packet    = 64M
# thread_stack          = 256K

# thread_cache_size       = -1

# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP

# max_connections        = 151

# table_open_cache       = 4000

# * Logging and Replication
#
# Both location gets rotated by the cronjob.
#
# Log all queries
# Be aware that this log type is a performance killer.
# general_log_file        = /var/log/mysql/query.log
# general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
# slow_query_log                = 1
# slow_query_log_file   = /var/log/mysql/mysql-slow.log
# long_query_time = 2
# log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
# server-id             = 1
# log_bin                       = /var/log/mysql/mysql-bin.log
# binlog_expire_logs_seconds    = 2592000
max_binlog_size   = 100M
# binlog_do_db          = include_database_name
# binlog_ignore_db      = include_database_name


0 个答案:

没有答案