如何在 Node.js 中连接数据库
连接 MySQL 数据库
要连接 MySQL 数据库,可以使用以下步骤:
- 安装
<a style="color:#f60; text-decoration:underline;" href="https://www.php.cn/zt/15713.html" target="_blank">mysql</a>
包:npm install mysql
- 创建一个数据库连接对象:
<code class="typescript">const mysql = require(\'mysql\'); const connection = mysql.createConnection({ host: \'localhost\', user: \'username\', password: \'password\', database: \'database_name\' });