From 20d9c3519db7e72ea6053a00104222916e526a67 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Fri, 2 Jun 2017 12:49:49 +0300 Subject: [PATCH] Bug 11974 - Enable non-default unix socket location for database connections. ----------------- - BUSINESS CASE - ----------------- Currently Koha doesn't support using a non-default unix socket for database connections. Using unix sockets is generally ~20-30% faster compared to using TCP packets. Because Koha abuses the database a lot, this will make a difference. Using a non-default unix socket is useful if you want to run multiple databases (eg. mysql and mariadb) in the same OS (eg. for testing purposes). Also if one needs to move the unix socket, for example to share it between LXC containers using a shared partition, this feature might be handy. Using unix sockets for connections are faster than TCP because there is no need for TCP and IP layer packet generation. Instead applications can communicate directly without extra overhead. The benefit is marginal for a single query, around 0.5ms, but escalates linearly when hundreds of queries are performed. ------------ - 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 Koha::Database->_new_schema routine to check for the presence of mysql_socket in koha-conf.xml and prioritize the socket connection. This patch works on MariaDB and Mysql and hopefully on PostgreSQL. ------------- - TEST PLAN - ------------- DEFAULT: 1. edit koha-conf.xml 1.1. put your socket path to the -attribute 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. BAD SOCKET: 1. but a bad path as your mysql_socket, eg /var/run/mysqld/mysqld.sockBADBAD 2. load any koha's web-page. It should fail due to DB connection error. Thank you for testing! --- Koha/Database.pm | 22 +++++++++++++++++++++- debian/templates/koha-conf-site.xml.in | 1 + etc/koha-conf.xml | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Koha/Database.pm b/Koha/Database.pm index 7daf5f0..fb59eb3 100644 --- a/Koha/Database.pm +++ b/Koha/Database.pm @@ -58,6 +58,7 @@ sub _new_schema { my $db_port = $context->config("port") || ''; my $db_user = $context->config("user"); my $db_passwd = $context->config("pass"); + my $db_socket = $context->config("socket") || ''; my $tls = $context->config("tls"); my $tls_options; if( $tls && $tls eq 'yes' ) { @@ -80,9 +81,28 @@ sub _new_schema { $encoding_query = "set client_encoding = 'UTF8';"; $tz_query = qq(SET TIME ZONE = "$tz") if $tz; } + + #Build the dsn, it is typically like + # dbi:mysql:database=koha;host=localhost;port=3306 + # dbi:mysql:database=koha;mysql_socket=/var/run/mysql/mysql.sock + # dbi:Pg:database=koha;host=/var/run/Pg + my $dsn = "dbi:$db_driver:database=$db_name;"; + #Figure out if we are using sockets and for which DBRMS? + if ($db_socket) { + if ($db_driver eq 'Pg') { + $dsn .= "host=$db_socket;port=$db_port"; #https://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-CONNECT-HOST + } + else { #default to mysql + $dsn .= "mysql_socket=$db_socket"; + } + } + else { + $dsn .= "host=$db_host;port=$db_port"; + } + my $schema = Koha::Schema->connect( { - dsn => "dbi:$db_driver:database=$db_name;host=$db_host;port=$db_port".($tls_options? $tls_options : ""), + dsn => $dsn.($tls_options? $tls_options : ""), user => $db_user, password => $db_passwd, %encoding_attr, diff --git a/debian/templates/koha-conf-site.xml.in b/debian/templates/koha-conf-site.xml.in index e273bc8..9639148 100644 --- a/debian/templates/koha-conf-site.xml.in +++ b/debian/templates/koha-conf-site.xml.in @@ -252,6 +252,7 @@ __END_SRU_PUBLICSERVER__ __DB_NAME__ __DB_HOST__ 3306 + __DB_SOCKET__ __DB_USER__ __DB_PASS__ biblios diff --git a/etc/koha-conf.xml b/etc/koha-conf.xml index 9003a6f..52777fd 100644 --- a/etc/koha-conf.xml +++ b/etc/koha-conf.xml @@ -76,6 +76,7 @@ __PAZPAR2_TOGGLE_XML_POST__ __DB_NAME__ __DB_HOST__ __DB_PORT__ + __DB_SOCKET__ __DB_USER__ __DB_PASS__ __DB_USE_TLS__ -- 2.7.4