Back to blog

// OSSeva Blog

Migration

MongoDB vs MySQL vs PostgreSQL: Data Model, Transactions, Scaling, Licensing and Which to Choose

Randall McClure8 min read

The short answer

Choose MySQL when your data is relational: customers, orders, invoices, accounts, with rows that reference each other and reports that join them. Choose MongoDB when each record is a self-contained document whose shape changes often, and when you expect to spread one collection across many machines. Choose PostgreSQL when you want relational integrity and good JSON support in the same database. Most business applications fit the relational model, which is why MySQL and PostgreSQL remain the default. MongoDB earns its place where documents really are independent and the schema really does move.

MongoDB vs MySQL at a glance

MongoDBMySQL
Data modelDocuments (BSON) in collections; flexible schema by defaultRows in tables with a defined schema
Query languageMongoDB Query API and aggregation pipelineSQL
Joins$lookup (left outer join within a database); embedding is the usual designFull SQL joins
TransactionsSingle-document writes are atomic; multi-document transactions supported at extra costACID transactions on InnoDB, the default engine
Schema enforcementOptional JSON Schema validationAlways; DDL changes commit implicitly
Scale-outSharding built in (shards, mongos routers, config servers)Read replicas and Group Replication; sharding is left to the application or a proxy
High availabilityReplica sets with automatic electionsGroup Replication and InnoDB Cluster with automatic primary election
LicenceServer Side Public License (SSPL) v1, not OSI-approvedCommunity Edition under GPLv2; commercial licence from Oracle

The difference is the data model

MySQL stores data as rows in tables. You define columns and types first, and related data lives in separate tables tied together by keys. Queries join those tables back together at read time. That design keeps each fact in one place, which is why relational databases handle updates to shared data, such as a customer's address used by many orders, without copying it.

MongoDB stores data as documents: nested objects and arrays, much like JSON. A collection does not require every document to have the same fields, though you can add JSON Schema validation rules when you want them. The usual MongoDB design embeds related data inside one document, so an order carries its line items. MongoDB's own documentation says that for many scenarios the denormalized model of embedded documents and arrays remains the best fit, and that it reduces the need for transactions across documents.

That is the real choice. If your application reads and writes whole aggregates, one order or one user profile at a time, documents map cleanly to your code. If it needs to slice the same data many ways, join across entities and enforce relationships, tables and SQL do that with less effort.

Transactions and consistency

In MongoDB, a write to a single document is atomic. Multi-document transactions work across collections, databases and shards, and they are fully ACID. MongoDB is direct about the trade-off: a distributed transaction costs more than single-document writes and should not replace good schema design. MySQL's InnoDB engine is transactional for every statement and supports row-level locking and multi-version concurrency control. One MySQL detail to plan for: DDL statements such as CREATE TABLE and ALTER TABLE cause an implicit commit, so a schema change cannot be rolled back inside a transaction.

Scaling and availability

MongoDB's sharding is part of the product. Data is split across shards, each a replica set, and mongos routers send each query to the shards that hold the data. Each document can be up to 16 MiB, with GridFS for larger files. Sharding makes horizontal write scaling an operational task rather than an application rewrite, provided you choose a good shard key. Replica sets give each shard automatic failover through elections.

MySQL scales reads with replicas and gets automatic failover from Group Replication, which runs in single-primary mode with automatic primary election or in multi-primary mode, wrapped by InnoDB Cluster and MySQL Shell. Each member holds the whole data set, so write volumes beyond what one primary can take mean partitioning data across separate MySQL servers at the application or proxy layer. Most applications never reach that point; a well-sized MySQL primary carries a great deal of traffic.

JSON in MySQL, and SQL-style work in MongoDB

The two have moved towards each other. MySQL has a native JSON type stored in a binary format, with -> and ->> operators; you index JSON by indexing a generated column or with multi-valued indexes on arrays. MySQL 8.4 also enables X Plugin by default, which lets MySQL act as a schema-flexible document store through X DevAPI and MySQL Shell. MongoDB, for its part, has $lookup for joins and an aggregation pipeline for reporting. Neither convergence changes the core: MongoDB is built around documents and MySQL around tables.

Licensing and support

MySQL Community Edition is under GPLv2, and Oracle sells a commercial licence for software that embeds or bundles MySQL. MongoDB Community Server releases since 16 October 2018 are under the SSPL, which is not OSI-approved; its Section 13 matters only if you offer MongoDB's functionality to third parties as a service. Both vendors set the support calendar. MySQL 8.0 reached end of life on 30 April 2026, and MongoDB 4.2 to 6.0 are past their end-of-life dates, so many estates of both are running versions their vendors no longer patch.

MongoDB vs MySQL performance

Neither project publishes a neutral comparison, and benchmarks written by one side tend to favour it. The architecture tells you where each is strong. Reading or writing one whole document in MongoDB touches one place on disk, which suits workloads built around single aggregates. Queries that join many entities run more naturally in MySQL, where the optimiser plans joins and indexes cover the relationships. Write scaling across machines is easier in MongoDB because sharding is built in. Test with your own data shapes and access patterns before choosing on speed.

How to choose

  • Choose MySQL for transactional business systems, reporting with joins, applications built on frameworks that expect SQL, and teams that already run MySQL well.
  • Choose MongoDB for content, catalogues, event payloads and user profiles whose structure varies from record to record, and for collections you expect to shard across many machines.
  • Choose PostgreSQL when you want relational modelling and strong JSON support together, through jsonb and GIN indexes, under a permissive licence. Our MongoDB vs PostgreSQL and PostgreSQL vs MySQL comparisons cover those pairings in depth.

Where OSSeva fits

OSSeva supports all three on your own servers. OSSeva for MySQL ships patched, signed MySQL Community builds for 5.7 and 8.0 after Oracle's end of life and supports 8.4 LTS and 9.7 LTS. OSSeva for MongoDB patches self-managed MongoDB Community Server 4.2 to 6.0, including estates held on 4.4 because their CPUs lack AVX; it does not cover MongoDB Atlas, which MongoDB runs and patches. OSSeva for PostgreSQL patches 11 to 14 past community end of life. Each is priced per cluster or replica set, under one contract and one renewal date. See MySQL extended support and MongoDB extended support, or book a discovery call for a quote.

Frequently asked questions

MongoDB vs MySQL vs PostgreSQL: which should I use?

MySQL or PostgreSQL for relational data, joins and reporting; PostgreSQL if you also want rich JSON querying and indexing in the same database. MongoDB when records are self-contained documents with a changing shape, or when you need to shard one collection across many machines from the start.

Which is better, MongoDB or MySQL?

Neither is better in general. MySQL is better for related data that you join and report on. MongoDB is better for independent documents with a flexible structure and for built-in horizontal sharding. Pick the one whose data model matches how your application reads and writes.

What is the difference between MongoDB and MySQL?

MongoDB is a document database: it stores JSON-like documents in collections without a required schema and queries them with its own API. MySQL is a relational database: it stores rows in tables with a defined schema and queries them with SQL. MongoDB shards data across machines as part of the server; MySQL scales out through replication and application-level partitioning.

Is MongoDB faster than MySQL?

For reads and writes of whole documents, MongoDB avoids joins and can be very fast. For queries across related entities, MySQL's joins and optimiser usually do better. No general answer survives contact with your own workload, so benchmark both with your data.

Can MongoDB replace MySQL?

Technically yes, but it is a redesign, not a port. Tables become documents, joins become embedding or $lookup, and SQL becomes MongoDB queries. Teams that only want JSON flexibility often find MySQL's JSON type or PostgreSQL's jsonb enough without changing databases.

Is MongoDB open source?

MongoDB Community Server is source-available under the SSPL, which is not an OSI-approved licence. Releases before 16 October 2018 remain under AGPLv3. MySQL Community Edition is under GPLv2.

Tags

MongoDBMySQLPostgreSQLComparison

Ready to get your open source under control?

Talk to an OSSeva engineer about CVE coverage, compliance, and migration support for your stack.