简体   繁体   English

如何在node.js中隐藏mysql数据库的连接详细信息?

[英]How to hide connection details for mysql database in node.js?

I just began working with mysql in node.js and I am setting up my app.js project and I am trying to hide my connection details such as my ip, username, pw, and db name. 我刚刚开始在node.js中使用mysql,并且正在设置我的app.js项目,并且试图隐藏我的连接详细信息,例如我的ip,用户名,pw和db名称。 I don't know how to go about hiding my connection details, so this is why i'm here. 我不知道如何隐藏我的连接详细信息,所以这就是为什么我在这里。

I have tried to add the details in my .profile , but I keep getting an authentication error. 我试图在.profile添加详细信息,但是我一直收到认证错误。 But, when I include these same connection details in my regular app.js file, it works and connects to the database. 但是,当我在常规的app.js文件中包含这些相同的连接详细信息时,它可以工作并连接到数据库。

Here is what is being displayed in my app.js file: 这是我的app.js文件中显示的内容:

var connection = mysql.createConnection({
              host     : 'my.ip.address.info',
              user     : 'username',
              password : 'password',
              database : 'databaseName'
            });
console.log('Connected');
connection.connect();

I just want to hide my connection details so that when my site goes live in the future it is secured from prying eyes. 我只想隐藏我的连接详细信息,以便将来我的网站上线时不会被窥探。 I understand that leaving these connection details in my app.js file is not the correct thing to do, so that's why I'm asking for help! 我了解将这些连接详细信息留在我的app.js文件中是不正确的,所以这就是我寻求帮助的原因! lol 大声笑

You can create a .env file to set environment variables and use the dotenv package to surface them in your process.env . 您可以创建一个.env文件来设置环境变量,并使用dotenv包在process.env它们。

https://github.com/motdotla/dotenv#readme https://github.com/motdotla/dotenv#readme

Create a file and name it .env and set your variables as such 创建一个文件并将其命名为.env然后将变量设置为

host=my-ip-address-info,
user=username,
password=password,
database=databaseName

Then you can access them like: 然后,您可以像这样访问它们:

var connection = mysql.createConnection({
          host: process.env.host
          user: process.env.username
          password: process.env.password,
          database: process.env.database
        });

You'll have to start your app with something like 您必须使用类似以下内容启动应用程序

node -r dotenv/config your_script.js

or add the following to the top of your entry script 或将以下内容添加到输入脚本的顶部

require('dotenv').config();

I prefer the first method because those environment variables should be set by your host provider so there is no need for the require statement in your code. 我更喜欢第一种方法,因为这些环境变量应该由您的宿主提供程序设置,因此您的代码中require语句。

I use dotenv . 我使用dotenv

yarn add dotenv

Create a .env file in the root directory of your project 在您的项目的根目录中创建一个.env文件

host=my.ip.address.info
user=username
password=password
database=databaseName

Then from your code 然后从你的代码

require('dotenv').config();

let host = process.env.host;

Do not commit the .env file to a public repo. 不要将.env文件提交到公共仓库。

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

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