您的位置:首页 > 数据库 > MySQL

node.js 连接 mysql 数据库的 基本操作

2020-06-04 06:49 183 查看

安装mysql
cnpm i mysql -S

创建一个js文件

const mysql = require('mysql');

// 建立数据库连接
const connection = mysql.createConnection({
host: 'localhost',  //主机地址 (默认:localhost)
user: 'root',  // 数据库用户名
password: '8888', // 数据库莫马
database: 'dom1',  // 数据库名
port: 3306    // 端口号 默认(3306)
});
//  连接数据库
connection.connect();

//查询数据库
connection.query('select * from user', (err, res, fil) => {
if (err) throw err;
console.log(res);
});

// 查询完关闭数据库
connection.end();

以上便是node中 mysql 的简单操作了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: