分类: Mysql/postgreSQL
2015-08-14 18:21:23
MySQL 5.6 Reference Manual :: 14 The InnoDB Storage Engine :: 14.11 InnoDB Startup Options and System Variables
innodb_buffer_pool_size如
果用Innodb,那么这是一个重要变量。相对于MyISAM来说,Innodb对于buffer
size更敏感。MySIAM可能对于大数据量使用默认的key_buffer_size也还好,但Innodb在大数据量时用默认值就感觉在爬了。
Innodb的缓冲池会缓存数据和索引,所以不需要给系统的缓存留空间,如果只用Innodb,可以把这个值设为内存的70%-80%。和
key_buffer相同,如果数据量比较小也不怎么增加,那么不要把这个值设太高也可以提高内存的使用率。
innodb_log_file_size对于写很多尤其是大数据量时非常重要。要注意,大的文件提供更高的性能,但数据库恢复时会用更多的时间。我一般用64M-512M,具体取决于服务器的空间。
innodb_log_buffer_size 默认值对于多数中等写操作和事务短的运用都是可以的。如果经常做更新或者使用了很多blob数据,应该增大这个值。但太大了也是浪费内存,因为1秒钟总会flush(这个词的中文怎么说呢?)一次,所以不需要设到超过1秒的需求。8M-16M一般应该够了。小的运用可以设更小一点。
innodb_flush_log_at_trx_commit (这个很管用)
抱怨Innodb比MyISAM慢100倍?那么你大概是忘了调整这个值。默认值1的意思是每一次事务提交或事务外的指令都需要把日志写入(flush)硬盘,这是很费时的。特别是使用电池供电缓存(Battery backed up
cache)时。设成2对于很多运用,特别是从MyISAM表转过来的是可以的,它的意思是不写入硬盘而是写入系统缓存。日志仍然会每秒flush到硬盘,所以你一般不会丢失超过1-2秒的更新。设成0会更快一点,但安全方面比较差,即使MySQL挂了也可能会丢失事务的数据。而值2只会在整个操作系统挂了时才可能丢数据。
innodb_flush_log_at_trx_commit
Command-Line Format |
--innodb_flush_log_at_trx_commit[=#]
|
||
System Variable | Name |
innodb_flush_log_at_trx_commit
|
|
Variable Scope | Global | ||
Dynamic Variable | Yes | ||
Permitted Values | Type |
enumeration
|
|
Default |
1
|
||
Valid Values |
0
|
||
1
|
|||
2
|
Controls the balance between strict ACID compliance for commit operations, and higher performance that is possible when commit-related I/O operations are rearranged and done in batches. You can achieve better performance by changing the default value, but then you can lose up to a second of transactions in a crash.
The default value of 1 is required for full ACID
compliance. With this value, the contents of the InnoDB
log buffer are
written out to the log
file at each transaction commit and the log file is flushed to disk.
With a value of 0, the contents of the InnoDB
log buffer are written to the
log file approximately once per second and the log file is
flushed to disk. No writes from the log buffer to the log
file are performed at transaction commit. Once-per-second
flushing is not 100% guaranteed to happen every second,
due to process scheduling issues. Because the flush to
disk operation only occurs approximately once per second,
you can lose up to a second of transactions with any mysqld process crash.
With a value of 2, the contents of the InnoDB
log buffer are written to the
log file after each transaction commit and the log file is
flushed to disk approximately once per second.
Once-per-second flushing is not 100% guaranteed to happen
every second, due to process scheduling issues. Because
the flush to disk operation only occurs approximately once
per second, you can lose up to a second of transactions in
an operating system crash or a power outage.
As of MySQL 5.6.6, InnoDB
log
flushing frequency is controlled by innodb_flush_log_at_timeout
,
which allows you to set log flushing frequency to N
seconds (where N
is 1 ...
2700
, with a default value of 1). However, any mysqld process crash can erase up to N
seconds of transactions.
DDL changes and other internal InnoDB
activities flush the InnoDB
log
independent of the innodb_flush_log_at_trx_commit
setting.
InnoDB
's crash recovery works regardless of the innodb_flush_log_at_trx_commit
setting.
Transactions are either applied entirely or erased
entirely.
For durability and consistency in a replication setup that
uses InnoDB
with transactions:
If binary logging is enabled, set sync_binlog=1
.
Always set innodb_flush_log_at_trx_commit=1
.
Many operating systems and some disk hardware fool the
flush-to-disk operation. They may tell mysqld that the flush has taken place,
even though it has not. Then the durability of transactions
is not guaranteed even with the setting 1, and in the worst
case a power outage can even corrupt InnoDB
data. Using a battery-backed disk
cache in the SCSI disk controller or in the disk itself
speeds up file flushes, and makes the operation safer. You
can also try using the Unix command hdparm to disable the caching of disk
writes in hardware caches, or use some other command
specific to the hardware vendor.