ผู้เขียน หัวข้อ: Tuning / Optimizing my.cnf file for MySQL  (อ่าน 10385 ครั้ง)

0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

doramon

  • บุคคลทั่วไป
Tuning / Optimizing my.cnf file for MySQL
« เมื่อ: พฤศจิกายน 15, 2008, 11:37:28 AM »
0
Tuning / Optimizing my.cnf file for MySQL

http://www.linuxweblog.com/tune-my.cnf

Had to do some fine tuning of MySQL 4.1.9 and here is what my.cnf file looks like for a 2GHz machine with 1GB of memory.

[mysqld]
socket=/path/to/mysql.sock
datadir=/var/lib/mysql
skip-locking
skip-innodb
# MySQL 4.x has query caching available.
# Enable it for vast improvement and it may be all you need to tweak.
query_cache_type=1
query_cache_limit=1M
query_cache_size=32M
# max_connections=500
# Reduced to 200 as memory will not be enough for 500 connections.
# memory=key_buffer+(sort_buffer_size+read_buffer_size)*max_connections
# which is now: 64 + (1 + 1) * 200 = 464 MB
# max_connections = approx. MaxClients setting in httpd.conf file
# Default set to 100.
#max_connections=200
#interactive_timeout=180
interactive_timeout=100
#wait_timeout=180
#wait_timeout=100
# Reduced wait_timeout to prevent idle clients holding connections.
#wait_timeout=30
wait_timeout=15
connect_timeout=10
# max_connect_errors is set to 10 by default
#max_connect_errors=10
#table_cache=256
#table_cache=1024
# Checked opened tables and adjusted accordingly after running for a while.
table_cache=512
#tmp_table_size=32M by default
#thread_cache=128
# Reduced it to 32 to prevent memory hogging. Also, see notes below.
thread_cache=32
# key_buffer=258M
# Reduced it by checking current size of *.MYI files, see notes below.
key_buffer=128M
# Commented out the buffer sizes and keeping the default.
# sort_buffer_size=2M by default.
#sort_buffer_size=1M
# read_buffer_size=128K by default.
#read_buffer_size=1M
# 1Mb of read_rnd_buffer_size for 1GB RAM -- see notes below.
# read_rnd_buffer_size=256K by default.
#read_rnd_buffer_size=1M
# myisam_sort_buffer_size used for ALTER, OPTIMIZE, REPAIR TABLE commands.
# myisam_sort_buffer_size=8M by default.
#myisam_sort_buffer_size=64M
# thread_concurrency = 2 * (no. of CPU)
thread_concurrency=2
# log slow queries is a must. Many queries that take more than 2 seconds.
# If so, then your tables need enhancement.
log_slow_queries=/var/log/mysqld.slow.log
long_query_time=2

[mysql.server]
user=mysql
basedir=/var/lib

[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
open_files_limit=8192

[mysqldump]
quick
max_allowed_packet=16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[isamchk]
key_buffer=64M
sort_buffer=64M
read_buffer=16M
write_buffer=16M

[myisamchk]
key_buffer=64M
sort_buffer=64M
read_buffer=16M
write_buffer=16M

[mysqlhotcopy]
interactive-timeout

[client]
socket=/path/to/mysql.sock

Below are notes on some of the important variables, I took down while tuning the config file.

   1. query_cache_size:
          * MySQL 4 provides one feature that can prove very handy - a query cache. In a situation where the database has to repeatedly run the same queries on the same data set, returning the same results each time, MySQL can cache the result set, avoiding the overhead of running through the data over and over and is extremely helpful on busy servers.
   2. key_buffer_size:
          * The value of key_buffer_size is the size of the buffer used with indexes. The larger the buffer, the faster the SQL command will finish and a result will be returned. The rule-of-thumb is to set the key_buffer_size to at least a quarter, but no more than half, of the total amount of memory on the server. Ideally, it will be large enough to contain all the indexes (the total size of all .MYI files on the server).
          * A simple way to check the actual performance of the buffer is to examine four additional variables: key_read_requests, key_reads, key_write_requests, and key_writes.
          * If you divide the value of key_read by the value of key_reads_requests, the result should be less than 0.01. Also, if you divide the value of key_write by the value of key_writes_requests, the result should be less than 1.
   3. table_cache:
          * The default is 64. Each time MySQL accesses a table, it places it in the cache. If the system accesses many tables, it is faster to have these in the cache. MySQL, being multi-threaded, may be running many queries on the table at one time, and each of these will open a table. Examine the value of open_tables at peak times. If you find it stays at the same value as your table_cache value, and then the number of opened_tables starts rapidly increasing, you should increase the table_cache if you have enough memory.
   4. sort_buffer:
          * The sort_buffer is very useful for speeding up myisamchk operations (which is why it is set much higher for that purpose in the default configuration files), but it can also be useful everyday when performing large numbers of sorts.
   5. read_rnd_buffer_size:
          * The read_rnd_buffer_size is used after a sort, when reading rows in sorted order. If you use many queries with ORDER BY, upping this can improve performance. Remember that, unlike key_buffer_size and table_cache, this buffer is allocated for each thread. This variable was renamed from record_rnd_buffer in MySQL 4.0.3. It defaults to the same size as the read_buffer_size. A rule-of-thumb is to allocate 1KB for each 1MB of memory on the server, for example 1MB on a machine with 1GB memory.
   6. thread_cache:
          * If you have a busy server that's getting a lot of quick connections, set your thread cache high enough that the Threads_created value in SHOW STATUS stops increasing. This should take some of the load off of the CPU.
   7. tmp_table_size:
          * "Created_tmp_disk_tables" are the number of implicit temporary tables on disk created while executing statements and "created_tmp_tables" are memory-based. Obviously it is bad if you have to go to disk instead of memory all the time.

Additional reference material:

    * samspublishing.com
    * databasejournal.com
    * High Performance MySQL
    * MySQL, Linux, and Thread Caching
    * Optimising MySQL

doramon

  • บุคคลทั่วไป
Re: Tuning / Optimizing my.cnf file for MySQL
« ตอบกลับ #1 เมื่อ: พฤศจิกายน 16, 2008, 13:28:58 PM »
0
default-storage-engine=INNODB

ออฟไลน์ yord

  • Hero Member
  • *****
  • กระทู้: 1,952
  • PATTANI HOSPITAL
  • Respect: +2
    • ดูรายละเอียด
    • http://www.pattanihos.com
Re: Tuning / Optimizing my.cnf file for MySQL
« ตอบกลับ #2 เมื่อ: พฤศจิกายน 16, 2008, 19:46:50 PM »
0
default-storage-engine=INNODB
เพิ่มเข้าไปใน my.cnf ตรงไหนก็ได้เหรอครับ อ.
Mr.Yordying Kongkachan (yord02@gmail.com)
ADMIN PATTANI HOSPITAL  http://www.pattanihos.com
เริ่มใช้ HOSxP พฤศจิกายน 2549
(Master)IBM X3650 M3 QuardCord 3.06 1 CPU 20 GBs. of Ram (up to 68 GBs 29/06/56)
Linux 64bit MySQL 5.5.21 64bit (ด้วยความช่วยเหลือของ อ.บอยครับ)
(Slave1)HP ProLiant ML350 G6 Xeon E5530 2.04 20 GBs. Ram (up to 54 GBs 27/06/56)
Linux 64bit MySQL 5.1.30 64bit Database v.3.56.4.9 (29/06/56) client v.3.56.3.19

ออฟไลน์ yord

  • Hero Member
  • *****
  • กระทู้: 1,952
  • PATTANI HOSPITAL
  • Respect: +2
    • ดูรายละเอียด
    • http://www.pattanihos.com
Re: Tuning / Optimizing my.cnf file for MySQL
« ตอบกลับ #3 เมื่อ: พฤศจิกายน 16, 2008, 19:51:26 PM »
0
อ.แล้ว my.cnf ของผม เป็น ram 4G 2cpu แบบนี้ใช้ได้หรือยังครับ ;D
แล้วถ้าเปิด InnoDB ล่ะครับ ขอบคุณครับ ;D
[client]
#password   = your_password
port      = 3306
socket      = /var/lib/mysql/mysql.sock
default-character-set=tis620

# The MySQL server
[mysqld]
port      = 3306
socket      = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 1064M
max_connections = 1000
max_allowed_packet = 128M
table_cache = 1500
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 1M
myisam_sort_buffer_size = 512M
thread_cache = 8
query_cache_size= 386M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
default-character-set=tis620
skip-name-resolve
innodb_file_per_table
skip-character-set-client-handshake
init_connect = 'SET NAMES tis620'

#skip-networking

# Replication Master Server (default)
# binary logging is required for replication
#log-bin

# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /var/lib/mysql/
innodb_data_file_path = ibdata1:100M:autoextend
innodb_log_group_home_dir = /var/lib/mysql/
innodb_log_arch_dir = /var/lib/mysql/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 512M
innodb_additional_mem_pool_size = 100M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M
allow-keywords

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set=tis620

[isamchk]
key_buffer = 1640M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 1640M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
Mr.Yordying Kongkachan (yord02@gmail.com)
ADMIN PATTANI HOSPITAL  http://www.pattanihos.com
เริ่มใช้ HOSxP พฤศจิกายน 2549
(Master)IBM X3650 M3 QuardCord 3.06 1 CPU 20 GBs. of Ram (up to 68 GBs 29/06/56)
Linux 64bit MySQL 5.5.21 64bit (ด้วยความช่วยเหลือของ อ.บอยครับ)
(Slave1)HP ProLiant ML350 G6 Xeon E5530 2.04 20 GBs. Ram (up to 54 GBs 27/06/56)
Linux 64bit MySQL 5.1.30 64bit Database v.3.56.4.9 (29/06/56) client v.3.56.3.19

ออฟไลน์ อู๋kokha50

  • Hero Member
  • *****
  • กระทู้: 1,169
  • อยากได้ความรู้Hosxpต้องลงมือเรียนรู้
  • Respect: 0
    • ดูรายละเอียด
Re: Tuning / Optimizing my.cnf file for MySQL
« ตอบกลับ #4 เมื่อ: พฤศจิกายน 16, 2008, 20:17:21 PM »
0
อ.แล้ว my.cnf ของผม เป็น ram 4G 2cpu แบบนี้ใช้ได้หรือยังครับ ;D
แล้วถ้าเปิด InnoDB ล่ะครับ ขอบคุณครับ ;D


ผมไม่มี server 2 CPU เลยไม่รู้ว่าจะช่วยได้มากหรือป่าว
แต่ถ้า 1 CPU RAM 4 GB แบบนี้ถ้าเปิด InnoDB ไม่น่าจะผ่าน ครับ
ปัญหาน่าจะเกิดที่ ...

1.key_buffer = 1064M ---> น่าจะน้อยไปสำหรับ RAM 4 GB ต้องประมาณ 40-60% ของRAM4GB
2. innodb_buffer_pool_size = 512M  ---> น่าจะน้อยไปสำหรับ RAM 4 GB ต้องประมาณ 40-60% ของRAM4GB
3.innodb_log_arch_dir = /var/lib/mysql/ ---> ควรจะ Mark ไว้ด้วย #
โรงพยาบาลเกาะคา จังหวัดลำปาง โทร 054281393 ต่อ 128 Server : HP Proliant ML150 G3 Xeon 2.0 Ram 16 G  HDDSATA 160 G 2 ตัว CentOS 5.2 Linux MySQL 5.1.30-4percona
HosXp Version 3.55.5.8b
Client : winXP,  Version 3.55.5.8b
 ขึ้นระบบ 1 พ.ค.50

ออฟไลน์ yord

  • Hero Member
  • *****
  • กระทู้: 1,952
  • PATTANI HOSPITAL
  • Respect: +2
    • ดูรายละเอียด
    • http://www.pattanihos.com
Re: Tuning / Optimizing my.cnf file for MySQL
« ตอบกลับ #5 เมื่อ: พฤศจิกายน 16, 2008, 21:03:42 PM »
0
ขอบคุณครับท่าน ;D
Mr.Yordying Kongkachan (yord02@gmail.com)
ADMIN PATTANI HOSPITAL  http://www.pattanihos.com
เริ่มใช้ HOSxP พฤศจิกายน 2549
(Master)IBM X3650 M3 QuardCord 3.06 1 CPU 20 GBs. of Ram (up to 68 GBs 29/06/56)
Linux 64bit MySQL 5.5.21 64bit (ด้วยความช่วยเหลือของ อ.บอยครับ)
(Slave1)HP ProLiant ML350 G6 Xeon E5530 2.04 20 GBs. Ram (up to 54 GBs 27/06/56)
Linux 64bit MySQL 5.1.30 64bit Database v.3.56.4.9 (29/06/56) client v.3.56.3.19

doramon

  • บุคคลทั่วไป
Re: Tuning / Optimizing my.cnf file for MySQL
« ตอบกลับ #6 เมื่อ: พฤศจิกายน 17, 2008, 09:24:34 AM »
0
อ.แล้ว my.cnf ของผม เป็น ram 4G 2cpu แบบนี้ใช้ได้หรือยังครับ ;D
แล้วถ้าเปิด InnoDB ล่ะครับ ขอบคุณครับ ;D


ผมไม่มี server 2 CPU เลยไม่รู้ว่าจะช่วยได้มากหรือป่าว
แต่ถ้า 1 CPU RAM 4 GB แบบนี้ถ้าเปิด InnoDB ไม่น่าจะผ่าน ครับ
ปัญหาน่าจะเกิดที่ ...

1.key_buffer = 1064M ---> น่าจะน้อยไปสำหรับ RAM 4 GB ต้องประมาณ 40-60% ของRAM4GB
2. innodb_buffer_pool_size = 512M  ---> น่าจะน้อยไปสำหรับ RAM 4 GB ต้องประมาณ 40-60% ของRAM4GB
3.innodb_log_arch_dir = /var/lib/mysql/ ---> ควรจะ Mark ไว้ด้วย #


1.key_buffer = 1064M ---> น่าจะน้อยไปสำหรับ RAM 4 GB ต้องประมาณ 40-60% ของRAM4GB

 ??? ??? ??? ??? ??? ??? ???  ดูดีๆๆ ครับ

doramon

  • บุคคลทั่วไป
Re: Tuning / Optimizing my.cnf file for MySQL
« ตอบกลับ #7 เมื่อ: พฤศจิกายน 17, 2008, 09:38:07 AM »
0
# memory=key_buffer+(sort_buffer_size+read_buffer_size)*max_connections
# which is now: 64 + (1 + 1) * 200 = 464 MB