Node.js MySQL连接到数据库
Node.js提供了一个模块来连接到MySQL数据库并执行MySQL操作。
以下是使用示例连接到MySQL数据库的分步指南。
步骤1:确保正确配置MySQL。收集有关IP地址,用户名和密码的信息。
步骤2:在您的node.js程序中,包括mysql模块。
connectToMySQL.js-在Node.js中连接到MySQL数据库
// 引入mysql模块
var mysql = require('mysql');
// 创建具有所需详细信息的连接变量
var con = mysql.createConnection({
host: "localhost", // 运行mysql的服务器的IP地址
user: "arjun", // mysql数据库的用户名
password: "password" // 对应的密码
});
// 连接到数据库。
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
$ node connectToMySQL.js Connected!
结论:
在此Node.js教程– Node.js MySQL –连接到数据库中,我们学习了在Node.js示例程序的帮助下使用Node.js MySQL模块-> createConnection方法连接到MySQL数据库。