{"id":22665233,"url":"https://github.com/volkansah/optimize-mysql-mariadb","last_synced_at":"2025-04-12T08:43:59.235Z","repository":{"id":189922514,"uuid":"606608520","full_name":"VolkanSah/optimize-MySQL-MariaDB","owner":"VolkanSah","description":"This configuration file includes some common settings to optimize MySQL or MariaDB performance, such as the innodb_buffer_pool_size, query_cache_size, and wait_timeout parameters.","archived":false,"fork":false,"pushed_at":"2024-06-02T06:25:22.000Z","size":27,"stargazers_count":13,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-05T22:51:24.636Z","etag":null,"topics":["database","innodb-buffer-pool-size","mariadb","mysql","optimization","optimization-methods","performance","speedups","thread-cache-size"],"latest_commit_sha":null,"homepage":"https://github.com/VolkanSah/optimize-MySQL-MariaDB","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VolkanSah.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":["volkansah"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2023-02-26T01:53:21.000Z","updated_at":"2025-03-20T06:47:44.000Z","dependencies_parsed_at":"2023-08-22T12:47:01.339Z","dependency_job_id":null,"html_url":"https://github.com/VolkanSah/optimize-MySQL-MariaDB","commit_stats":null,"previous_names":["volkansah/optimize-mysql-or-mariadb"],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VolkanSah%2Foptimize-MySQL-MariaDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VolkanSah%2Foptimize-MySQL-MariaDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VolkanSah%2Foptimize-MySQL-MariaDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VolkanSah%2Foptimize-MySQL-MariaDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VolkanSah","download_url":"https://codeload.github.com/VolkanSah/optimize-MySQL-MariaDB/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248543825,"owners_count":21121837,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","innodb-buffer-pool-size","mariadb","mysql","optimization","optimization-methods","performance","speedups","thread-cache-size"],"created_at":"2024-12-09T13:29:39.930Z","updated_at":"2025-04-12T08:43:59.212Z","avatar_url":"https://github.com/VolkanSah.png","language":null,"funding_links":["https://github.com/sponsors/volkansah"],"categories":[],"sub_categories":[],"readme":"\n# Several Ways to Optimize MySQL/MariaDB for Performance\n###### Update 2024\n\n## Table of Contents\n- [1. Tune Your Database Configuration](#1-tune-your-database-configuration)\n- [2. Optimize Your Queries](#2-optimize-your-queries)\n- [3. Enable Query and Slow Query Logging](#3-enable-query-and-slow-query-logging)\n- [4. Use Connection Pooling](#4-use-connection-pooling)\n- [5. Monitor and Tune Your System](#5-monitor-and-tune-your-system)\n- [Best Practices and Troubleshooting](#best-practices-and-troubleshooting)\n- [Example Configuration](#example-configuration)\n  - [16 GB of Server RAM](#16-GB-of-Server-RAM)\n  - [30 GB of Server RAM](#30-GB-of-Server-RAM)\n  - [64 GB of Server RAM](#64-GB-of-Server-RAM)\n\n## 1. Tune Your Database Configuration\nAdjust your database configuration to align with your server hardware and usage patterns. This involves tweaking parameters like buffer sizes, cache sizes, and timeouts.\n\n### Example Configuration Changes\n- **Buffer Pool Size**: Increase `innodb_buffer_pool_size` to store more data in memory, reducing disk I/O. For a system with 16GB RAM, setting it to 8GB (50% of RAM) could be effective.\n  \n```shell\n  innodb_buffer_pool_size = 8G\n```\n\n- **Log File Size**: Adjust `innodb_log_file_size` to manage the transaction log size, improving write performance.\n\n```shell\n  innodb_log_file_size = 512M\n```\n\n- **Thread Cache Size**: Increase `thread_cache_size` to improve performance by reusing threads.\n```shell\n  thread_cache_size = 16\n```\n\n- **Query Cache**: Configure the query cache to speed up repetitive queries.\n```shell\n  query_cache_size = 128M\n  query_cache_type = 1\n```\n\n- **Table Open Cache**: Increase `table_open_cache` to optimize the number of open tables.\n```shell\n  table_open_cache = 4096\n```\n\n### System-Specific Configuration File Locations\n- **Ubuntu/Debian**: `/etc/mysql/my.cnf`\n- **CentOS**: `/etc/my.cnf`\n- **Arch Linux**: `/etc/mysql/my.cnf`\n- **Other Linux**: Varies, commonly `/etc/mysql/my.cnf` or `/etc/my.cnf`\n\n## 2. Optimize Your Queries\nUse proper indexing, minimize subqueries and temporary tables, and avoid expensive joins. Utilize the `EXPLAIN` command to analyze and optimize query execution plans.\n\n### Example Query Optimization\n```sql\nEXPLAIN SELECT * FROM orders JOIN customers ON orders.customer_id = customers.id;\n```\nThis command helps identify if the join operation is using indexes efficiently.\n\n## 3. Enable Query and Slow Query Logging\nIdentifying resource-intensive queries is crucial. Enable slow query logging to detect queries that take too long to execute, allowing for targeted optimizations.\n\n### Configuration for Slow Query Logging\nAdd the following to your MySQL configuration file:\n\n```shell\nslow_query_log = 1\nslow_query_log_file = /var/log/mysql/mysql-slow.log\nlong_query_time = 2\n```\n\n### System-Specific Log File Locations\n- **Ubuntu/Debian**: `/var/log/mysql/mysql-slow.log`\n- **CentOS**: `/var/log/mysqld-slow.log`\n- **Arch Linux**: `/var/log/mysql/mysql-slow.log`\n- **Other Linux**: Varies, commonly `/var/log/mysql-slow.log`\n\n## 4. Use Connection Pooling\nReduce the overhead associated with establishing new database connections by implementing connection pooling.\n\n### Implementing Connection Pooling with `mysqlnd_ms`\n```php\n$hosts = array(\n  \"mydb1\" =\u003e array(\"host\" =\u003e \"host1\", \"weight\" =\u003e 2),\n  \"mydb2\" =\u003e array(\"host\" =\u003e \"host2\", \"weight\" =\u003e 1)\n);\nmysqlnd_ms_set_qos($conn, MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL, MYSQLND_MS_QOS_OPTION_GTID, $hosts);\n```\n\n## 5. Monitor and Tune Your System\nRegularly check performance metrics such as CPU usage, memory usage, and disk I/O. Adjust configurations and queries based on these metrics.\n\n### Tools for Monitoring\n- **MyTop**: A real-time console-based tool to monitor queries and performance.\n- **PT-Query-Digest**: Analyze slow queries to find potential bottlenecks.\n\n## Best Practices and Troubleshooting\n### Best Practices\n- Always validate changes in a staging environment before production.\n- Regularly update your database system to benefit from performance improvements and security patches.\n\n### Troubleshooting Common Issues\n- **High CPU Usage**: Check for inefficient queries and consider increasing `query_cache_size`.\n- **Disk I/O Bottlenecks**: Increase `innodb_buffer_pool_size` to reduce disk reads.\n\n## Example Configuration\n##### 16 GB of Server RAM\nAn example MySQL/MariaDB configuration file optimized for a system with 16GB of RAM:\n\n```ini\n[mysqld]\n# General settings\nuser = mysql\npid-file = /var/run/mysqld/mysqld.pid\nsocket = /var/run/mysqld/mysqld.sock\ndatadir = /var/lib/mysql\n\n# Performance settings\ninnodb_buffer_pool_size = 8G\ninnodb_log_file_size = 512M\nthread_cache_size = 16\nquery_cache_size = 128M\nquery_cache_type = 1\ntable_open_cache = 4096\n\n# Slow query log settings\nslow_query_log = 1\nslow_query_log_file = /var/log/mysql/mysql-slow.log\nlong_query_time = 2\n\n# Connection settings\nmax_connections = 500\nmax_user_connections = 50\n\n# Replication settings\nserver-id = 1\nlog_bin = /var/log/mysql/mysql-bin.log\nbinlog_format = mixed\n\n# Additional settings\nlog_error = /var/log/mysql/error.log\nexpire_logs_days = 10\nmax_binlog_size = 100M\n```\n##### 30 GB of Server RAM\nAn example MySQL/MariaDB configuration file optimized for a system with 30GB of RAM:\n\n```ini\n[mysqld]\n# General settings\nuser = mysql\npid-file = /var/run/mysqld/mysqld.pid\nsocket = /var/run/mysqld/mysqld.sock\ndatadir = /var/lib/mysql\n\n# Performance settings\ninnodb_buffer_pool_size = 24G\ninnodb_log_file_size = 1G\nthread_cache_size = 32\nquery_cache_size = 256M\nquery_cache_type = 1\ntable_open_cache = 8192\n\n# Slow query log settings\nslow_query_log = 1\nslow_query_log_file = /var/log/mysql/mysql-slow.log\nlong_query_time = 2\n\n# Connection settings\nmax_connections = 1000\nmax_user_connections = 100\n\n# Replication settings\nserver-id = 1\nlog_bin = /var/log/mysql/mysql-bin.log\nbinlog_format = mixed\n\n# Additional settings\nlog_error = /var/log/mysql/error.log\nexpire_logs_days = 10\nmax_binlog_size = 100M\n```\n\n##### 64 GB of Server RAM\nAn example MySQL/MariaDB configuration file optimized for a system with 64GB of RAM:\n\n```ini\n[mysqld]\n# General settings\nuser = mysql\npid-file = /var/run/mysqld/mysqld.pid\nsocket = /var/run/mysqld/mysqld.sock\ndatadir = /var/lib/mysql\n\n# Performance settings\ninnodb_buffer_pool_size = 48G\ninnodb_log_file_size = 2G\nthread_cache_size = 64\nquery_cache_size = 512M\nquery_cache_type = 1\ntable_open_cache = 16384\n\n# Slow query log settings\nslow_query_log = 1\nslow_query_log_file = /var/log/mysql/mysql-slow.log\nlong_query_time = 2\n\n# Connection settings\nmax_connections = 2000\nmax_user_connections = 200\n\n# Replication settings\nserver-id = 1\nlog_bin = /var/log/mysql/mysql-bin.log\nbinlog_format = mixed\n\n# Additional settings\nlog_error = /var/log/mysql/error.log\nexpire_logs_days = 10\nmax_binlog_size = 100M\n```\nThese enhancements and explanations aim to provide a thorough guide for tuning and optimizing MySQL and MariaDB, helping to achieve better database performance across various environments.\n### Credits\n**Volkan Sah**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvolkansah%2Foptimize-mysql-mariadb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvolkansah%2Foptimize-mysql-mariadb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvolkansah%2Foptimize-mysql-mariadb/lists"}