Mysql DB Storage

Hello,
I deployed my custom embedded app in cloudflare successfully. Now I want to store sessionObject in mysql db of my own. But if I do it, cloudflare is throwing an error. App is not displaying.

let _sessionStorage: MySQLSessionStorage | null = null;

function getSessionStorage() {

if (!_sessionStorage) {

\_sessionStorage = new MySQLSessionStorage(

  "mysql://user:user%40123@153.178.2.241:3306/user"

);

}

return _sessionStorage;

}

export const shopify = (context: AppLoadContext) =>

shopifyApp({

apiKey: context.cloudflare.env.SHOPIFY_API_KEY,

apiSecretKey: context.cloudflare.env.SHOPIFY_API_SECRET || "",

apiVersion: ApiVersion.October24,

scopes: context.cloudflare.env.SCOPES?.split(","),

appUrl: context.cloudflare.env.SHOPIFY_APP_URL || "",

authPathPrefix: "/auth",

sessionStorage: getSessionStorage(),

distribution: AppDistribution.AppStore,

future: {

  unstable_newEmbeddedAuthStrategy: true,

},

...(process.env.SHOP_CUSTOM_DOMAIN

  ? { customShopDomains: \[process.env.SHOP_CUSTOM_DOMAIN\] }

  : {}),

});

Can you share the error message itself?

I suspect your MySQL instance isn’t accessible by CF due to IP address restrictions.

MySQL is “bound” to localhost only by default, you’ll need to configure it to allow connections from external IP addresses.

Just a note, please refrain from sharing the connection secrets. You’re exposing configuration to your database to anyone reading this topic.

Hey @Hema_Varshini_B :waving_hand: - I agree with Dylan on this one. If you’re able to share the exact error message, that would be super helpful! I also wanted to share this documentation that might help with your set up if you haven’t taken a look at it yet (looks like you’re setting up a CF worker directly?):

If the above doesn’t help, please feel free to ping me here with that specific error message and I can take a look :slight_smile:

More of a Cloudflare question than a Shopify question I think but, after resolving this issue, you may still have trouble running the Mysql Session Storage package on Cloudflare. It’s designed for a stateful server with a connection pool, with Cloudflare (or any serverless worker) you’re going to be reinitializing a new connection pool on every single request.

If you’re open to modifying the Mysql session package, you could use Cloudflare Hyperdrive - I would only suggest this if you’re working on an existing app with real store sessions in your Mysql.

If this is a new app, Cloudflare KV is an excellent option for your session storage since you’re already hosting on Cloudflare, and you can use Shopify’s dedicated package out of the box.

1 Like