简体   繁体   English

在Node JS中将mysql与过程变量连接

[英]Connecting mysql with process variable in Node JS

I am new to Node JS and was trying to get it connected with MySQL. 我是Node JS的新手,并试图使其与MySQL连接。 While studying I came around a snippet which I don't understand. 在学习时,我遇到了一个我不理解的片段。 It is as follows: 如下:

const mysqlServer = `mysql://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_SERVER}`;

Can anyone tell what is happening in this? 谁能告诉我这是怎么回事? And when I am printing mysqlServer it is giving me the output as : 当我打印mysqlServer时,输出为:

mysql://undefined:undefined@undefined

Please help..!! 请帮忙..!!

The process object is a global that provides information about, and control over, the current Node.js process. 流程对象是一个全局对象,可提供有关当前Node.js流程的信息并对其进行控制。 As a global, it is always available to Node.js applications without using require(). 全局而言,它始终可用于Node.js应用程序,而无需使用require()。

source 来源

The process.env property returns an object containing the user environment. process.env属性返回一个包含用户环境的对象。 See environ(7) . 参见environ(7)

source 来源

So the code is reading a couple of environment variables and inserting them into the string. 因此,代码正在读取几个环境变量,并将它们插入字符串中。

Since you are getting undefined, they are clearly not environment variables that have been defined. 由于您未定义,因此它们显然不是已定义的环境变量。

How you define environment variables depends, unsurprisingly, on the environment you are using. 毫无疑问,如何定义环境变量取决于您所使用的环境。

For example, in bash you could: 例如,在bash中,您可以:

export DB_USERNAME=example 
export DB_PASSWORD=example
export DB_SERVER=localhost
node myscript.js

(It is possible to do this as a one-liner but then your password can end up in the list of running processes, which is not good for security). (可以单行执行此操作,但是您的密码可能会出现在正在运行的进程列表中,这对安全性不利)。

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

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