简体   繁体   中英

Should a node.js server have a single connection to a mysql instance?

Say I have app.js as the main app on the server, and it imports(requires) 4 other scripts that route login, join, write article, delete article requests. I currently have connection instances through var connection = mysql.createConnection(mysqlConfig) in EACH of the login.js, join.js, write.js, delete.js. I have a feeling that this is bad practice. What should I do? Should I create a mysqlconnection.js that exports a connection instance, and require that in each of the scripts that require a connection?

Deciding the number of connections depends on your application. If you have a sporadically used application with very few calls to mysql, then a single connection might work. However, looking at your use case, it seems that multiple users will be trying to login, write, etc. In such a case I recommend using Connection Pooling. https://github.com/coopernurse/node-pool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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