直接上干货,主要是利用微信的云函数和Sequelize进行连接外部MySQL,本文章主要讲述:

  • 创建一个连接MySQL的云函数
  • 在云函数内对MySQL数据进行简单的操作


拉取我的代码或者创建一个新的云开发小程序

//本文章主要的代码在以下目录中
miniprogram-cloud-development
 ├── cloudfunctions
 │   └── mysql
 ├── miniprogram
 │   └── pages
 │       └── mysql
 ├── project.config.json
 └── README.md

//本文章主要的代码在以下目录中
miniprogram-cloud-development
 ├── cloudfunctions
 │   └── mysql
 ├── miniprogram
 │   └── pages
 │       └── mysql
 ├── project.config.json
 └── README.md

选择已经配置好的mysql页面编译模式,在编译器中直接打开MySQL页面

我们页面代码也只需要一行代码,按钮进行触发云函数即可。

mysql.wxml

<button bindtap="mysql">mysqlbutton>

<button bindtap="mysql">mysqlbutton>

mysql.js

Page({
    mysql: function(event) {
        console.log(event)
  //调用云函数
        wx.cloud.callFunction({
            name: 'mysql' //调用我们后面写的mysql云函数对应命名
        }).then(res => {
            console.log(res)
        })
    }
})

Page({
    mysql: function(event) {
        console.log(event)
  //调用云函数
        wx.cloud.callFunction({
            name: 'mysql' //调用我们后面写的mysql云函数对应命名
        }).then(res => {
            console.log(res)
        })
    }
})

这是我们前端看到的页面了,就一个按钮触发云函数的页面就写好了。

创建连接MySQL云函数

可以看到我的代码中已经创建好一个叫mysqlindex.js云函数如下

const cloud = require('wx-server-sdk')

const Sequelize = require('sequelize')
// const sequelize = new Sequelize('数据库名称', '用户名', '密码', {
const sequelize = new Sequelize('xx', 'xx', 'xx', {
    host: '119.28.161.110',//云数据库IP地址
    port: 3306,
    dialect: 'mysql',
    operatorsAliases: false,
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    }
})

//创建USER表,表结构为一个firstName和lastName字段同为String类型
const User = sequelize.define('user', {
    firstName: {
        type: Sequelize.STRING
    },
    lastName: {
        type: Sequelize.STRING
    }
})

cloud.init()

exports.main = async (event, context) => {
 
    //这里进行调用创建USER表,并且插入一条数据,最后返回结果给调用者。
    return await User.sync({ force: true }).then(() => User.create({
        firstName: 'Huangquan',
        lastName: 'Wu'
    })).then(res => {
        return res.toJSON()
    })

}

const cloud = require('wx-server-sdk')

const Sequelize = require('sequelize')
// const sequelize = new Sequelize('数据库名称', '用户名', '密码', {
const sequelize = new Sequelize('xx', 'xx', 'xx', {
    host: '119.28.161.110',//云数据库IP地址
    port: 3306,
    dialect: 'mysql',
    operatorsAliases: false,
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    }
})

//创建USER表,表结构为一个firstName和lastName字段同为String类型
const User = sequelize.define('user', {
    firstName: {
        type: Sequelize.STRING
    },
    lastName: {
        type: Sequelize.STRING
    }
})

cloud.init()

exports.main = async (event, context) => {
 
    //这里进行调用创建USER表,并且插入一条数据,最后返回结果给调用者。
    return await User.sync({ force: true }).then(() => User.create({
        firstName: 'Huangquan',
        lastName: 'Wu'
    })).then(res => {
        return res.toJSON()
    })

}

Sequelize的文档地址:https://sequelize.org/v4/

下面我们要在个云函数miniprogram-cloud-development\cloudfunctions\mysql目录打开终端,安装Sequelize的依赖

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

D:\Git\miniprogram-cloud-development\cloudfunctions\mysql>npm install --save sequelize
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN mysql@1.0.0 No description
npm WARN mysql@1.0.0 No repository field.

+ sequelize@5.21.6
added 21 packages from 93 contributors and audited 22 packages in 7.577s
found 0 vulnerabilities


D:\Git\miniprogram-cloud-development\cloudfunctions\mysql>npm install --save mysql2
npm WARN mysql@1.0.0 No description
npm WARN mysql@1.0.0 No repository field.

+ mysql2@2.1.0
added 19 packages from 21 contributors and audited 41 packages in 10.162s
found 0 vulnerabilities


D:\Git\miniprogram-cloud-development\cloudfunctions\mysql>

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

D:\Git\miniprogram-cloud-development\cloudfunctions\mysql>npm install --save sequelize
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN mysql@1.0.0 No description
npm WARN mysql@1.0.0 No repository field.

+ sequelize@5.21.6
added 21 packages from 93 contributors and audited 22 packages in 7.577s
found 0 vulnerabilities


D:\Git\miniprogram-cloud-development\cloudfunctions\mysql>npm install --save mysql2
npm WARN mysql@1.0.0 No description
npm WARN mysql@1.0.0 No repository field.

+ mysql2@2.1.0
added 19 packages from 21 contributors and audited 41 packages in 10.162s
found 0 vulnerabilities


D:\Git\miniprogram-cloud-development\cloudfunctions\mysql>

安装好后我们需要把它同步到云开发的环境中,这里就不详细讲解怎么开通创建云开发环境了,可以自行在官方文档进行查找学习

附上地址:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html

测试调用云函数

我们只需要在预先设置的mysql编译模式下后,点击我们的按钮观察控制台打印的结果,和数据库可视化工具是否创建了表和插入数据。

mysql中user表里的用户 mysql user表_MySQL


微信开发工具

mysql中user表里的用户 mysql user表_数据库_02


Navicat

可以看到我们远端的MySQL数据库已经创建了USER表并且插入一条数据。

总结

个人比较推荐直接使用云开发自带的数据库,因为速度快,用外连MySQL的话会收到网络波动的影响。就像我要把这个云函数的超时时间跳到15s