Vanilla forum: MySQL sockets installation

This morning I wanted to hack around with Vanilla forums and convert a phpBB 3 installation over to it. After several attempts at the installer I found that I couldn’t run it ‘as is’ on my 1&1 managed server. The issue is that Vanilla’s PDO connection method has been written to only work with host access (such as localhost, or 127.0.0.1) rather than socket access (see http://www.php.net/manual/en/ref.pdo-mysql.connection.php). To get it working using a socket like: localhost:/tmp/mysql5.sock took a little hacking around in the code. Here is a wee how-to for anyone else that might come across the same problem.

There are two files that you need to be edit before you start the install:

library/core/functions.general.php
library/database/class.database.php

First open library/core/functions.general.php and find the function called GetConnectionString (I found it on line 680). It looks like this:

if (!function_exists('GetConnectionString')) {
   function GetConnectionString($DatabaseName, $HostName = 'localhost', $ServerType = 'mysql') {
      $HostName = explode(':', $HostName);
      $Port = count($HostName) == 2 ? $HostName[1] : '';
      $HostName = $HostName[0];
      $String = $ServerType.':host='.$HostName;
      if ($Port != '')
         $String .= ';port='.$Port;
      return $String .= ';dbname='.$DatabaseName;
   }
}

A simple change to the code here is to change the line:

      $String = $ServerType.':host='.$HostName;

to:

      $String = $ServerType.':unix_socket='.$HostName;

Save the file. This will ensure that you can run the install. Now you need to make sure that following the install you can point to the right database.

Moving on to the library/database/class.database.php file open this and look for the following code (line 183-195):

if(!isset($Dbname)) {
  $Dsn = $DefaultConfig['Dsn'];
} else {
  if(empty($Port)) {
    // Was the port explicitly defined with the host name? (ie. 127.0.0.1:3306)
    $Host = explode(':', $Host);
    $Port = count($Host) == 2 ? $Host[1] : '';
    $Host = $Host[0];
  }
 
  if(empty($Port)) {
    $Dsn = sprintf('host=%s;dbname=%s;', $Host, $Dbname);
  } else {
    $Dsn = sprintf('host=%s;port=%s;dbname=%s;', $Host, $Port, $Dbname);
  }
}

The initial if(!$Port) conditional needs to be commented out (or removed) and the second one needs the host changing to unix_socket. When you’re done it will look like this:

if(!isset($Dbname)) {
  $Dsn = $DefaultConfig['Dsn'];
} else {
  /* **This could be deleted**  if(empty($Port)) {
    // Was the port explicitly defined with the host name? (ie. 127.0.0.1:3306)
    $Host = explode(':', $Host);
    $Port = count($Host) == 2 ? $Host[1] : '';
    $Host = $Host[0];
  }  ** Stop deleting here */
 
  if(empty($Port)) {
    $Dsn = sprintf('unix_socket=%s;dbname=%s;', $Host, $Dbname);
  } else {
    $Dsn = sprintf('unix_socket=%s;port=%s;dbname=%s;', $Host, $Port, $Dbname);
  }
}

When this is done go to the install page and complete all your database access details as normal, until you come to the host. You need to remove localhost from your host; it should look something like /tmp/mysql5.sock rather than localhost:/tmp/mysql5.sock.

The possible issue with this is that you will not be able use this hack if you’re running MySQL on a non-default port (anything other than 3306) as we have just removed the code that Vanilla uses to strip the port number from the host.

Enjoy!

Disclaimer: This worked for me. I can’t guarantee that it will for you. It’s never a great idea to start playing around with code so if you break anything it’s not my fault!

Jamie Davies is an occasionally brilliant, 33-year-old assistant principal, teacher, author, data scientist and educationalist from Yorkshire.

3 comments: On Vanilla forum: MySQL sockets installation

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: