NodeJs中MySQL数据库的使用

1. nodejs安装mysql模块

mysql 8 之前的版本中加密规则是mysql_native_password,而在之后版本,加密规则是caching_sha2_password ,node不支持mysql 8,连接时会报如下错误

Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

1.1 node不支持mysql 8的解决办法

ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘root’ PASSWORD EXPIRE NEVER;

ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘root’;

FLUSH PRIVILEGES;

1.2 nodejs项目中引入mysql2模块

npm i mysql2

1.3 mysql2模块在项目中的使用
const mysql = require('mysql2')
const db = mysql.createPool({
    host: 'XXX',
    user: 'root',
    password: 'root',
    database: 'XXX'
})
db.query('select 1',(err,results) =>{
    if (err){
        return console.log(err.message);
    }
    console.log(results);
})
1.3.1 插入数据
const user = {userId: 8, userName: 'Tom'}
const isql = 'INSERT INTO t (id,c) values (?,?)'
db.query(isql, [user.userId, user.userName], (err, results) => {
    if (err) {
        return console.log(err.message);
    }
    if (results.affectedRows === 1) {
        console.log('插入数据成功!');
    }
})
1.3.2 便捷插入
// id,c 和 表t的列名一致
const user = {id: 9, c: 'Mike'}
const isql = 'INSERT INTO t SET ? '
db.query(isql, user, (err, results) => {
    if (err) {
        return console.log(err.message);
    }
    if (results.affectedRows === 1) {
        console.log('插入数据成功!');
    }
})
1.3.3 更新数据
const user = {id: 9, c: 'Mike1'}
const isql = 'UPDATE t SET c = ? where id = ?'
db.query(isql, [user.c,user.id], (err, results) => {
    if (err) {
        return console.log(err.message);
    }
    if (results.affectedRows === 1) {
        console.log('更新数据成功!');
    }
})
1.3.4 更新数据的便捷方式
const user = {id: 8, c: 'Mike1'}
const isql = 'UPDATE t SET  ? where id = ?'
db.query(isql, [user,user.id], (err, results) => {
    if (err) {
        return console.log(err.message);
    }
    if (results.affectedRows === 1) {
        console.log('更新数据成功!');
    }
})
1.3.5 删除数据

const user = {id: 8}
const isql = 'DELETE FROM  t where id = ?'
db.query(isql, user.id, (err, results) => {
    if (err) {
        return console.log(err.message);
    }
    if (results.affectedRows === 1) {
        console.log('删除数据成功!');
    }
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Node.js操作MySQL数据库的示例代码: 1.安装mysql模块 ```shell npm install mysql ``` 2.连接MySQL数据库 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); }); ``` 3.创建数据库 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); connection.query('CREATE DATABASE test', (err, result) => { if (err) throw err; console.log('Database created!'); }); }); ``` 4.创建表 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))'; connection.query(sql, (err, result) => { if (err) throw err; console.log('Table created!'); }); }); ``` 5.插入数据 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'INSERT INTO customers (name, address) VALUES ("Company Inc", "Highway 37")'; connection.query(sql, (err, result) => { if (err) throw err; console.log('1 record inserted'); }); }); ``` 6.查询数据 ```javascript const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'test' }); connection.connect((err) => { if (err) throw err; console.log('Connected!'); const sql = 'SELECT * FROM customers'; connection.query(sql, (err, result) => { if (err) throw err; console.log(result); }); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值