MySQL Database my.cnf Optimization with MySQLTuner

mysql logoServer optimization always include MYSQL optimization which plays an important role in the overall performance of the server. Optimization of my.cnf file which is located at /etc/my.cnf is very important to ensure good performance of MYSQL .

I am putting forward a optimized my.cnf for a 2GHZ processor and 1GB RAM.

Kindly do not copy and paste the codes blindly. Check the server configuration and optimize my.cnf file accordingly.

# cat /etc/my.cnf

# No community software I am aware of actually needs LOAD DATA LOCAL,
# so I just disable it. See:
local-infile= 0
# MySQL 4.x has query caching available.

# Enable it for vast improvement and it may be all you need to tweak.
# 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.
# Reduced wait_timeout to prevent idle clients holding connections.
# max_connect_errors is set to 10 by default
# Checked opened tables and adjusted accordingly after running for a while.
#tmp_table_size=32M by default
# Reduced it to 32 to prevent memory hogging. Also, see notes below.
# key_buffer=258M
# Reduced it by checking current size of *.MYI files, see notes below.
# Commented out the buffer sizes and keeping the default.
# sort_buffer_size=2M by default.
# read_buffer_size=128K by default.
# 1Mb of read_rnd_buffer_size for 1GB RAM -- see notes below.
# read_rnd_buffer_size=256K by default.
# myisam_sort_buffer_size used for ALTER, OPTIMIZE, REPAIR TABLE commands.
# myisam_sort_buffer_size=8M by default.
# thread_concurrency = 2 * (no. of CPU)
# log slow queries is a must. Many queries that take more than 2 seconds.
# If so, then your tables need enhancement.




# Remove the next comment character if you are not familiar with SQL





Using SHOW STATUS MySQL command will allow you to monitor some of the variables like Threads_created , Created_tmp_disk_tables , Created_tmp_tables and so on.

Some important concepts:

  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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. tmp_table_size can be set to from 32MB upto 512MB. Remember, max_heap_table_size and tmp_table_size should always be set to same values.
  • local-infile: This can be set to 0 if LOAD DATA LOCAL is not used on the server. Remember, if you are using LOAD DATA LOCAL then it local-infile should be set to 1 in the my.cnf so that when mysql restarts, LOAD DATA LOCAL is still enabled on the server.

Using MySQLTuner for my.cnf optimization:

MySQLTuner is a Perl script that analyzes your MySQL performance and, based on the statistics it gathers, gives recommendations which variables you should adjust in order to increase performance. That way, you can tune your my.cnf file to tease out the last bit of performance from your MySQL server and make it work more efficiently.

# wget
# perl

On executing MySQLTuner, it will show the suggestions.

If any feedback, queries are always welcome!

Share on Facebook0
Share on Google+4Tweet about this on TwitterShare on StumbleUpon0Share on LinkedIn0Share on Tumblr0Digg thisPin on Pinterest0Share on Reddit0Email this to someonePrint this page

About Abhijit Sandhan

Abhijit Sandhan
Loves Linux, Blogging, Traveling, Hiking and sharing knowledge!

One comment

  1. Thank you for Mysql optimization. My mysql usage in hosting seems 140% after making some changes with your help, i decrease it 84% nearly half.
    Thanks again.

Leave a Reply

Your email address will not be published. Required fields are marked *