Why We Put JupyterHub on a Shared MySQL
The official recommendation is PostgreSQL
Section titled “The official recommendation is PostgreSQL”Let us get the facts straight first. The external database backend that the Zero to JupyterHub documentation describes and actually tests is PostgreSQL. Very simple installs use a SQLite file inside the Hub Pod. JupyterHub itself runs on SQLAlchemy, so the MySQL dialect is syntactically within the supported surface, but it is not a combination that upstream verifies on a regular basis.
So MySQL is less “unsupported” and more “not the well-trodden path”. It means there are few cases to find when you search upstream issues, and few solutions you can copy verbatim when something breaks. This platform chose MySQL knowing that cost, and the purpose of this article is to show the calculation honestly rather than to defend the choice.
What risk MySQL actually accepts
Section titled “What risk MySQL actually accepts”A summary along the lines of “SQLAlchemy supports MySQL too, so it is fine” hides the real failure points. Concretely, four things matter.
Charset and collation
Section titled “Charset and collation”The Hub stores strings such as usernames, group names, server names, OAuth client ids, and API token notes. If the database is created as legacy utf8 (MySQL’s UTF-8 in name only, storing at most 3 bytes) instead of utf8mb4, then the moment a 4-byte code point arrives the insert fails or the value is truncated. Server names containing emoji, supplementary plane characters, and some non-normalized input all fall into this category.
The quieter failure is on the collation side. With a case-insensitive collation, Alice and alice are treated as the same value in a unique index. The Hub treats usernames as unique, so input that would produce two separate accounts on PostgreSQL is either rejected with a duplicate key error on MySQL, or worse, two different people end up sharing one workspace. If the authentication method is later switched to an external identity provider, that difference surfaces then.
For that reason the database must be created explicitly with utf8mb4 and an intended collation. Leaving it to the server default means the value varies with the engine version or with when the cluster was rebuilt.
Index key length
Section titled “Index key length”utf8mb4 is up to 4 bytes per character. That collides directly with indexes. InnoDB has long imposed a 767-byte limit on index key prefixes, and it only extends to 3072 bytes when the DYNAMIC or COMPRESSED row format and innodb_large_prefix are in effect. Under the 767-byte limit, a utf8mb4 column can effectively only be indexed up to 191 characters.
What matters in the JupyterHub schema are the tables that use strings directly as indexed columns. The name column of the users table, the name column of the groups table, the identifier column of oauth_clients, the hash and prefix columns of api_tokens and oauth_codes, and the references that spawners holds by user and server name all belong here. If the declared length of these columns exceeds 191 characters, table creation itself fails with Specified key was too long in a 767-byte environment.
Recent MySQL 8.x defaults to DYNAMIC, so most of the time this is not a problem. What is dangerous is a cluster that inherited old server settings, or an operating standard that explicitly pins ROW_FORMAT to COMPACT. Using a shared MySQL means JupyterHub does not decide these server-level settings alone, so they must be verified in advance.
The driver
Section titled “The driver”The Hub picks its dialect and driver from the URI scheme. This deployment uses the mysql+pymysql:// form. The point most often missed here is that the driver must actually be installed inside the Hub image. The official Hub image is built assuming a PostgreSQL driver, so without a MySQL driver the Hub dies right after start with a ModuleNotFoundError. From the Kubernetes point of view this is just CrashLoopBackOff, and it does not look like a database connectivity problem.
pymysql is a pure Python implementation, so it needs no native library build, which keeps the image extension simple. In exchange you can no longer use the Hub image at its upstream tag and must maintain your own image layer. That burden repeats on every chart version bump.
Alembic migrations
Section titled “Alembic migrations”JupyterHub handles schema changes between versions with Alembic. Those migration scripts are exercised far more on PostgreSQL and SQLite. MySQL does not support transactional DDL, so if a migration fails midway the schema can be left half-applied. A situation that would end in a rollback on PostgreSQL becomes a situation a human must repair by hand on MySQL.
Also, the key length constraint described above sometimes surfaces for the first time during a column type change or an index rebuild step. In other words, a database that was perfectly healthy at deployment time breaks at the version bump. That is why in this combination a dry run before raising the version is not optional but mandatory.
Why we chose the shared MySQL anyway
Section titled “Why we chose the shared MySQL anyway”Reading only the list above, PostgreSQL is plainly correct. But that comparison assumes a world in which JupyterHub is the only thing that exists. The real platform has several components that need relational metadata (pipelines, the metadata store, the model registry, experiment management, the object storage filer, MLflow) and they already run on a single MySQL cluster, separated into per-component logical databases.
In that state, moving JupyterHub alone to PostgreSQL doubles all of the following.
- Backup and restore procedures, and restore drills
- Engines subject to security patching, and CVE tracking
- Monitoring metrics, alert rules, and dashboards
- HA configuration and failover verification
- Credential rotation procedures and least-privilege policy
What is the gain on the other side? The Hub’s write volume is small. Logins, spawns, stops, and token issuance are essentially all of it, and database traffic does not explode linearly as single-user servers grow. The schema is simple too. It is on the order of a few dozen tables with no complex queries, and it does not depend on PostgreSQL-specific features such as JSONB operators, array types, partial indexes, or advanced full-text search. In other words, almost no functional benefit is lost by changing engine.
Put plainly, this decision is not “MySQL is better for JupyterHub” but “the cost of adding one more operational surface for a single component is larger than the technical risk that component accepts”. The risk can be made finite with the mitigations below, whereas the operational burden of a second database engine stays forever.
Which components touch the database
Section titled “Which components touch the database”The decisive fact that makes this risk acceptable is that only one component touches the database. Only the Hub reads a URI of the form mysql+pymysql://<user>:***@<mysql-writer>.<db-namespace>.svc.cluster.local:3306/jupyterhub as DB_URI and applies it to c.JupyterHub.db_url. The Configurable HTTP Proxy keeps its route table in memory, and single-user Pods use only the workspace PVC and the Hub API.
Therefore the blast radius of the charset, index length, driver, and migration problems closes at the single Hub Deployment. This is not a situation where several workloads hit the same schema with different client libraries, so the number of combinations to verify is small.
PlantUML source:
The mitigations actually required to make it safe
Section titled “The mitigations actually required to make it safe”This choice is not one where you can do nothing. At minimum the following four things must be in place before the risk drops to a manageable level.
First, create the database explicitly instead of leaving it to the server default.
CREATE DATABASE jupyterhub CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_as_cs;Choose the collation’s case sensitivity deliberately. If usernames should be case sensitive, use an accent and case sensitive collation; if you decided not to distinguish them for login convenience, record that decision in the documentation. Drifting into the default and discovering it later is the worst outcome.
Second, use a least-privilege user scoped to this one logical database. Using the administrator account directly on a shared cluster lets a JupyterHub incident spread to other components’ databases.
CREATE USER 'jupyterhub'@'%' IDENTIFIED BY '<from-secret-store>';GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP, REFERENCES ON jupyterhub.* TO 'jupyterhub'@'%';CREATE, ALTER, INDEX, and DROP are needed because Alembic migrations change the schema directly. If you want to avoid that, you can split migrations into a separate Job with its own privileges and leave only DML to the runtime user. Either way, GRANT ALL ON *.* is not the answer.
Third, size the connection pool against the Hub’s restart pattern. The Hub is a single replica with few writes, so it does not need a large pool. The real problem is that when the Hub Pod restarts, the previous connections are not cleaned up immediately on the server side and leftover connections remain. On a shared cluster those leftovers eat into other components’ connection limits. Keep the pool size small and set a recycle interval shorter than the server’s wait_timeout so stale connections are never reused.
Fourth, actually run the Alembic upgrade on a staging copy before a version bump. Restore a dump of the production database into staging, run only the migration with the new Hub image, and compare schema and row counts. Raising the chart version without this procedure is the most common accident path in the MySQL combination.
Engine comparison
Section titled “Engine comparison”| Item | PostgreSQL | MySQL (shared) | SQLite |
|---|---|---|---|
| Official support | The reference backend z2jh documents and tests | Works as a SQLAlchemy dialect, but with few upstream verification cases | Documented, but only for trivial installs |
| Operational cost | Adding a separate engine adds backup, patching, and monitoring in full | One more logical database on an already operated cluster | No separate operations, but tied to the Hub Pod |
| Charset pitfalls | Effectively none, UTF-8 is the default | utf8mb4, collation, and index key length must be managed explicitly | None |
| HA | Possible via an operator or a managed service, but must be built from scratch | Reuses the existing cluster’s replication and writer endpoint as is | None, data loss risk when the Pod is recreated |
| Suitability | The default choice where no shared MySQL exists | Justified when a shared MySQL already exists and Hub write volume is small | Only for single-user experiments or throwaway verification |
When you should not do this
Section titled “When you should not do this”Do not generalize the conclusion of this article into “JupyterHub can go on MySQL”. This decision holds under exactly one premise: a well-operated shared MySQL cluster already exists, so the additional operational cost is close to zero.
If any one of the following applies, PostgreSQL is the correct default.
- There is no existing MySQL cluster to amortize. In that case standing up MySQL costs the same as standing up PostgreSQL, and all you gain is a less verified path.
- There is a policy requiring the Hub image to stay at its upstream tag. If you cannot carry your own image layer to add the driver, use PostgreSQL.
- Your team does not control the shared MySQL server settings and cannot change
ROW_FORMATor the charset default. - You must raise the JupyterHub version often and quickly, without capacity for a staging migration dry run each time.
- Other components already consume nearly the entire connection limit of that MySQL cluster.
Conversely, when all of these premises hold, this choice is not laziness but a deliberate trade-off to keep the operational surface singular. What matters is recording that trade-off in writing, so the next person does not burn hours staring at a CrashLoopBackOff.