本节介绍在可以为数据库服务器提供更多和更快的存储硬件时,如何配置存储设备。有关优化 InnoDB 配置以提高 I/O 性能的信息,请参见 第 10.5.8 节,“优化 InnoDB 磁盘 I/O”。This section describes ways to configure storage devices when
you can devote more and faster storage hardware to the database
server. For information about optimizing an
InnoDB configuration to improve I/O
performance, see Section 10.5.8, “Optimizing InnoDB Disk I/O”.
磁盘寻道是一个巨大的性能瓶颈。当数据量开始增长到以致于有效缓存变得不可能时,这个问题会变得更加明显。对于大数据库,如果你以或多或少随机的方式访问数据,你可以确定读取至少需要一次磁盘寻道,写入则需要几次磁盘寻道。为了最小化这个问题,使用寻道时间低的磁盘。Disk seeks are a huge performance bottleneck. This problem becomes more apparent when the amount of data starts to grow so large that effective caching becomes impossible. For large databases where you access data more or less randomly, you can be sure that you need at least one disk seek to read and a couple of disk seeks to write things. To minimize this problem, use disks with low seek times.
通过符号链接文件到不同的磁盘或将磁盘进行条带化来增加可用磁盘轴数(从而减少寻道开销):Increase the number of available disk spindles (and thereby reduce the seek overhead) by either symlinking files to different disks or striping the disks:
使用符号链接Using symbolic links
这意味着,对于
MyISAM表,您可以将索引文件和数据文件从数据目录中的常规位置通过符号链接指向另一个磁盘(也可以是条带化的磁盘)。假设该磁盘没有用于其他目的,这可以改善查找和读取时间。参见 第 10.12.2 节,“使用符号链接”。This means that, forMyISAMtables, you symlink the index file and data files from their usual location in the data directory to another disk (that may also be striped). This makes both the seek and read times better, assuming that the disk is not used for other purposes as well. See Section 10.12.2, “Using Symbolic Links”.符号链接不支持用于
InnoDB表。然而,可以将InnoDB数据和日志文件放在不同的物理磁盘上。有关更多信息,请参见 第 10.5.8 节,“优化 InnoDB 磁盘 I/O”。Symbolic links are not supported for use withInnoDBtables. However, it is possible to placeInnoDBdata and log files on different physical disks. For more information, see Section 10.5.8, “Optimizing InnoDB Disk I/O”.条带化意味着你有许多磁盘,并且将第一个块放在第一个磁盘上,第二个块放在第二个磁盘上,第
N块放在(N MOD number_of_disks)磁盘上,依此类推。这意味着如果你的常规数据大小小于条带大小(或完全对齐),你会获得更好的性能。条带化非常依赖操作系统和条带大小,因此请使用不同的条带大小对你的应用进行基准测试。参见第 10.13.2 节,“使用你自己的基准测试”。Striping means that you have many disks and put the first block on the first disk, the second block on the second disk, and theN-th block on the () disk, and so on. This means if your normal data size is less than the stripe size (or perfectly aligned), you get much better performance. Striping is very dependent on the operating system and the stripe size, so benchmark your application with different stripe sizes. See Section 10.13.2, “Using Your Own Benchmarks”.NMODnumber_of_disks条带化的速度差异非常依赖于参数。根据你如何设置条带化参数和磁盘数量,可能会得到数量级的差异。你必须选择优化随机访问还是顺序访问。The speed difference for striping is very dependent on the parameters. Depending on how you set the striping parameters and number of disks, you may get differences measured in orders of magnitude. You have to choose to optimize for random or sequential access.
为了可靠性,你可能想使用 RAID 0 1(条带化加镜像),但在这种情况下,你需要 2 ×
N个硬盘来存储N个硬盘的数据。如果你有足够的预算,这可能是最好的选择。不过,你也可能需要投资一些卷管理软件来有效地处理它。For reliability, you may want to use RAID 0+1 (striping plus mirroring), but in this case, you need 2 ×Ndrives to holdNdrives of data. This is probably the best option if you have the money for it. However, you may also have to invest in some volume-management software to handle it efficiently.一个不错的选择是根据数据的重要性来改变 RAID 级别。例如,将可以重新生成的半重要数据存储在 RAID 0 磁盘上,但将真正重要的数据,如主机信息和日志,存储在 RAID 0 1 或 RAID
N磁盘上。如果写入量很大,RAIDN可能会成为问题,因为更新奇偶校验位需要时间。A good option is to vary the RAID level according to how critical a type of data is. For example, store semi-important data that can be regenerated on a RAID 0 disk, but store really important data such as host information and logs on a RAID 0+1 or RAIDNdisk. RAIDNcan be a problem if you have many writes, due to the time required to update the parity bits.您还可以设置数据库使用的文件系统的参数:You can also set the parameters for the file system that the database uses:
如果你不需要知道文件的最后访问时间(这在数据库服务器上其实并不太有用),你可以使用
-o noatime选项来挂载文件系统。这会跳过文件系统 inode 上最后访问时间的更新,从而避免一些磁盘寻道。If you do not need to know when files were last accessed (which is not really useful on a database server), you can mount your file systems with the-o noatimeoption. That skips updates to the last access time in inodes on the file system, which avoids some disk seeks.在许多操作系统上,你可以通过使用
-o async选项挂载文件系统,使其异步更新。如果你的计算机相对稳定,这应该能在不牺牲过多可靠性的情况下提高性能。(在 Linux 上这个选项默认是开启的。)On many operating systems, you can set a file system to be updated asynchronously by mounting it with the-o asyncoption. If your computer is reasonably stable, this should give you better performance without sacrificing too much reliability. (This flag is on by default on Linux.)
在考虑是否将 NFS 与 MySQL 一起使用时,您应该谨慎。可能出现的问题因操作系统和 NFS 版本而异,包括以下内容:You should be cautious when considering whether to use NFS with MySQL. Potential issues, which vary by operating system and NFS version, include the following:
放置在 NFS 卷上的 MySQL 数据和日志文件可能会被锁定并无法使用。在多实例 MySQL 访问相同数据目录或 MySQL 非正常关闭(例如因停电)时,可能会出现锁定问题。NFS 版本 4 通过引入建议性锁定和基于租约的锁定解决了底层锁定问题。然而,不建议在多个 MySQL 实例之间共享数据目录。MySQL data and log files placed on NFS volumes becoming locked and unavailable for use. Locking issues may occur in cases where multiple instances of MySQL access the same data directory or where MySQL is shut down improperly, due to a power outage, for example. NFS version 4 addresses underlying locking issues with the introduction of advisory and lease-based locking. However, sharing a data directory among MySQL instances is not recommended.
由于消息接收顺序混乱或网络流量丢失而引入的数据不一致。为避免此问题,请使用带有
hard和intr挂载选项的 TCP。Data inconsistencies introduced due to messages received out of order or lost network traffic. To avoid this issue, use TCP withhardandintrmount options.最大文件大小限制。NFS 版本 2 客户端只能访问文件的前 2GB(带符号 32 位偏移)。NFS 版本 3 客户端支持更大的文件(最多 64 位偏移)。支持的最大文件大小还取决于 NFS 服务器的本地文件系统。Maximum file size limitations. NFS Version 2 clients can only access the lowest 2GB of a file (signed 32 bit offset). NFS Version 3 clients support larger files (up to 64 bit offsets). The maximum supported file size also depends on the local file system of the NFS server.
在专业的 SAN 环境或其他存储系统中使用 NFS 通常比在此类环境之外使用 NFS 更可靠。然而,在 SAN 环境中使用 NFS 的速度可能比直接连接或总线连接的非旋转存储设备慢。Using NFS within a professional SAN environment or other storage system tends to offer greater reliability than using NFS outside of such an environment. However, NFS within a SAN environment may be slower than directly attached or bus-attached non-rotational storage.
如果您选择使用 NFS,建议使用 NFS 版本 4 或更高版本,并在部署到生产环境之前彻底测试您的 NFS 设置。If you choose to use NFS, NFS Version 4 or later is recommended, as is testing your NFS setup thoroughly before deploying into a production environment.