From 4d3c4b468d7e83db53bbf06800a84fd9f77f8742 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Thu, 20 Mar 2014 14:35:57 +0200 Subject: [PATCH] Bug 11974 - Enable unix socket connections for database connections. ----------------- - BUSINESS CASE - ----------------- Currently Koha doesn't support using a non-default unix socket for database connections. Using unix sockets is generally ~30-40% faster compared to using TCP packets. Because Koha abuses the database a lot, this will make a difference. ------------ - SOLUTION - ------------ Mysql uses unix sockets for localhost connections only, but DBI::Mysql cannot automatically find the non-default socket path, thus it needs to be explicitly given. This patch modifies the C4::Context->dbh() routine to check for the presence of mysql_socket in koha-conf.xml and prioritize the socket connection. If making the socket connection fails, dbh() defaults to the TCP-connection if available. This patch works on MariaDB and Mysql, hence the configuration variable name mysql_socket. ------------- - TEST PLAN - ------------- DEFAULT: 1. edit koha-conf.xml 1.1. uncomment row ... and put your socket path here, by default /var/run/mysqld/mysqld.sock . or create the xml-entry /var/run/mysqld/mysqld.sock 2. load any koha's web-page. Shouldn't get any errors. SOCKET ONLY: replicate DEFAULT and do the following instructions: 3. From koha-conf.xml, remove hostname or set to bad name. 4. load any koha's web page. 5. Mysql/MariaDB connection works faster than ever! BAD SOCKET: 1. but a bad path as your mysql_socket, eg /var/run/mysqld/mysqld.sockBADBAD 2. load any koha's web-page. see your koha-error_log or koha-opac-error_log for the error. You can still use Koha normally, but it is recommended to fix bad socket ASAP. TOTAL DESTRUCTION: replicate BAD SOCKET and do the following instructions: 3. from koha-conf.xml remove hostname or change to a bad one. 4. load any koha's web page. Enjoy the "Software error". Thank you for testing! --- C4/Context.pm | 19 ++++++++++++++++--- etc/koha-conf.xml | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 5a76137..453a89d 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -806,9 +806,22 @@ sub _new_dbh my $db_port = $context->config("port") || ''; my $db_user = $context->config("user"); my $db_passwd = $context->config("pass"); - # MJR added or die here, as we can't work without dbh - my $dbh = DBI->connect("DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port", - $db_user, $db_passwd, {'RaiseError' => $ENV{DEBUG}?1:0 }) or die $DBI::errstr; + my $db_mysql_socket = $context->config("mysql_socket") || ''; + + my $dbh; + #if a unix socket is given use that instead of TCP (as it is way faster!) ONLY Mysql-derivatives + # Instead of dying, just warn about the error because we can fall back to a TCP-connection. + if ($db_mysql_socket && ($db_driver =~ /mysql/i || $db_driver =~ /mariadb/i)) { + # MJR added or die here, as we can't work without dbh + $dbh = DBI->connect("DBI:$db_driver:dbname=$db_name;mysql_socket=$db_mysql_socket", + $db_user, $db_passwd, {'RaiseError' => $ENV{DEBUG}?1:0 }) or warn $DBI::errstr; + } + #Try making a TCP connection + if ((! defined ($dbh))) { + # MJR added or die here, as we can't work without dbh + $dbh = DBI->connect("DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port", + $db_user, $db_passwd, {'RaiseError' => $ENV{DEBUG}?1:0 }) or die $DBI::errstr; + } # Check for the existence of a systempreference table; if we don't have this, we don't # have a valid database and should not set RaiseError in order to allow the installer diff --git a/etc/koha-conf.xml b/etc/koha-conf.xml index 72da2de..8f1be34 100644 --- a/etc/koha-conf.xml +++ b/etc/koha-conf.xml @@ -271,6 +271,7 @@ __PAZPAR2_TOGGLE_XML_POST__ __DB_NAME__ __DB_HOST__ __DB_PORT__ + __DB_USER__ __DB_PASS__ biblios -- 1.8.1.2