node mysql默认密码_Node.js中的硬编码mysql用户和密码

bd96500e110b49cbb3cd949968f18be7.png

I'm using mysql with node.js. Something like this:

var mysql = require('mysql');

var connection = mysql.createConnection({

host : 'localhost',

user : 'me',

password : 'secret',

database : 'my_db'

});

connection.connect();

I was just wondering if having the database credentials (user and password) hardcoded in the server doesn't compromise security.

If it does, what would be a better way of doing this?

解决方案

The best way to handle this is to have your credentials stored in environment variables. You would then access your credentials like this:

var mysql = require('mysql');

var connection = mysql.createConnection({

host : process.env.DB_HOST,

user : process.env.DB_USER,

password : process.env.DB_PASS,

database : process.env.DB_NAME

});

connection.connect();

Then you would use a library like dotenv to set environment variables. You would put your environment variables in a .env file that is not distributed with the application, and that isn't in version control. An example .env might look like this

DB_NAME=my_db

DB_HOST=localhost

DB_PASS=secret

DB_USER=me

https://www.npmjs.com/package/dotenv - see this link for more information on using dotenv and do some research on 12 factor security in apps :)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值