MySQL

Technical Notes

These notes are my personal online notebook of useful commands and "how-to's". You are welcome to make use of them if you find them helpful. They obviously don't come with any warranty! Click on one of the category tags above for the notes in any category.

MySQL tuning

This is my experience tuning a MySQL server running on Rimuhosting for Drupal.
First I installed a tuning script in /root
wget http://day32.com/MySQL/tuning-primer.sh
chmod 700 tuning-primer.sh
yum install bc
cp /etc/my.cnf /etc/my_$(date +"%Y-%m-%d").cnf
./tuning-primer.sh

Now add the following values under [mysqld]:
table_cache = 512
query_cache_size = 32M
query_cache_type = 1
max_heap_table_size = 32M
tmp_table_size = 64M

Here is some info: http://drupal.org/node/85768

Misc Drupal database queries

This are probably not of general interest, just some example queries from a particular database that I wanted to save here as snippets.

--view all category titles from node: n & d, category node: c
$sql="SELECT n.nid, c.title AS cname, d.field_catcache_value AS cat FROM node AS n"
. " JOIN content_type_document AS d ON d.nid=n.nid AND d.vid=n.vid"
. " JOIN node AS c ON c.nid=d.field_category_nid"
. " WHERE n.type = 'document'";

--get the body of the category definition c from a given node n & d
SELECT n.nid, c.title, r.body AS cname FROM node AS n

backup database

pre{border:none;background-color:#f4f3d7;padding:1em;-moz-border-radius:10px}

<?php
exec("mysqldump -uDBUSER -pPASSWORD DBNAME > DBNAME.sql");
exec("tar -czvf BACKUP.tar.gz .");
print "Done";

Benchmarking MySQL

First make sure the right packages are installed. With Ubuntu the benchmark gets installed with mysql-server. With Red Hat/CentOS you have to ask for it under servers/MySQL/benchmarks
Ensure MySQL is running with:
service mysqld start # Red Hat/CentOS
/etc/init.d/mysql start # Ubuntu

MySQL backup

-create a user (such as backup) with select and lock priviledges for all databases from any host
-set MySQL server configuration to listen on external ports
pw=mybackuppassword
hst=MySQLhost
nice mysqldump -h $hst -u backup -p$pw --compress --add-drop-table --extended-insert -A | gzip -9 >/backups/$hst-$(date +"%Y%m%d").gz

MySQL misc tips

phpMyBackupPro: No simpler backup for MySQL: http://www.linux.com/feature/127811
Tunnel to locally running mysql server using ssh:
http://www.nerdlogger.com/2008/06/tunnel-to-locally-running-mysql-server...
create and setup remote administrator:
first make sure that there are not multiple name@addresses where the user can log in, else every one will have to get permissions separately.
CREATE USER 'andrewfoo'@'192.168.1.%' IDENTIFIED BY 'foomatic';
GRANT ALL ON *.* to 'andrewfoo'@'192.168.1.%' WITH GRANT OPTION;

Syndicate content