egg 项目实战(七)Egg.js 连接 mysql 数据库
2023-09-11 14:15:29 时间
1.创建 mysql 表结构
create database egg_article; use egg_article; create table article( id int(10) not null auto_increment, img text default null comment '缩略图', title varchar(80) default null comment '文章标题', summary varchar(300) default null comment '文章简介', content text default null comment '文章内容', createTime timestamp default null comment '发布时间', primary key(id) )engine=InnoDB AUTO_INCREMENT=1 comment '文章表'; insert into article(img, title, summary, content, createTime) values('编程必备基础知识 计算机组成原理+操作系统+计算机网络', 'https://img2.mukewang.com/szimg/5d1032ab08719e0906000338.jpg', '介绍编辑必备基础知识', '快速、系统补足必备的计算机系统知识、更快更有趣、更贴近实际工作,让你更快地学到满足实际工作需要的知识,为以后的工作打好良好的基础', '2020-03-03 10:20:20');
2.安装 egg-mysql
mkdir server cd server npm init egg --type=simple yarn add egg-mysql
3.配置 plugin.js
config/plugin.js
'use strict'; exports.mysql = { enable: true, package: 'egg-mysql', };
4.配置 config.default.js
config/config.default.js
config.mysql = { // 单数据库信息配置 client: { // host host: 'localhost', // 端口号 port: '3306', // 用户名 user: 'root', // 密码 password: 'root', // 数据库名 database: 'egg_article', }, // 是否加载到 app 上,默认开启 app: true, // 是否加载到 agent 上,默认关闭 agent: false, };
5.调用
async index() { const { ctx, app } = this; const res = await app.mysql.select('article'); console.log(res); ctx.body = 'hi, egg'; }
.
相关文章
- JS框架_(JQuery.js)文章全屏动画切换
- JS框架_(JQuery.js)Tooltip弹出式按钮插件
- JS框架_(Progress.js)圆形动画进度条
- Mysql授权允许远程访问解决Navicat for MySQL连接mysql提示客户端不支持服务器请求的身份验证协议;考虑升级MySQL客户端
- baguetteBox.js响应式画廊插件(纯JS)
- Mac Pro 解压安装MySQL二进制分发版 mysql-5.6.30-osx10.11-x86_64.tar.gz(不是dmg的)
- 【MySQL】常见的mysql 进程state
- 【MySql】开机自动启动mysql服务
- linux - mysql 异常:MySQL Daemon failed to start.
- MySQL按照汉字的拼音排序,mysql汉字排序
- MySQL选择数据库use与mysql_select_db使用详解
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)--续
- MYSQL随机抽取查询 MySQL Order By Rand()效率问题
- Mysql 如果有多个可选条件怎么加索引_MySQL|mysql-索引
- atitit.跨语言实现备份mysql数据库 为sql文件特性 api 兼容性java c#.net php js
- 原生js实现随机验证码HTMl-JS
- MySQL数据库加密和解密~认证登陆密码(mysql.user)和MySQL不区分大小写
- 为实施运维人员准备的的MySQL英文记忆表
- Mysql的mysqldump详解 mysql数据库备份和导入
- 再谈 Mysql解决中文乱码
- Linux MySQl 5.7.17 MySQL ERROR 1366(HY000):Incorrect string value 解决方法
- MySQL-mysql 8.0.11安装教程 windows
- golang操作mysql数据库(Go-SQL-Driver/MySQL)
- 【JS高级】js面向对象三大特性之封装—如何创建对象_05
- 【Mysql异构实时同步Oracle】OGG12异构同步mysql到oracle(windows mysql实时同步数据到linux oracle)详细文档
- MySQL 中 my.cnf 常用配置