Monthly Archives: May 2011

MySQL database dump & restore

Putting this here as I keep having to google it (paranoid that I’d do something wrong and mess up the db more than anything else) and keep finding crap results.

[sourcecode]
mysqldump -u [username] -p [database_name] > dump.sql
# it then prompts for the password

mysql -u [username] -p [database_name] < dump.sql
# it then prompts for the password
[/sourcecode]

Using strace to analyze apache

For times that you want to know exactly what a process is doing (for instance, if your apache is Pleskified and you’re getting weird website errors that can’t be traced back to any of your own code…) you can use strace.

[sourcecode]
service apache2 stop

strace -f -o trace.txt /etc/init.d/apache2 start  # this will run strace on all child processes of apache, so you don’t need to worry about figuring out the correct process

## do whatever to generate the error message (in my case, just accessing the site)

service apache2 stop

## use htop to verify the strace has stopped

service apache2 start
[/sourcecode]

Then check trace.txt to find your issue!

And have a look here for a more thorough tutorial.

Making apache virtual host config changes to Plesk

Make changes here (assuming you’re running vhosts from this location):

[sourcecode]
/var/www/vhosts/[vhostname]/conf/vhost.conf
/var/www/vhosts/[vhostname]/conf/vhost_ssl.conf
[/sourcecode]

You’ll need to create these files if they don’t exist.

Then run:

[sourcecode]
/usr/local/psa/admin/sbin/httpdmng –reconfigure-domain [vhostname]
[/sourcecode]

Drupal 7 custom template files per content type

Very useful – got this from here where you can also find the one for D6.

As the comment suggests, if you have a content type called ‘blog’, you can create a template file to apply to nodes with this content type only called page–blog.tpl.php.

[sourcecode]

<?php
function themeName_preprocess_page(&$vars, $hook) {
if (isset(
$vars['node'])) {
// If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
}
}
?>
[/sourcecode]

 

Drupal 7 functions during JOINs / matching the URL alias with the node ID using a dynamic query

While Drupal 7 dynamic query JOINs do accept functions (CONCAT at least), I had a tough time trying to figure out how to get them to work.

In my case I wanted to pull the URL alias out of the database along with a bunch of other information on a handful of nodes (don’t use this for lots of nodes, if you can help it, as MySQL performance will not be good).  This is easy if you already have the node ID before starting the query, however it can be tricky if you don’t as the node ID is not in the url_alias table.

The only way I could get it to work was like this:

[sourcecode]
$query->join(‘url_alias’, ‘ua’, ‘ua.source = CONCAT(\’node/\’,n.nid)’);
[/sourcecode]

As you’ll notice, this will only work with taking out aliases for nodes, unfortunately.

I also tried the following which DO NOT WORK:

[sourcecode]
// substring instead of concat
$query->join(‘url_alias’, ‘ua’, ‘n.nid = SUBSTRING(ua.source FROM \’/\’)’);
// another version of substring
$query->join(‘url_alias’, ‘ua’, ‘n.nid = SUBSTRING(ua.source,5)’);
// and variations on the above, adding single, escaped single and double quotes in various places

// concat using double quotes
$query->join(‘url_alias’, ‘ua’, ‘ua.source = CONCAT(“node/”,n.nid)’);

// using concat / substring in placeholders
$query->join(‘url_alias’, ‘ua’, ‘ua.source = :node_source’, array(‘:node_source’ => ‘CONCAT(\’node/\’, n.nid)’));
$query->join(‘url_alias’, ‘ua’, ‘n.nid = :node_source’, array(‘:node_source’ => ‘SUBSTRING(ua.source,5)’));
// and variations on the above, using single/escaped single/double quotes, and different versions of SUBSTRING

// using the addExpression with a join
$query->addExpression(‘CONCAT(:source_pre,n.nid)’, ‘node_source’, array(‘:source_pre’ => ‘node/’));
or
$query->addExpression(‘CONCAT(\’node/\’,n.nid)’, ‘node_source’);
with
$query->join(‘url_alias’, ‘ua’, ‘:node_source = ua.source’, array(‘:node_source’ => ‘node_source’));
or
$query->join(‘url_alias’, ‘ua’, ‘node_source = ua.source’);

// I even tried adding an AND to the join, hoping it had some strange and magical usage
$query->join(‘url_alias’, ‘ua’, ‘n.nid = ua.source AND ua.source = :node_source’, array(‘:node_source’ => ‘CONCAT(\’node/\’, n.nid)’));

[/sourcecode]

I’m sure there are some syntax issues with some of the above examples, but it still seems the dynamic database API is pretty rigid!

ApacheBench length error failed requests

I failed to find anything in the AB documentation about interpretting AB results, specifically with regards to length errors.

Turns out AB records the length of the first request and compares subsequent requests with it, flagging an error if there’s a length difference.  So on a dynamic script there’s nothing to worry about, static page, however…