Tuning MySQL Performance with a neat little perl script
Searching randomly through Google for my next post topic, I came across a little MySQL Tuner perl script written by a RackSpace Senior Systems Engineer named Major Haden. Two things caught my eye.
1. It's always great to optimize things, especially MySQL.
2. Major is a seriously cool name.
Nothing about this script was lacking in coolness, so I decided to run it on my crappy little home FreeBSD development box.
fetch http://mysqltuner.com/mysqltuner.pl
chmod +x mysqltuner.pl
./mysqltuner.pl
The result?
>> MySQLTuner 1.0.0 - Major Hayden
>> Bug reports, feature requests, and downloads at http://mysqltuner.com/
>> Run with '--help' for additional options and output filtering
Please enter your MySQL administrative login:
Please enter your MySQL administrative password:
-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.35
[OK] Operating on 32-bit architecture with less than 2GB RAM
-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 756M (Tables: 26)
[!!] InnoDB is enabled but isn't being used
[!!] Total fragmented tables: 2
-------- Performance Metrics -------------------------------------------------
[--] Up for: 6d 9h 42m 52s (5K q [0.010 qps], 23 conn, TX: 482K, RX: 5M)
[--] Reads / Writes: 58% / 42%
[--] Total buffers: 34.0M global + 2.7M per thread (151 max threads)
[OK] Maximum possible memory usage: 439.8M (43% of installed RAM)
[OK] Slow queries: 0% (0/5K)
[OK] Highest usage of available connections: 1% (2/151)
[OK] Key buffer size / total MyISAM indexes: 8.0M/395.4M
[OK] Key buffer hit rate: 98.5% (32K cached / 472 reads)
[!!] Query cache is disabled
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 3 sorts)
[OK] Temporary tables created on disk: 7% (3 on disk / 38 total)
[!!] Thread cache is disabled
[OK] Table cache hit rate: 87% (49 open / 56 opened)
[OK] Open file limit used: 0% (98/11K)
[OK] Table locks acquired immediately: 100% (5K immediate / 5K locks)
[!!] Connections aborted: 13%
-------- Recommendations -----------------------------------------------------
General recommendations:
Add skip-innodb to MySQL configuration to disable InnoDB
Run OPTIMIZE TABLE to defragment tables for better performance
Enable the slow query log to troubleshoot bad queries
Set thread_cache_size to 4 as a starting value
Your applications are not closing MySQL connections properly
Variables to adjust:
query_cache_size (>= 8M)
thread_cache_size (start at 4)
Clearly I haven't gone anywhere near my MySQL config.
FreeBSD 8.0 – Code Freeze – Whats cooking? Why FreeBSD? Why are you using linux?
FreeBSD 8.0 – Code freeze [www.evilcoder.org]
In continued preparation to the FreeBSD 8.0 Release, the tree had been frozen by re@. This means that development had been slowed down a lot because everything needs to be reviewed by the Release Engineers first. This step will make sure that the system will get as stable as possible before a fork will be done to RELENG_8. Stay tuned!
What's cooking for FreeBSD 8? Plenty. Jails v2, Xen dom-U support, ULE 3.0, and DTrace will all likely make it into the release, not to mention all the other new features, stability and code improvements. What else is cooking in FreeBSD 8? Here is a very long post to pump you up.
FreeBSD - What's cooking for FreeBSD 8? [ivoras.sharanet.org]
Dizzy? Pull yourself together. From Wikipedia [en.wikipedia.org]:
As of 2009, FreeBSD 8.0 is the "bleeding edge" development version, called -CURRENT in FreeBSD development terminology. It will feature superpages [en.wikipedia.org], Xen [en.wikipedia.org] DomU support, network stack virtualization, stack-smashing protection [en.wikipedia.org], TTY layer rewrite, much improved ZFS support and, new USB stack, and rewritten NFS client/server introducing NFSv4. Inclusion of improved device mmap() extensions will allow the technical implementation of a 64-bit Nvidia display driver for the amd64 platform. FreeBSD 8.0 is planned to be released in the 3rd quarter of 2009.
Yahoo! has been using it for years. I have been using it for years. Why FreeBSD? IBM tells you why in 2005.
Why FreeBSD - A quick tour of the BSD alternative [www.ibm.com]
FreeBSD tells you why.
About FreeBSD's Internetworking [www.freebsd.org]
FreeBSD made you a slideshow about 7.0 explaining why.
Introducing FreeBSD 7.0 [people.freebsd.org]
Right now, you might be feeling anxious. You've been using some linux distro with a name you can't even pronounce or spell properly and a confusing package management system. You thought it was the best. Better than Windows right? You don't know anymore. It doesn't make sense. You're not sure if it ever did make sense. The room starts spinning. You realize this must have been a joke. A Penguin? Some guy named Linus? Ubuntu? Why didn't you see the clues? Is this the real life?
Why are you using linux?
How to install Lighttpd, PHP 5, and MySQL 5.1 on FreeBSD 7.2
Installing MySQL
Before we start, make sure your ports are updated.
portsnap fetch
portsnap update
Right. Now we're ready.
cd /usr/ports/databases/mysql51-server
make install clean
This might take a while. Do something productive.
When that finishes we'll install the database, and make sure user and group mysql is the owner of the database folder.
/usr/local/bin/mysql_install_db
chown -R mysql /var/db/mysql/
chgrp -R mysql /var/db/mysql/
Next, we'll set MySQL to start at FreeBSD boot.
echo 'mysql_enable="YES"' >> /etc/rc.conf
Finally, we'll start MySQL and change the root password to keep out the hackers.
/usr/local/etc/rc.d/mysql-server start
/usr/local/bin/mysqladmin -u root password newpassword
If you did everything right, MySQL should now be running.
Installing Lighttpd
Assuming our ports are still up to date after that MySQL compile, we'll start the Lighttpd install.
cd /usr/ports/www/lighttpd
make install clean
When that's finished, we'll create the document root folder.
mkdir -p /usr/local/www/data
Now, create the access and error log files. Those files are:
/var/log/lighttpd.access.log
/var/log/lighttpd.error.log
Next, we'll give Lighttpd ownership of it's log files.
chown www:www /var/log/lighttpd.access.log
chown www:www /var/log/lighttpd.error.log
Now it's time to open up the Lighttpd configuration file. Great.
vi /usr/local/etc/lighttpd.conf
Uncomment the line "mod_fastcgi", in "server.modules".
Append the following code in the fastcgi module section.
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => "/tmp/php-fastcgi.socket",
"bin-path" => "/usr/local/bin/php-cgi"
)
)
)
Finally, we'll set Lighttpd to start with FreeBSD.
echo 'lighttpd_enable="YES"' >> /etc/rc.conf
Before we start Lighttpd, Lets install PHP.
Installing PHP
Installing PHP is very easy. Ready? Ok.
cd /usr/ports/lang/php5
make install clean
Now we'll install some PHP extensions. Make sure you check the mysql and or mysqli boxes to be able to use MySQL in PHP.
cd /usr/ports/lang/php5-extensions
make config install clean
Next, copy php.ini-recommended to php.ini.
cp /usr/local/etc/php.ini-recommended /usr/local/etc/php.ini
With PHP installed, we can now start Lighttpd.
/usr/local/etc/rc.d/lighttpd start
It should work now, unless I missed a few steps. Create a phpinfo script in /usr/local/www/data and load it in your browser to test!
How to install MySQL 5.1 on FreeBSD 7.2
I've had trouble in the past with installing MySQL on FreeBSD. This is the best method I've used that works.
Before we start, make sure your ports are updated.
portsnap fetch
portsnap update
Right. Now we're ready.
cd /usr/ports/databases/mysql51-server
make install clean
This might take a while. Do something productive.
When that finishes we'll install the database, and make sure user and group mysql is the owner of the database folder.
/usr/local/bin/mysql_install_db
chown -R mysql /var/db/mysql/
chgrp -R mysql /var/db/mysql/
Next, we'll set MySQL to start at FreeBSD boot.
echo 'mysql_enable="YES"' >> /etc/rc.conf
Finally, we'll start MySQL and change the root password to keep out the hackers.
/usr/local/etc/rc.d/mysql-server start
/usr/local/bin/mysqladmin -u root password newpassword
Full code:
cd /usr/ports/databases/mysql51-server
make install clean
/usr/local/bin/mysql_install_db
chown -R mysql /var/db/mysql/
chgrp -R mysql /var/db/mysql/
echo 'mysql_enable="YES"' >> /etc/rc.conf
/usr/local/etc/rc.d/mysql-server start
/usr/local/bin/mysqladmin -u root password newpassword
