Home >Database >Mysql Tutorial >Reducing MySQL Memory Usage in Docker
Optimize MYSQL Memory Use in Docker Containers, especially in Limited Resources environments like VPSS. I recently solved a memory problem on my VPS performing multiple WordPress containers with mysql; Idle consumption was approximately 500mb, reduced to about 150MB after the optimizations described here.
This guide demonstrates how to configure a MYSQL container on Docker to minimize memory consumption. Includes an example of a optimized file, a my.cnf
file with volume mapping and file permissions instructions. docker-compose.yml
Step 1: Creating the configuration file my.cnf
in the directory of your choice on the host. This file will contain optimized settings to reduce memory use. mysql-low-memory-my.cnf
<code>[mysqld] # Tamanho do buffer principal do InnoDB innodb_buffer_pool_size = 128M # Tamanho do buffer de chave (MyISAM) key_buffer_size = 8M # Número máximo de conexões simultâneas max_connections = 50 # Cache de threads thread_cache_size = 8 # Tamanho de tabelas temporárias em memória tmp_table_size = 16M max_heap_table_size = 16M # Desativa o Performance Schema performance_schema = 0 # Tamanho do buffer de log innodb_log_buffer_size = 4M # Cache de tabelas abertas table_open_cache = 200</code>
Step 2: Setting the file permissions
After creating the file, set the reading permissions only by MySQL, avoiding modifications by other processes or users. mysql-low-memory-my.cnf
<code class="language-bash">chmod 0444 mysql-low-memory-my.cnf</code>
Step 3: Configuring docker-compose.yml
file, map the volume containing the configuration file for the mysql container: docker-compose.yml
<code class="language-yaml">services: mysql: image: mysql:8 container_name: mysql-container environment: MYSQL_ROOT_PASSWORD: sua_senha volumes: - './mysql-low-memory-my.cnf:/etc/mysql/conf.d/mysql-low-memory-my.cnf' ports: - "3306:3306"</code>
Step 4: Starting the container
Run the command below to start the container in detaated mode:
<code class="language-bash">docker compose up -d</code>Your MySQL container is now running with optimized memory consumption! Remember that these settings can affect database performance, adjust them as needed.
Use the following command to monitor container memory use:
<code class="language-bash">docker stats mysql-container</code>[repository with the code used] (link to the repository)
The above is the detailed content of Reducing MySQL Memory Usage in Docker. For more information, please follow other related articles on the PHP Chinese website!