使用nodejs操作mysql数据库
- 安装命令
npm init -y
- 安装命令
npm i mysql --registry=https://registry.npm.taobao.org
const mysql = require('mysql')
// 创建链接对象
const con = mysql.createConnection({
host: 'localhost',
user: '账号',
password: '密码',
port: '3306',
database: '数据库名'
})
// 开始连接
con.connect()
// 执行 sql 语句
const sql = `mysql操作命令`
//异步操作
con.query(sql, (err, result) => {
if (err) {
console.error(err)
return
}
console.log(result)
})
// 关闭连接
con.end()