Home > Database > Mysql Tutorial > Reducing MySQL Memory Usage in Docker

Reducing MySQL Memory Usage in Docker

Patricia Arquette
Release: 2025-01-27 02:11:09
Original
355 people have browsed it

Reduzindo o Uso de Memória do MySQL no 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

Create a file called

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>
Copy after login

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>
Copy after login

Step 3: Configuring docker-compose.yml

In the

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>
Copy after login

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>
Copy after login
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>
Copy after login
[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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template