简体   繁体   English

如何设置我的数据库并将其连接到数字海洋上的 NodeJS 应用程序

[英]How to setup and connect my database to NodeJS application on digital ocean

I have managed to create a droplet on Digital-Ocean and managed to clone my Node JS app onto it.我设法在 Digital-Ocean 上创建了一个 droplet,并设法将我的 Node JS 应用程序克隆到它上面。 Locally, the app connects to MySQL database and I wanted to the same on the live version.在本地,该应用程序连接到 MySQL 数据库,我想在实时版本上也一样。 Ignorantly, I attempted to create a Managed database cluster which I did and added 1 user account and created 1 database.无知地,我尝试创建一个托管数据库集群,并添加了 1 个用户帐户并创建了 1 个数据库。 Right now I do not know how I can import the exported database.sql file into the database since I am only used to phpMyAdmin.现在我不知道如何将导出的database.sql文件导入数据库,因为我只习惯了phpMyAdmin。

How can I get this to work and connect to my NodeJS app?我怎样才能让它工作并连接到我的 NodeJS 应用程序?

You were using phpmyadmin as an interactive Mysql client program.您使用phpmyadmin作为交互式 Mysql 客户端程序。 It's easy to use but hard to set up because it's a web app.它易于使用但难以设置,因为它是一款 web 应用程序。

Try another MySql client program.尝试另一个 MySql 客户端程序。 The command-line client, memorably named mysql , is a good choice.令人难忘的命令行客户端mysql是一个不错的选择。

Get a shell on your droplet, then say在您的液滴上获取 shell,然后说

sudo apt install -y mysql-client
mysql -u username -p -h databasehostname -D database
mysql> source database.sql
mysql> quit

You'll be prompted for your database password.系统将提示您输入数据库密码。

That should import your database.那应该导入您的数据库。

The mysql command line program is very useful and worth some of your time to learn to use. mysql命令行程序非常有用,值得您花一些时间学习使用。

First, make sure your database cluster is not open to the outside world by adding a DB firewall using DigitalOcean databases.首先,通过使用 DigitalOcean 数据库添加数据库防火墙,确保您的数据库集群不对外界开放。 You can allow connections from your own droplet's private IP address, and your own public address (or VPN or however way you're set up).您可以允许来自您自己的 droplet 的私有 IP 地址和您自己的公共地址(或 VPN 或您的设置方式)的连接。 Once you've done that, you should be able to import your SQL file locally (or from the DO Droplet, as long as you have the mysql client installed):完成此操作后,您应该能够在本地导入 SQL 文件(或从 DO Droplet,只要您安装了 mysql 客户端):

mysql -h [host-provided-do] -P [port-provided-do] -u [username-provided-do] -p [db-name-provided-do] < my-file.sql

The most important thing is to make sure your managed database is not open to the outside world, and that you make sure it only allows incoming connections from known IP addresses.最重要的是确保您的托管数据库不对外界开放,并确保它只允许来自已知 IP 地址的传入连接。

In your NodeJS app, you can set the driver to connect to the private subnet that DO provides.在您的 NodeJS 应用程序中,您可以将驱动程序设置为连接到 DO 提供的私有子网。

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

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