How to Enable the query cache in MySQL

To improve the response from your MySQL server you need to add following two configurations to your MySQL server:

  • 1- query_cache_size
  • The amount of memory (SIZE) allocated for caching query results. The default value is 0, which disables the query cache.
  • 2- query_cache_type
  • Set the query cache type. It has as options : 0 : Don't cache results in or retrieve results from the query cache. 1 : Cache all query results except for those that begin with SELECT S_NO_CACHE. 2 : Cache results only for queries that begin with SELECT SQL_CACHE To enable this parameters you need to be the administrator of the server. Steps to follow :
  • 1-Connect as root user
  •  mysql -u root -p
  • 2-Set the query_cache_size size:(set to 64mb)
  • mysql:>SET GLOBAL query_cache_size = 67108864;
  • 3-Alter you my.cnf file and add the following:
  • query_cache_size = 268435456
    query_cache_type=1
    query_cache_limit=1048576
    Where : query_cache_size - This is the size of the cache in bytes. Setting this value to 0 will effectively disable caching. query_cache_type - This value must be ON or 1 for query caching to be enabled by default. query_cache_limit - This is the maximum size query (in bytes) that will be cached. Next restart your MySQL server so that your changes become permanent.