Use node.js to connect to mysql database

Before connecting, you need to have the database and table structure
Code:

let mysql = require('mysql')

// 1. 创建连接
const connection = mysql.createConnection({
    
    
    host: 'localhost',
    user: 'root',
    password: '1234567',
    database: 'node-test'
})

// 2. 连接数据库
connection.connect()

// 3. 执行数据操作
connection.query('SELECT * FROM `user`', function (error, results, fields) {
    
    
    if (error) throw error;
    console.log('The solution is: ', results);
})

// 4. 关闭连接
connection.end()

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43612538/article/details/109190093