[TOC]
> 案例 **数据库** 采用的是 MySQL
## 1. 基础连接数据库
~~~
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'pmz.2580',
database: 'test'
});
// 连接数据库
connection.connect();
const sql = 'SELECT * FROM movies_detail';
connection.query(sql, function (error, results, fields) {
if (error) throw error;
// 从数据库中查到的数据
console.log('The solution is: ', results[0]);
});
// 关闭连接
connection.end();
~~~
// 输出结果
![](https://img.kancloud.cn/b5/d6/b5d601f9f849c80a0f2bb4f069c974b6_1604x394.png)