MySQL服务器安装完之后如何调节性能

原文作者: Peter Zaitsev

原文来源: http://www.mysqlperformanceblog.com/2006/09/29/what-to-tune-in-mysql-server-after-installation


What to tune in MySQL Server after installation

My favorite question during Interview for people to work as MySQL DBAs or be involved with MySQL Performance in some way is to ask them what should be tuned in MySQL Server straight after installation, assuming it was installed with default settings.

在面试MySQL的DBA或者遇见那些进行MySQL性能优化工作的人时,我最喜欢问的问题是:MySQL按照默认设置完成安装之后,我们需要调节哪些参数呢?

I’m surprised how many people fail to provide any reasonable answer to this question, and how many servers are where in wild which are running with default settings.

令人惊讶的是,少有人能对这个问题给出一个合理的答案,大量的MySQL服务器运行在默认的设置下。

Even though you can tune quite a lot of variables in MySQL Servers only few of them are really important for most common workload. After you get these settings right other changes will most commonly offer only incremental performance improvements.

尽管你能在MySQL上调节如此之多的变量,但在许多常见的数据负载场景下,只有少数才是真正重要的。当你将这些重要参数进行正确的设置之后,其他参数的修改对于性能改进的提高不大。

key_buffer_size – Very important if you use MyISAM tables. Set up to 30-40% of available memory if you use MyISAM tables exclusively. Right size depends on amount of indexes, data size and workload – remember MyISAM uses OS cache to cache the data so you need to leave memory for it as well, and data can be much larger than indexes in many cases. Check however if all of key_buffer is used over time – it is not rare to see key_buffer being set to 4G while combined size of .MYI files is just 1GB. This would be just a waste. If you use few MyISAM tables you’ll want to keep it lower but still at least 16-32Mb so it is large enough to accommodate indexes for temporary tables which are created on disk.

key_buffer_size – 如果使用MyISAM类型的数据表,这个参数对性能优化非常重要。如果系统中只使用MyISAM类型的数据表,可以把这个参数值设置为可用内存的30%-40%。合理的值取决于索引的数量,数据量的大小和负载—记住,MyISAM使用操作系统的缓存来缓存数据,所以你也需要给这些数据留下部分内存空间,而且很多情况下数据比索引所占空间要大。随着时间的推移,检查是否所有的key_buffer都被利用了—所有.MYI文件的总和只有1GB而key_buffer设置为4G的情况并不常见。这只会造成浪费。如果你使用少量的MyISAM数据表,你仍旧需要保留少量的至少16-32M的key_buffer_size以适应给予磁盘的临时表索引所需。

innodb_buffer_pool_size – This is very important variable to tune if you’re using Innodb tables. Innodb tables are much more sensitive to buffer size compared to MyISAM. MyISAM may work kind of OK with default key_buffer_size even with large data set but it will crawl with default innodb_buffer_pool_size. Also Innodb buffer pool caches both data and index pages so you do not need to leave space for OS cache so values up to 70-80% of memory often make sense for Innodb only installations. Same rules as for key_buffer apply – if you have small data set and it is not going to grow dramatically do not oversize innodb_buffer_pool_size you might find better use for memory available.

innodb_buffer_pool_size 这对Innodb表非常重要。Innodb表对缓冲池大小比MyISAM更为敏感。在默认的key_buffer_size设置下,MyISAM表可以非常好的运行,甚至于在大数据量负载下也无妨。但是,在默认的innodb_buffer_pool_size的设置下,Innodb表的运行速度将和蜗牛一样。并且,Innodb缓冲池同时缓存数据和索引页,所以你不需要留空间给系统缓存。如果只使用Innodb,将innodb_buffer_pool_size值设置为内存大小的70-80%会加快其运行速度。

innodb_additional_pool_size This one does not really affect performance too much, at least on OS with decent memory allocators. Still you might want to have it 20MB (sometimes larger) so you can see how much memory Innodb allocates for misc needs.

innodb_additional_pool_size – 这个选项对性能影响并不太多,至少在有差不多足够内存可分配的操作系统上是这样。不过如果你仍然想设置为 20MB(或者更大),因此就需要看一下Innodb其他需要分配的内存有多少。

innodb_log_file_size Very important for write intensive workloads especially for large data sets. Larger sizes offer better performance but increase recovery times so be careful. I normally use values 64M-512M depending on server size.

innodb_log_file_size 在高写入负载尤其是大数据集的情况下很重要。这个值越大则性能相对越高,但是要注意到可能会增加恢复时间。我经常设置为 64-512MB,跟据服务器大小而异。

innodb_log_buffer_size Default for this one is kind of OK for many workloads with medium write load and shorter transactions. If you have update activity spikes however or work with blobs a lot you might want to increase it. Do not set it too high however as it would be waste of memory – it is flushed every 1 sec anyway so you do not need space for more than 1 sec worth of updates. 8MB-16MB are typically enough. Smaller installations should use smaller values.

innodb_log_buffer_size 默认的设置在中等强度写入负载以及较短事务的情况下,服务器性能还可以。如果存在更新操作峰值或者负载较大,就应该考虑加大它的值了。如果它的值设置太高了,可能会浪费内存 — 它每秒都会刷新一次,因此无需设置超过1秒所需的内存空间。通常 8-16MB 就足够了。越小的系统它的值越小。

innodb_flush_logs_at_trx_commit Crying about Innodb being 100 times slower than MyISAM ? You probably forgot to adjust this value. Default value of 1 will mean each update transaction commit (or each statement outside of transaction) will need to flush log to the disk which is rather expensive, especially if you do not have Battery backed up cache. Many applications, especially those moved from MyISAM tables are OK with value 2 which means do not flush log to the disk but only flush it to OS cache. The log is still flushed to the disk each second so you normally would not loose more than 1-2 sec worth of updates. Value 0 is a bit faster but is a bit less secure as you can lose transactions even in case MySQL Server crashes. Value 2 only cause data loss with full OS crash.

innodb_flush_logs_at_trx_commit 是否为Innodb比MyISAM慢1000倍而头大?看来也许你忘了修改这个参数了。默认值是 1,这意味着每次提交的更新事务(或者每个事务之外的语句)都会刷新到磁盘中,而这相当耗费资源,尤其是没有电池备用缓存时。很多应用程序,尤其是从MyISAM转变过来的那些,把它的值设置为 2 就可以了,也就是不把日志刷新到磁盘上,而只刷新到操作系统的缓存上。日志仍然会每秒刷新到磁盘中去,因此通常不会丢失每秒1-2次更新的消耗。如果设置为 0 就快很多了,不过也相对不安全了 — MySQL服务器崩溃时就会丢失一些事务。设置为 2 指挥丢失刷新到操作系统缓存的那部分事务。

table_cache – Opening tables can be expensive. For example MyISAM tables mark MYI header to mark table as currently in use. You do not want this to happen so frequently and it is typically best to size your cache so it is large enough to keep most of your tables open. It uses some OS resources and some memory but for modern hardware it is typically not the problem. 1024 is good value for applications with couple hundreds tables (remember each connection needs its own entry) if you have many connections or many tables increase it larger. I’ve seen values over 100.000 used.

table_cache — 打开一个表的开销可能很大。例如MyISAM把MYI文件头标志该表正在使用中。你肯定不希望这种操作太频繁,所以通常要加大缓存数量,使得足以最大限度地缓存打开的表。它需要用到操作系统的资源以及内存,对当前的硬件配置来说当然不是什么问题了。如果你有200多个表的话,那么设置为 1024 也许比较合适(每个线程都需要打开表),如果连接数比较大那么就加大它的值。我曾经见过设置为 100,000 的情况。

thread_cache Thread creation/destructions can be expensive, which happen at each connect/disconnect. I normally set this value to at least 16. If application has large jumps in amount of concurrent connections and I see fast growth of

Threads_Created variable I boost it higher. The goal is not to have threads created in normal operation.

thread_cache — 线程的创建和销毁的开销可能很大,因为每个线程的连接/断开都需要。我通常至少设置为 16。如果应用程序中有大量的跳跃并发连接并且 Threads_Created 的值也比较大,那么我就会加大它的值。它的目的是在通常的操作中无需创建新线程。

query_cache If your application is read intensive and you do not have application level caches this can be great help. Do not set it too large as it may slow things down as its maintenance may get expensive. Values from 32M to 512M normally make sense. Check it however after a while and see if it is well used. For certain workloads cache hit ratio is lower than would justify having it enabled.

query_cache — 如果你的应用程序有大量读,而且没有应用程序级别的缓存,那么这很有用。不要把它设置太大了,因为想要维护它也需要不少开销,这会导致MySQL变慢。通常设置为 32-512Mb。设置完之后最好是跟踪一段时间,查看是否运行良好。在一定的负载压力下,如果缓存命中率太低了,就启用它。

Note: as you can see all of these are global variables. These variables depend on hardware and mix of storage engines, while per session variables are typically workload specific. If you have simple queries there is no reason to increase sort_buffer_size even if you have 64GB of memory to waste. Furthermore doing so may decrease performance.

I normally leave per session variable tuning to second step after I can analyze workload.

注意:就像你看到的上面这些全局表量,它们都是依据硬件配置以及不同的存储引擎而不同,但是会话变量通常是根据不同的负载来设定的。如果你只有一些简单的查询,那么就无需增加 sort_buffer_size 的值了,尽管你有 64GB 的内存。搞不好也许会降低性能。

我通常在分析系统负载后才来设置会话变量。

P.S Note MySQL distribution contains bunch of sample my.cnf files which may be great templates to use. Typically they would already be much better than defaults if you chose correct one.

P.S,MySQL的发行版已经包含了各种 my.cnf 范例文件了,可以作为配置模板使用。通常这比你使用默认设置好的多了。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: MySQL是一个开源的关系型数据库管理系统,其最新版本是MySQL 5.7.11。以下是MySQL 5.7.11的安装教程详细步骤: 1. 首先,从MySQL官方网站(https://dev.mysql.com/downloads/mysql/)下载MySQL 5.7.11的安装程序。 2. 安装程序下载成后,双击安装程序并按照提示进行安装。 3. 在安装向导页面中,选择“Custom”自定义安装类型,以便选择安装选项。 4. 在选择产品要安装的组件页面中,选择MySQL Server核心组件以及相关的工具和连接器。 5. 在选择配置类型页面中,选择“Server Machine”配置类型。如果你是在开发环境中安装,也可以选择“Development Machine”配置类型。 6. 接下来,选择MySQL安装的位置。可以选择默认位置,也可以选择其他位置。 7. 在选择开始菜单文件夹页面中,选择是否创建开始菜单文件夹,并命名文件夹名称。 8. 在MySQL服务器配置页面中,选择使用哪种身份验证插件进行登录。可以选择“Use Strong Password Encryption”以增强安全性。 9. 在设置root密码页面中,输入你想要设置的root密码。 10. 在选择服务页面中,选择安装MySQL作为Windows服务。可以选择自动启动或手动启动。 11. 点击“执行”按钮开始安装MySQL。 12. 安装成后,点击“下一步”按钮来配置MySQL服务器。 13. 在配置MySQL服务器页面中,选择“Standalone MySQL Server/Classic MySQL Replication”配置。 14. 在配置类型页面中,选择使用默认的标准系统性能配置。 15. 在验证设置页面中,点击“执行”按钮来验证配置设置。 16. 验证成功后,点击“下一步”按钮。 17. 在应用配置页面中,点击“执行”按钮应用配置更改。 18. 成后,点击“下一步”按钮进入“Group Replication”配置页面。 19. 在Group Replication配置页面中,选择是否启用Group Replication,并选择适当的配置选项。 20. 点击“执行”按钮成配置。 21. 安装成后,点击“下一步”按钮,然后点击“退出”按钮退出安装向导。 至此,MySQL 5.7.11安装成。你可以通过命令行或其他图形界面工具来管理和操作MySQL数据库。 ### 回答2: MySQL是一种常用的关系型数据库管理系统,本文将详细介绍MySQL 5.7.11的安装教程。 1. 首先,从MySQL官网(https://dev.mysql.com/downloads/mysql/5.7.html)上下载MySQL 5.7.11的安装包。根据你的操作系统选择适合的安装包,并确保下载最新版本。 2. 下载毕后,打开安装包,并按照向导进行安装。 3. 在安装向导中,选择“Custom”(自定义)选项,然后单击“Next”(下一步)。 4. 选择安装位置,默认情况下,MySQL安装在C盘根目录下。如果你希望将MySQL安装在其他位置,可以选择相应的目录。然后单击“Next”(下一步)。 5. 在选择功能选项中,选择你需要安装数据库组件。根据你的需求,选择适当的组件。一般情况下,选择默认组件即可。然后单击“Next”(下一步)。 6. 在检查需求的选项中,系统将会检查你的系统是否满足MySQL安装需求。如果有任何问题,请按照提示修复。然后单击“Next”(下一步)。 7. 接下来,设置MySQL的密码。输入你希望的root密码,并确认密码。请注意保持密码的安全性。然后单击“Next”(下一步)。 8. 在MySQL的配置向导中,你可以选择启用或禁用MySQL的某些功能。根据你的需求进行选择。然后单击“Next”(下一步)。 9. 接下来,你可以选择是否将MySQL添加到系统的PATH环境变量中。如果你希望在任何位置都能使用MySQL命令,建议选中此选项。然后单击“Next”(下一步)。 10. 最后,单击“Execute”(执行)按钮开始安装MySQL安装过程可能需要一些时间,请耐心等待。 11. 安装成后,你可以选择启动MySQL服务,并选择是否安装MySQL的工具。然后单击“Finish”(成)。 12. 最后,打开命令提示符或终端,并输入“mysql -u root -p”命令以登录MySQL。输入之前设置的root密码,并进入MySQL。 至此,你已经成功安装MySQL 5.7.11。你可以使用命令行或其他MySQL客户端来管理和操作你的MySQL数据库。 ### 回答3: 安装MySQL 5.7.11的详细教程如下: 1. 下载MySQL 5.7.11的安装文件。可以从MySQL官方网站下载,选择适合你操作系统的版本。 2. 解压安装文件。将下载的文件解压到合适的目录中。 3. 配置MySQL安装路径。进入解压目录中,找到my.cnf文件。使用文本编辑器打开此文件,将其中的basedir和datadir的路径修改为你希望安装MySQL的位置。保存并关闭该文件。 4. 创建一个新的MySQL数据库实例。打开命令提示符或终端窗口,进入解压目录的bin文件夹中。执行命令: ``` ./mysqld --initialize --user=mysql --basedir=/your/mysql/installation/path --datadir=/your/mysql/data/path ``` 注意替换“/your/mysql/installation/path”和“/your/mysql/data/path”为你选择的安装路径和数据存储路径。 5. 启动MySQL服务。执行命令: ``` ./mysqld_safe --user=mysql & ``` 此命令会以后台进程方式启动MySQL。 6. 设置MySQL的root用户密码。在命令提示符或终端窗口中执行命令: ``` ./mysqladmin -u root password 'yourpassword' ``` 将“yourpassword”替换为你希望设置的root用户密码。 7. 设置MySQL服务自动启动。如果使用的是Linux系统,可以使用以下命令启动MySQL服务并将其设置为开机自动启动: ``` sudo systemctl start mysql sudo systemctl enable mysql ``` 如果使用的是Windows系统,在服务列表中找到MySQL服务,右键点击,选择“属性”,然后将启动类型设置为“自动”。 至此,你已经成了MySQL 5.7.11的安装。接下来你可以使用各种客户端工具连接和管理MySQL数据库了。 注意:以上步骤是一般情况下的安装流程,具体细节可能会根据操作系统和安装文件的版本有所不同。建议在安装前阅读MySQL官方文档,以获得更准确的安装指导。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值