<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>workbench @ haefelinger.it &#187; php</title>
	<atom:link href="http://workbench.haefelinger.it/archives/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://workbench.haefelinger.it</link>
	<description>Notes, thouhts and other stuff on software development</description>
	<lastBuildDate>Wed, 07 Apr 2010 07:02:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MacPorts, MySQL, PHP5 and that socket thing ..</title>
		<link>http://workbench.haefelinger.it/archives/162</link>
		<comments>http://workbench.haefelinger.it/archives/162#comments</comments>
		<pubDate>Sat, 13 Feb 2010 20:36:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[macosx]]></category>
		<category><![CDATA[macports]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://workbench.haefelinger.it/?p=162</guid>
		<description><![CDATA[I&#8217;m using MacPorts to have a variety of Open-Source software on my computer. Amongst them MySQL and Apache&#8217;s web server with PHP. In addition I&#8217;m also running Wordpress, a popular blog software, and  Mediawiki, the software behind Wikipedia. Both applications are written in PHP and can be configured to use MySQL as database. 
I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using MacPorts to have a variety of Open-Source software on my computer. Amongst them MySQL and Apache&#8217;s web server with PHP. In addition I&#8217;m also running Wordpress, a popular blog software, and  Mediawiki, the software behind Wikipedia. Both applications are written in PHP and can be configured to use MySQL as database. </p>
<p>I install those two web applications myself rather than using MacPorts to do so. The installation is basically nothing more than unzipping the distribution package and to modify the configuration files to make sure they connect to my MySQL database on my <em>localhost</em>. However, while installing Wordpress went smooth, I run into <em>database connection failures</em> when installing Mediawiki. It took me some time to figure out what is going on here.</p>
<p>If you have weird connection problems using PHP and MySQL then try one or both of the following solutions:</p>
<ul>
<li><strong>Solution</strong>: Use <code>127.0.0.1</code> instead of <code>localhost</code> as host of your MySQL database server. This has a rather surprising effect, see below.
</li>
<li><strong>Solution</strong>: Change your PHP&#8217;s configuration file, which is  <code>/opt/local/etc/php5/php.ini</code>, to contain the following two lines:
<ol>
<li>Tell PHP about the Unix domain socket to use
<pre>
   mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
</pre>
</li>
<li> In addtion make sure that PHP&#8217;s save mode is off (notice that this feature has been deprecated and removed in PHP 6), otherwise your previous modification will be gracefully ignored:
<pre>
    safe_mode = Off
</pre>
</li>
</ol>
<p>Notice that in addition you need to <strong>restart your Apache web server</strong> so that your changes are taken into account. You can do that with <code>apachectl restart</code>.
</li>
</ul>
<h4>Background Information</h4>
<p>Neither Mediawiki nor Wordpress (nor most other PHP based web application accessing MySQL) implement the gory details how to connect to a database. Usually they just delegate this to <code>mysql_connect();</code>, a API function provided by PHP.</p>
<p>PHP in turn uses the functionality of a (shared) library provided by MySQL, i.e. the MySQL driver. MySQL allows you to connect to the database via <strong>two routes</strong>:</p>
<ol>
<li>via <strong>Internet sockets</strong> (TCP/IP); and via</li>
<li><strong>UNIX domain sockets</strong></li>
</ol>
<p>So, how does one choose between this two routes? The answer is rather surprising. MySQL decided to give <em>localhost</em> a new meaning. If you specify <code>localhost</code>, then the UNIX domain socket route is chosen. If you say <code>127.0.0.1</code> instead, then you connect via an Internet socket. While this is probably the most natural thing for those MySQL folks, it&#8217;s a rather confusing fact for all other humans. It is rather carved deep in our brain for years now that  <code>127.0.0.1</code> and <code>localhost</code> is the same thing.</p>
<p>After I have changed both Mediawiki and Wordpress to use <code>localhost</code> I had for the first time a situation where both web applications behaved in the very same way. <em>They both did not work</em>. Then, when I changed to <code>127.0.0.1</code>, both worked fine. Btw, notice that <code>localhost</code> is the default <em>hostname</em> used by Mediawiki while  <code>127.0.0.1</code> is the default <em>host</em> suggested by Wordpress.</p>
<p>Using <code>127.0.0.1</code> is usually fine, however I <strong>do want</strong> to use the UNIX socket and I was wondering what went wrong.</p>
<p>The problem is that MacPorts MySQL port patches the original MySQL sources to use socket <code>/opt/local/var/run/mysql5/mysqld.sock</code> instead <code>/tmp/mysql.sock</code>. This change is not particular well documented. One would assume that this kind of change goes into an appropriate <code>my.conf</code> configuration file. That&#8217;s actually not the case. Even worse, MacPorts MySQL port does not even ship with such a configuration file. </p>
<p>Changing the default socket location is one thing, not telling us and the PHP5 maintainer this is another sin. So, while MySQL happily uses that weird socket location, PHP5 still expects <code>/tmp/mysql.sock</code>. In other words, PHP and MySQL are not working out of the box when using MacPorts. At least not when relying on Unix domain sockets. It does work when using Internet domain sockets cause that default port, being 3306, has not changed. Btw, the MacPorts maintainer of PHP and MySQL is one and the same person. Sounds familiar? Yes, I also have been thinking about <em>Dr. Jekyll and Mr. Hyde</em>. No serious. I assume that there is a serious reason why this does not work out of the box as we all wish. I just don&#8217;t get it.</p>
]]></content:encoded>
			<wfw:commentRss>http://workbench.haefelinger.it/archives/162/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
