如何为 MySQL 配置 Linux 资源组 (cgroups)

下面提供了Linux下资源组的配置和使用过程:

  1. 要使用 cgroups,我们必须在系统上安装“libcgroup”包。
# yum install libcgroup
  1. 为 cgroups 创建一个配置文件 (/etc/cgconfig.conf ) 并添加以下配置。
# vi /etc/cgconfig.conf
mount {
cpu = /cgroup/cpumem;
cpuset = /cgroup/cpumem;
memory = /cgroup/cpumem;
}
# High priority group
group mysqldb {
cpu {
# Allocate the relative share of CPU resources equal to 75%
cpu.shares="750";
}
cpuset {
# No alternate memory nodes if the system is not NUMA
cpuset.mems="0";
# Allocate CPU cores 2 through 5 to tasks in the cgroup
cpuset.cpus="2-5";
}
memory {
# Allocate at most 8 GB of memory to tasks
memory.limit_in_bytes="8G";
# Allocate at most 16 GB of memory+swap to tasks
memory.memsw.limit_in_bytes="16G";
# Apply a soft limit of 4 GB to tasks
memory.soft_limit_in_bytes="4G";
}
}
  1. 使用以下配置创建 cgroup 规则定义文件 /etc/cgrules.conf:
# vi /etc/cgrules.conf
# Assign tasks run by the mysql user to mysqldb
mysql cpu,cpuset,memory mysqldb
  1. 启动cgconfig服务,配置为系统启动时启动。
$ service cgconfig start
$ chkconfig cgconfig on
  1. 此时mysql的资源组已经准备就绪。
    重新启动 mysqld,它将根据上面提供的配置和规则获得资源。

  2. 我们也可以在运行时查找和更改资源组限制:

a)
要查找资源限制:

$ cgget -r memory.stat mysqldb

b) 设置资源限制:

$ cgset -r blkio.throttle.read_bps_device="8:1 0" iocap1

c) 保存当前修改的配置以备将来使用:

$ cgsnapshot -s > current_cgconfig.conf
日期:2020-09-17 00:11:07 来源:oir作者:oir