We use cookies to make your viewing experience better. By accepting you consent, you agree to our Cookie policy

Accept
Improve your Craft CMS skills

Database Options For Craft CMS

10 min read
Andy Golpys, Co-Founder of MadeByShape

Choosing and configuring the right database is mission-critical for any Craft CMS website. This definitive guide explores the myriad database options for Craft, from MySQL to PostgreSQL and beyond. Learn how to select, optimize, secure, back up, and monitor a database that delivers the performance, scalability, and continuity your Craft CMS site needs to thrive.

Craft CMS supports MySQL, PostgreSQL, and SQL Server databases. Consider performance needs, future scalability, security requirements, hosting environment, budget, and in-house expertise when selecting. Benchmark options and optimize the chosen database for best performance. Frequent automated backups are essential. Robust security hardening is also critical.

Database Options for Craft CMS

Overview of Databases for Craft CMS

When building a website with Craft CMS, one of the most crucial choices is which database to use. The database is the foundation that all your content, users, assets and other data is stored in. Picking the right database can have a major impact on your site's performance, scalability and security.

Craft CMS is flexible and supports several options for databases, including MySQL, PostgreSQL and SQL Server. The choice comes down to your specific needs and environment. MySQL is the default and most common option as it's free, open source and works well for many use cases.

PostgreSQL offers more advanced features and can handle heavier workloads. SQL Server brings the power of Microsoft's enterprise-grade database engine.

Some key factors to evaluate when selecting a database for your Craft CMS site are:

  • Expected Traffic and Growth: How many visitors do you anticipate, and how rapidly is your user base and content library expected to expand over time? Databases like PostgreSQL and SQL Server are better suited for high traffic loads.

  • Data Types and Volume: What kinds of data will your site need to store, and in what quantities? Text-heavy content favors PostgreSQL, while SQL Server excels with binary data like large media files.

  • Performance Requirements: How fast does your database need to be in querying data and delivering dynamic pages to users? Benchmark tests consistently show PostgreSQL and SQL Server outperforming MySQL here.

  • Hosting Environment: Which databases are supported on your server or hosting platform? This technical constraint often dictates the options.

  • Budget: Proprietary commercial databases carry licensing fees and higher hosting costs. Open source options like MySQL and PostgreSQL have cost advantages.

  • In-House Expertise: Leverage your team's existing knowledge and experience with a particular database technology.

Evaluating Database Needs for Your Craft Site

Selecting the right database for your Craft CMS website requires carefully evaluating your unique needs and priorities across a number of factors:

Expected Traffic and Complexity

Consider your site's anticipated traffic volumes, number of users, and backend complexity. Higher levels in these areas may indicate PostgreSQL or SQL Server can better handle the load. Monitor resource usage on existing Craft sites to extrapolate needs.

Types of Data and Content

Assess what kinds of data your site will manage, including text, media files, and structured content types. PostgreSQL excels at text search and indexing. SQL Server handles large binaries like video seamlessly. Match database strengths to your content.

Scalability and Growth

Weigh future expansion plans. MySQL and PostgreSQL both allow easy scaling through replication and sharding. SQL Server makes scaling seamless via its proprietary engine. Plan ahead for growth to avoid painful migrations down the road.

Security and Compliance Needs

Review regulatory and security requirements for your industry. SQL Server offers robust enterprise-grade capabilities like granular user permissions and dynamic data masking. PostgreSQL also has excellent security features.

Integration and Maintenance

Factor in developer experience. PostgreSQL and MySQL work well with most common web stacks. SQL Server integrates tightly with Windows and .NET frameworks. Also consider in-house familiarity supporting each database long-term.

Budgetary Factors

Lookup the hosting, licensing and hardware costs associated with each option. Open source PostgreSQL and MySQL offer significant cost savings over commercial counterparts. However, higher load may justify the investment for SQL Server.

Evaluating Craft CMS database options thoroughly upfront allows choosing the optimum platform for performance, scalability, security and total cost of ownership. Test options early in development to validate the best database for your site's specific needs.

Comparing Major Database Systems for Craft CMS

MySQL for Craft CMS

MySQL is the most common database used with Craft CMS, and for good reason. It's free, open source, easy to get started with and can handle many typical Craft workloads.

Pros:

  • Free and open source - very low cost to get started

  • Wide platform support including shared hosting

  • High performance for simpler read/write loads

  • Easy replication and clustering for scaling

  • Flexible schema and indexing capabilities

  • Developer familiarity & community support

Cons:

  • Not as robust for complex queries as PostgreSQL/SQL Server

  • More DBA optimization needed for large datasets

  • Weaker enterprise-level capabilities like security

MySQL is a great choice for small to mid-size Craft sites with simpler content models and tens of thousands of pages. It can provide snappy performance out of the box. Scaling can be achieved through replication and sharding. Overall, MySQL delivers a solid low-cost database option for many Craft use cases.

PostgreSQL for Craft CMS

PostgreSQL is growing in popularity as a database for demanding Craft CMS installs due to its advanced feature set and reputation for reliability.

Pros:

  • Powerful JSON support and text search capabilities

  • Excellent performance for read/write intensive workloads

  • Robust security and user permission controls

  • Highly extensible and customizable

  • Reliable replication and failover capabilities

  • Strong community and developer support

Cons:

  • More complex to administer than MySQL

  • Steeper learning curve for developers

  • Needs more manual query tuning than MySQL


For large, complex Craft sites with 100K+ pages, deep content relationships, and heavy traffic, PostgreSQL is worth evaluating. It can significantly outperform MySQL under heavy loads. Its JSON handling simplifies Craft's native data structures. The maturity and active development of PostgreSQL also make it appealing for business-critical sites.

Microsoft SQL Server for Craft CMS

Microsoft SQL Server offers enterprise-grade capabilities for Craft CMS at scale, with advanced security, performance monitoring and high availability features.

Pros:

  • Leading performance, scalability and uptime

  • Granular security policies and encryption

  • Seamless Windows/IIS environment integration

  • Robust cloud platform support with Azure SQL

  • Powerful analytics and reporting capabilities

Cons:

  • Expensive licensing and DBA costs

  • Steep learning curve for developers

  • Challenging to migrate from other databases

For large media-heavy sites, regulated industries requiring security, and Windows-centric development shops, SQL Server merits consideration. It brings rock-solid stability and uptime capabilities that allow it to run some of the world's largest web properties. The drawbacks are costs and migration challenges, but for the right use case, SQL Server delivers.

Setting up Craft CMS with Different Databases

Configuring Craft CMS with MySQL

Setting up Craft CMS with MySQL as the database backend requires a few key steps. First, you will need access to an existing MySQL server version 5.5 or higher. On this server, create a new, empty database that will be dedicated solely to Craft, such as craftcms. Be sure to make note of the hostname, port and credentials for connecting to the MySQL server.

Next, create a separate MySQL user account that will be used specifically by Craft to access the database. Grant this user full privileges on the new Craft database, including permissions for CREATE, ALTER, INSERT, UPDATE, DELETE and DROP operations. Restrict other users and roles from accessing the Craft database for security.

With the MySQL server and dedicated Craft database established, now configure Craft itself. Edit Craft's config/db.php file and specify 'mysql' as the database driver. Fill in the database name, hostname, port, username and password as per the MySQL server setup. You can also set a table name prefix like 'craft_' at this point to namespace the tables Craft will create.

Once the MySQL pieces are provisioned and Craft is configured, Craft will automatically create its database schema and begin utilizing MySQL for all data storage and queries. Monitor usage over time and optimize MySQL configuration settings like innodb_buffer_pool_size for optimal performance.

Configuring Craft CMS with PostgreSQL

To configure Craft CMS with PostgreSQL, start by ensuring you have a PostgreSQL server version 9.5 or later available. Log into this server using an admin account with permissions to create new databases. Create an empty PostgreSQL database for Craft to use.

Then, make a dedicated PostgreSQL user account for Craft and grant it strong passwords and full privileges on the Craft database, including connect and CRUD operations. Revoke any public access and restrict other users.

With the PostgreSQL database provisioned, install the pdo_pgsql PHP extension on your web server if needed. Then, open Craft's config/db.php file and specify 'pgsql' as the database driver. Enter the PostgreSQL database name, server hostname and port, admin username and password that were set up earlier.

Optionally set a table prefix as well, like 'craft_' to namespace the tables. Craft will now build its database schema in PostgreSQL and implement all queries and data storage using it. Tune PostgreSQL config settings like shared_buffers for optimal performance.

Configuring Craft CMS with Microsoft SQL Server

To configure Craft CMS with Microsoft SQL Server, first ensure you have access to an instance of SQL Server 2012 or later. Create a new empty SQL database that will be dedicated to Craft. Make note of the SQL Server hostname, port, admin credentials and new database name.

Install the pdo_sqlsrv PHP extension on your web server if needed. Then, open config/db.php in Craft and specify 'sqlsrv' as the database driver. Enter the SQL Server name, Craft's new database name, and the SQL admin username and password.

Log into SQL Server with an admin account in the db_creator role and grant the Craft user account db_owner permissions on the Craft database. Tighten security policies as needed, leveraging features like encryption.

Enable SQL Server Agent for tasks like backups. Look at configuring replication and data masking for increased resilience and privacy. With SQL Server provisioned and access configured, Craft will utilize it as its high-powered database engine.

Backing Up and Restoring Craft CMS Databases

Automated Backups for Craft CMS Databases

Implementing automated backups for Craft CMS databases is a best practice to ensure continuity and recoverability. Popular solutions for scheduled backups include:

  • Native database tools like pg_dump or SQL Server Agent Jobs

  • Backup services like UpdraftPlus or BackupBuddy

  • Craft plugins such as Feed Me or B&C Backup

  • Managed database backups from your hosting provider

Aim to backup Craft databases daily at a minimum, if not more frequently. Retain at least 7 days of backups, with longer retention of 30+ days ideal. Store backups in multiple locations, like locally on your server and in cloud storage buckets for redundancy.

Test restoration from backups regularly. Automated solutions take the manual work out of remembering to backup your database. They provide peace of mind that your Craft data is protected.

Manual Backups for Craft CMS Databases

Manually backing up Craft CMS databases is also straightforward:

For MySQL or PostgreSQL, tools like phpMyAdmin offer user-friendly backup interfaces. Click the database, choose Export, set format to SQL, compression to gzipped, and download the dump file locally or to cloud storage.

For SQL Server, use Management Studio, right-click the database, choose Tasks > Back Up... and create a compressed full backup. Save the .BAK file locally and externally.

You can also backup databases from the command line using mysqldump, pg_dump or SQL Server's BACKUP command. Scripts automate this for recurring backups.

Always download backup files rather than leaving them on your web server, which risks data loss if the server has issues. Backup prior to Craft or plugin upgrades in case of conflicts. Validate restored backups before deleting older ones.

Restoring Craft CMS Databases from Backup

To restore a Craft CMS database from a backup:

Locate the compressed SQL or BAK file, ideally from cloud storage or an external hard drive for recovery scenarios. Place the backup file on your local computer.

Delete the existing Craft database, and recreate an empty one with the same name. For MySQL/PostgreSQL, connect to the database server locally or via phpMyAdmin. For SQL Server, use Management Studio.

Open a database tool like phpMyAdmin, MySQL Workbench, pgAdmin or SSMS. Connect to your Craft database server and empty database. Select to run or import the backup file, decompressing it.

The tool will rebuild the database, recreating all tables and importing the data. Expect the process to take several minutes for large sites.

Check for any errors in importing the SQL data. Review tables and data to verify successful restoration. Test front-end and backend functionality before returning the site to production use.

Be careful not to overwrite your existing production database when restoring! An outdated or incomplete backup can also result in lost content or corruption. Always validate thoroughly.

Migrating a Craft CMS Database

Preparing for a Craft CMS Database Migration

Planning and preparation are key when migrating a Craft CMS database to minimize downtime and errors. Important steps include:

  • Audit current data and structure to define migration scope

  • Check platform compatibility if changing database systems

  • Provision new database servers, storage and hardware if needed

  • Pick maintenance windows and set timelines to minimize downtime

  • Test and optimize migration scripts on a staging environment

  • Document processes, assets, dependencies and credentials

  • Backup current database, codebase, files and assets

Taking time upfront to inventory data, map out dependencies, test processes, and backup everything reduces issues down the road.

Using Tools to Migrate a Craft CMS Database

Automated tools and scripts streamline migrating a Craft CMS database for more consistent results. Popular options:

Native Database Utilities

  • mysqldump or pg_dump for logical MySQL/PostgreSQL migrations

  • SQL Server Migration Assistant for Microsoft migrations

Craft Plugins

  • Feed Me or Craft-Scripts for export/import

  • Retcon for easier staging migrations

Third Party Tools

  • WP Migrate DB Pro - robust migration capabilities

  • All-in-One WP Migration - optimized process

Tools migrate better than manual SQL exports. Test rigorously first on staging environments.

Schedule migrations during maintenance windows to minimize downtime. Verify all data is preserved and no errors occurred. Avoid making other configuration changes simultaneously.

Automated tools paired with proper planning result in smooth database migrations.

Common Issues and Challenges Migrating Craft CMS Databases

Some potential pitfalls to watch out for when migrating Craft CMS databases:

Platform Compatibility: Source and target database versions and engines can conflict, preventing seamless migrations. Test compatibility upfront.

Data Mapping: Complex relational Craft data may not transfer over cleanly. Use ID mapping in migration tools.

Dependency Errors: Asset files, linked entries and other dependencies can break if not addressed. Migrate those elements first.

Legacy or Custom Data: Heavily customized data schemas may require manual migrations of some tables.

Performance Problems: Larger databases can take a long time and overload servers if not optimized. Do test transfers first.

Downtime Overruns: Estimate generously and have rollback plans for any issues extending downtime.

Thorough preparation, testing, using the right tools, and having contingency plans for problems will all help smooth database migrations. Take it slow, validate everything, and focus on avoiding data loss above all. Advanced planning is key for successful Craft CMS database migrations.

Monitoring and Optimizing Craft CMS Databases

Monitoring Database Performance for Craft CMS

Keeping a close eye on key database performance metrics is crucial for Craft CMS installs.

Rather than waiting for issues to arise, take a proactive approach to monitoring. Leverage tools like MySQL Workbench, New Relic, Scout, and custom scripts to gain visibility into load, query latency, active connections, cache efficiency, and storage utilization. Set prudent alerts on critical thresholds so you are notified of any anomalous activity or concerning trends. Analyze spikes during traffic surges or batch operations to right-size capacity planning.

Robust monitoring reveals optimization opportunities and enables scaling your database proactively rather than reactively.

Optimizing Database Queries in Craft CMS

Unoptimized queries can drag down CMS and front-end performance over time. Use slow query logging in MySQL and PostgreSQL to identify queries needing attention. Add indexes appropriately on fields used for filtering, sorting, or joining data to speed up query execution. But strike a balance as excessive indexing can also degrade performance. Tune the database config itself for increased caching and memory utilization based on your usage patterns.

Offload repetitive or complex queries to Redis caching or your application cache locally. Avoid eager overfetching by lazy loading associated data only when needed. Judicious use of {% cache %} tags and query parameters at the template level can work wonders. Eliminate extraneous queries triggered from Twig templates for lighter execution. And for truly complex data models, PostgreSQL or SQL Server may provide superior query handling capabilities. Keeping queries nimble ensures your Craft CMS database can support growing demands.

Resolving Craft CMS Database Bottlenecks

If your Craft database becomes a performance bottleneck, several techniques can provide relief: Scale up server resources by increasing RAM, upgrading CPUs, and investing in faster storage. Scale out with database replicas, clustering, sharding or other distributed architectures.

Partition tables that grow very large or indefinitely. Upgrade to an enterprise-level database like PostgreSQL, Aurora, or SQL Server for more robust capabilities. Address slow queries through indexing, caching, and tuning as discussed earlier. Move file-based assets to cloud object storage to reduce database load. Enable caching of expensive queries via Redis or Memcached.

Deactivate plugins with intensive database operations temporarily until the issues are resolved.

And consider spinning up a cloud-based database replica to offload read traffic. Both rapidly adding resources and addressing the root cause through optimizations are warranted to eliminate bottlenecks and keep your Craft CMS database humming.

Securing Your Craft CMS Database

Craft CMS Database Security Best Practices

Running a Craft CMS site with a database backend obligates you to implement robust security measures. Start by encrypting all data at rest and in transit using strong SSL/TLS connections and passwords. Harden the database server itself through strict firewall policies, OS hardening to close vulnerabilities, and isolating it within a private subnet without any remote access.

Tightly control access with least privilege in mind - avoid root and require VPN. Regularly patch and update the database software to prevent exploits. Audit user activity and enable anomaly detection. Maintain frequent backups tested and stored securely. And disable unneeded features and samples to minimize the attack surface.

By applying database security in layered, defense-in-depth approach focused on prevention and minimizing potential impact, Craft CMS data stays protected.

Securing MySQL for Craft CMS

For MySQL specifically, isolate all databases within a private internal network completely blocking external traffic. Require TLS 1.2+ encryption for all connections to MySQL. Create a dedicated MySQL user account for Craft CMS with only the minimum necessary privileges.

Leverage user and host-based access controls wherever possible, avoiding the root account except when absolutely needed. Use a WAF like mod_security to filter MySQL requests. Bind MySQL to localhost to prevent remote connections. Disable features like XML and local file loading along with sample users and databases.

Limit information exposure through configured variables. And validate the full MySQL configuration using a security scanner to catch any overlooked hardening.

Securing PostgreSQL for Craft CMS

PostgreSQL also provides robust tools for locking down Craft CMS databases. Isolate PostgreSQL completely, disallowing any external access without a VPN connection. Enforce TLS 1.2+ encryption for all connections. Create a dedicated PostgreSQL role for Craft CMS with least privileges. Tighten host-based authentication via pg_hba.conf. Validate the PostgreSQL configuration is hardened against known exploits. Use SELinux or AppArmor for mandatory access control. Only permit remote access from trusted internal IP addresses over VPN.

Disable extraneous functions like sunshine() that reveal server details. And actively review logs to detect escalation attempts. With thoughtful configuration, PostgreSQL provides enterprise-grade security for Craft CMS.

Choosing Database Hosting for Craft CMS

PaaS Database Hosting Options for Craft CMS

Fully-managed platform-as-a-service (PaaS) databases simplify deploying Craft CMS without managing servers. Top options include:

Amazon RDS:

Supports MySQL, PostgreSQL, SQL Server. Managed failover and redundancy. Scales on demand. Fast provisioning. Complex configurations via parameter groups. Integrates with other AWS services. Free tier available.

Azure Database for MySQL/PostgreSQL:

Fully-managed with automatic monitoring, tuning, security, and updates. Scales storage and compute independently. Zone-redundant high availability. Integrated with Azure ecosystem. Generous free tier.

Google Cloud SQL:

Managed MySQL and PostgreSQL with high availability configurations. Automated replication, failover, and flexibility. Scales to 15TB storage. Integrates with Google Cloud Platform tools. Always-on encryption.

Benefits:

Automated scalability, redundancy, security patching, and maintenance. Usage-based billing. Fast provisioning. Generally cost-effective for production workloads. Reduce DevOps overhead.

Considerations:

Potential vendor lock-in. Custom config limitations. No direct server access. Learning cloud-specific management.

Managed Database Hosting for Craft CMS

Specialized managed database providers tailored for Craft CMS include:

NitroPack:

Optimized MySQL and PostgreSQL plans for Craft. WAF protection. Regular backups. Staging sites. Free migrations. Performance monitoring. 24/7 expert support.

ServerPilot:

High-performance MySQL, PostgreSQL, Redis. Automated failover and clustering. Backups and restores. CLI and APIs for management. Craft-focused support.

SpinupWP:

Managed MySQL and object caching. Free migrations, backups, and SSL. Craft focused hosting and support. All plans include staging sites.

Benefits:

Performance optimization, security hardening, frequent backups, and expert Craft CMS support.

Only pay for resources used. Consolidated management.

Tradeoffs:

Less flexibility for unique DB configurations. No Windows platform support.

Self-Hosted Database Options for Craft CMS

Self-hosting Craft CMS databases requires provisioning your own servers and resources.

Considerations include:

  • Hardware selection, sizing, and colocation options

  • OS and database software licensing costs

  • Managing configuration, security hardening, scaling, redundancy

  • In-house database administration expertise

  • Higher time investment for management

Benefits include full control, customization, and avoiding cloud lock-in.

Weigh the DIY tradeoffs, TCO, and team skills before self-hosting production databases.

Shape April 2022 HR 202
Andy Golpys
- Author

Andy has scaled multiple businesses and is a big believer in Craft CMS as a tool that benefits both Designer, Developer and Client. 

Share
Feedback
Show us some love
Email Us
We usually reply within 72 hours
Agency Directory
Submit your agency
Affiliate Partners
Let's chat