mysqlflaskhostingpeeweerailway

Why we have to restart MySQL service on railway? How to resolve my sql connection error on railway online server with MySQL as a seperate instance?


Error:

peewee.OperationalError: (2003, "Can't connect to MySQL server on 'containers-us-west-205.railway.app' (timed out)")

When it occurs:

After everyday, or so.

Account on railway:

Paid

Dev Frameworks:

Flask, MySQL as Backend

Railway dashboard:

Railway Dashboard

Shared varaibels are directly from the mysql instance on dashboard:

Shared variables

Works fine, just have to restart database every once in a while, why is that?


Solution

  • MySQL's docs for error 2003 state:

    The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server.

    Peewee is passing you the error from pymysql, which is indicating that your application timed-out attempting to connect to the MySQL server.

    I would reach out to your host to see what the issue is, and why the MySQL server is unreachable. You can also try:

    1. Using a connection pool w/Peewee (just make sure to set a stale timeout to ensure reconnects occur on long-idle connections).
    2. Ensuring connections are opened in the app.before_request and closed in app.teardown_request callbacks.
    3. Increasing the timeout pymysql uses, via connect_timeout, e.g. MySQLDatabase(..., connect_timeout=x) (default is 10s).