先进行安装mysql模块 : npm install mysql
连接数据库
//导入mysql模块
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost', //表示连接的是本地主机
user: 'root',
password: '连接数据库的密码',
database: '数据库的名称'
});
connection.connect();
connection.query('mysql语句', (err,result)=>{
//如果有错误,则抛出错误信息
if(err) throw err ;
//没有错误,打印mysql执行的结果
console.log(result);
});
connection.end();