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.
1, be sure to do the initialization of cloud development environment in app.js
2, in Project.config.json Configuring Directory
3, must use the AppId
Second,Create a cloud function, the name of it called mysql
created in our cloud, cloud right function
III. installation mysql2 module dependency
1, right our mysql cloud function, click open in a terminal
2, in the terminal enter npm install mysql2
need your computer to install npm, if not installed, your own Baidu, online installation tutorial lot of npm.
waiting for our mysql2 successful installation
Fourth, write mysql cloud function links mysql database
the complete code stickers for everyone
Remember to replace the above Host, Database, User, Password with yourself.5. Upload and deploy cloud function
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.
To here we click on our own defined MySQL cloud function, you can successfully link our MySQL database.
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
The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Cloud.