Mariadb with cloudflare worker
Hey I want to upload my nodejs website to cloudflare pages and use my own mariadb database that I have only my server since I must have it to work fast because its a game server that must talk with the db without latency.
Can I connect my database to cloudflare pages using cf workers?
6 Replies
On your server running mariadb you have to run cloudflared tunnel. From your worker you can access the mariadb with this library:
https://github.com/cloudflare/worker-template-mysql
GitHub
GitHub - cloudflare/worker-template-mysql: Reference demo and modif...
Reference demo and modified MySQL driver to connect Cloudflare Workers to a relational database. - GitHub - cloudflare/worker-template-mysql: Reference demo and modified MySQL driver to connect Clo...
I wouldn't recommend that, that's long unmaintained, and starting a new connection to the db for each function run is really inefficient. You'd want to pool the connections or use some http/rest interface between you and the database. Cloudflare does support outbound TCP Connections now, but I don't know of any libraries that have been updated to use it.
nodejs website to cloudflare pagesCloudflare Pages supports Pages Functions, which run in a similar environment as Workers, as V8 Isolates, not node.js. You only have node.js in the build environment. If your site uses a web framework like Express it won't work on Pages
if he already has a server running I dont see why he shouldnt use cloudflared. For me it worked really well.
But yeah otherwise he'll have to wait for mysql or mysql2 support when workers tcp socket api has been implemented for those libs.
How to use cloudflared with cloudflare pages for connecting my db to the website
It's not the cloudflared part as much as the library, an unmaintained 2 year old fork of a deno mysql driver, and it connects using tcp over websocket (this is a cloudflared limitation), unless you use DOs to pool you're also creating a whole new DB connection each time, not ideal.
I already have one nodejs express working perfectly fine on pages
The only problem is the database connection
I don't like the idea that the lib in not maintained, the website should fetch the data from the database every 10 seconds or less for being fully updated
How can I get my cloudflare access Public Key 🗝️?