MySql my.cnf 推荐设置

Fra*_*aes 8 mysql

我在谷歌上做了研究,阅读了一些博客,但没有发现任何对我来说真正有效的方法。在我的 mysql 多次测试配置崩溃后,我别无选择,只能在这里寻求帮助。

有人能给我一些关于在 my.cnf 中为具有以下规格的重型数据库服务器(12 个表,超过 50G 的数据)推荐哪些值的提示:`

root@blah:/etc/mysql# lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                16
On-line CPU(s) list:   0-15
Thread(s) per core:    16
Core(s) per socket:    1
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 62
Stepping:              4
CPU MHz:               2600.092
BogoMIPS:              5200.18
Hypervisor vendor:     Xen
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              20480K
NUMA node0 CPU(s):     0-15
root@blah:/etc/mysql# free -m
             total       used       free     shared    buffers     cached
Mem:         64237       2747      61490         19        189       1811
-/+ buffers/cache:        746      63491
Swap: 

    2047          0       2047
Run Code Online (Sandbox Code Playgroud)

该服务器仅用作数据库,因此我不必担心其他任何事情。只是为了确保 MySql 以最佳方式获取所有资源并使用所有资源。

我的 my.cnf 文件看起来像这样:

root@blah:/etc/mysql# cat my.cnf
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
#skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address       = 127.0.0.1
#
# * Fine Tuning
#
key_buffer      = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries   = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size         = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem



[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer      = 16M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
Run Code Online (Sandbox Code Playgroud)

小智 4

我看到你有 64GB RAM,而且全部用于 MySQL 服务器,这很好。首先,我建议增加缓存大小、tmp_tables 等,您可以在 Google 上搜索这些内容。如果您使用 InnoDB 表,我建议您也调整它们。下面我将给出一些建议,但最终取决于你该怎么做,你能做的最好的就是阅读并测试不同的配置。

要检查配置参数,请使用此查询,修改类似部分或完全省略它,这样您就可以确定您的设置是否在那里:

show variables like '%log%'; 
Run Code Online (Sandbox Code Playgroud)

跳过名称解析,这将使 mysql 更快,因为它不需要解析 DNS,请确保在连接到服务器后使用 IP 地址:

[mysqld]
skip-name-resolve
Run Code Online (Sandbox Code Playgroud)

建议为服务器上每 1 GB RAM 的两个值都指定 64M。这可以提高性能,根据您的需要更改这些值

tmp_table_size= 2000M
max_heap_table_size= 2000M
max_tmp_tables=300
Run Code Online (Sandbox Code Playgroud)

记录慢查询,long_query_time是查询较长的秒数,相应调整

[mysqld]
slow-query-log = 1
slow-query-log-file = /var/log/mysql-slow.log
long_query_time = 1
Run Code Online (Sandbox Code Playgroud)

我在下面给出了一些您需要调整的设置。

sort_buffer_size=10M 
read_buffer_size=10M 
table_open_cache=8000
query_cache_limit=50M
join_buffer=10M
Run Code Online (Sandbox Code Playgroud)

这些通常是您需要调整的主要设置,此处的数字仅供您参考,您应该相应地更改/调整它们。阅读 Mysql 文档以了解其中每一个的含义。

如果您正在运行 phpMyAdmin,将会出现数据库状态,转到那里,它会向您显示危险信号。除此之外,您可以手动检查一些参数,如下所示:SHOW GLOBAL STATUS LIKE 'Opened_tables';

偶尔检查一下表是否有碎片或者是否需要修复。这个命令解决了这个问题:

mysqlcheck -u root --auto-repair --optimize --all-databases
Run Code Online (Sandbox Code Playgroud)

另一个有用的工具是 mysqltuner,也安装并运行它。

希望这可以帮助。