Heads up: Using local sockets to connect to MySQL

at 2012-01-08 in AnnouncementsDatabases by friebe (0 comments)

MySQL logoMySQL allows running MySQL servers with disabled networking. This is suggested as security measure if the application and the database server run on the same machine, but in reality we usually don't have this kind of a setup except on the developers' machines. This is where we hit an inconsistency in our userland MySQL driver "mysqlx": It wasn't connecting to the MySQL server on localhost, where the standard MySQL driver was. This has been fixed in the meantime.

Some research into this topic revealed the following:

  • MySQL uses an AF_UNIX socket on Un*x systems. By default, this is in /tmp/mysql.sock
  • On Windows, a named pipe serves the same purpose. It's usually called \\.\pipe\MySQL
  • The MySQL libraries will connect via these local sockets if "localhost" is supplied as host name

Especially the last part is a bit frustrating, because it's magic, and magic behaviour is never good in software. This is why the XP group has decided to be inconsistent with the MySQL libraries and not support this in mysqlx. Furthermore, we have decided to remove the magic from the standard MySQL driver implementations in order to be consistent. Instead, use the dot (.) as a notation for "this machine", as seen in the following examples:

  // Connect to the MySQL server on this machine via local sockets.
// Determine socket by using OS-dependant lookup mechanisms.
$conn= DriverManager::getConnection('mysql://./NEWS');

// Supply local socket's name
$conn= DriverManager::getConnection('mysql://./NEWS?socket=/tmp/mysql.sock');
$conn= DriverManager::getConnection('mysql://./NEWS?socket=\\\\.\\pipe\\mysql');
In contrast, this will now always use TCP/IP:

  $conn= DriverManager::getConnection('mysql://localhost/NEWS');



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES
Unicode
Language
5.9-SERIES

Related

Find related articles by a search for «Heads».