WeChat small program cloud function uses mysql database process detailed

The function of the small program cloud development is getting stronger, and now the small program cloud development can link Mysql data directly with the cloud function, operate the MySQL database, today Just tell you how to use the cloud function developed using the small program cloud to operate the MySQL database.

First, it is clear that the cloud function developed by the small program cloud is based on node.js, so we can use the Node.js’s mysql2 module to link and operate the mysql database, so we have to do now. Just use the MySQL2 module in the cloud function, and use this module library to implement the link of the mysql database.


We have to link the mysql database in the cloud function, and return to the link of MySQL The version number of the database. The MySQL database can be successfully linked, and the rear of MySQL is still small. So our first successful link MySQL database is the most important.

Here I am not explained, I have said that the initialization creation of small program cloud development, also Recording video explanation, classmates who don’t understand can be moved to see, the creation of cloud development project


Here you need to pay attention to

1, be sure to do the initialization of cloud development environment in app.js 微信小程序云函数使用mysql数据库过程详解

2, in Project.config.json Configuring Directory 微信小程序云函数使用mysql数据库过程详解

3, must use the AppId 微信小程序云函数使用mysql数据库过程详解

微信小程序云函数使用mysql数据库过程详解 Second,Create a cloud function, the name of it called mysql

created in our cloud, cloud right function

微信小程序云函数使用mysql数据库过程详解 III. installation mysql2 module dependency

1, right our mysql cloud function, click open in a terminal 微信小程序云函数使用mysql数据库过程详解

2, in the terminal enter npm install mysql2 微信小程序云函数使用mysql数据库过程详解

need your computer to install npm, if not installed, your own Baidu, online installation tutorial lot of npm.

微信小程序云函数使用mysql数据库过程详解 waiting for our mysql2 successful installation

Fourth, write mysql cloud function links mysql database
the complete code stickers for everyone

// function entry file cloud const cloud = require ( ‘wx-server-sdk’) // introducing mysql operation module const mysql = require ( ‘mysql2 / promise’) cloud.init () // function entry function cloud exports.main = async (event, context) => {// test library linking mysql database, where you you can link any libraries try mysql in {const connection = await mysql.createConnection ({host: “your server ip”, database: “operating the database”, user: “after using mysql name”, password: “mysql password “}) const [rows, fields] = awAIT Connection.execute (‘Select Version ();’) Return Rows;} catch (err) {Console.log (“Link Error”, Err) Return Err}}

Remember to replace the above Host, Database, User, Password with yourself. 微信小程序云函数使用mysql数据库过程详解

5. Upload and deploy cloud function

微信小程序云函数使用mysql数据库过程详解 Deployment success

微信小程序云函数使用mysql数据库过程详解 Here, you need to pay attention to you can’t link your local MySQL database with a cloud function. Because it is uploaded to the cloud function, it is uploaded to the WeChat server. There is no way to call you local mysql, unless you set the local MySQL Can be accessed by an outside or use the MySQL database on your own server.

This can successfully use WeChat applet links our MySQL database.

To here we click on our own defined MySQL cloud function, you can successfully link our MySQL database. 微信小程序云函数使用mysql数据库过程详解

is very simple.

About the knowledge of cloud development, you can look at my previous article, or you can see the video explanation of my recording

Video explanation


The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Cloud.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment