Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Apache, PHP and MySQL - Segmentation faults

Hi there,

My experience of Snow Leopard so far has been very positive, with the exception of this one problem: Apache segfaults when serving a PHP page that accesses a MySQL connection.

I am not using any custom builds of PHP or Apache. The segfault appears using the Snow Leopard bundled PHP 5.3.0 and Apache 2.2.11.

The problem manifests when connecting to either a local or remote database, and with or without the MySQL Community Server installed. I have tested this on my own MacBook Pro which has MySQL 5.1.38 x86_64 installed, and also a 12-month old iMac upgraded to Snow Leopard which has never had MySQL installed.

*Steps to reproduce:*

1. Create a php.ini file by copying */etc/php.ini.default* to */etc/php.ini*, since one is not installed by default.

2. Modify */etc/php.ini* with the following settings:

*mysql.default_port = 3306*
*date.timezone = "Europe/London"*

The MySQL port number is blank by default, causing PHP to use port 0 when trying to access a database connection. Also, unless a timezone is set PHP issues a warning. It's not critical, but it's a log-filler.

3. Open */etc/apache2/httpd.conf* and uncomment the following line to enable PHP (ie, remove the # at the start of the line):

*#LoadModule php5_module libexec/apache2/libphp5.so*

4. Go to "System Preferences > Sharing" and check "Web Sharing" to start Apache, or type "sudo apachectl graceful" in a terminal to restart it if it's already running.

5. Create a new file called "mysql.php" in ~/Sites and paste the following:

*<?php*
*$database = 'localhost';*
*$username = 'root';*
*$password = '';*

*mysql_connect($database,$username,$password);*
*mysql_close();*
*?>*

Obviously if you do not have MySQL Community Edition installed you should replace "localhost" with the location of your database server. Also replace the username/password with correct credentials.

6. Produce the segfault with one of the following:

a. Type *cd ~/Sites ; php mysql.php* in a terminal. You will see "Segmentation fault".

b. Accessing the page in a browser at * http://localhost/~yourshortusername/mysql.php* produces a plain white page with no error.

In both cases an error is logged to */var/log/apache2/error_log* that looks something like this:

*\[Thu Sep 03 16:01:11 2009\] \[notice\] child pid 2518 exit signal Segmentation fault (11)*



Doing some quick googling reveals others are having the same or similar problems, and also with extensions other than the MySQL one. Workarounds exist in compiling your own Apache/PHP, but I want to avoid this on my own setup.

Hopefully this information will help Apple to fix the problem in a Software Update.

Thanks for reading,
Conan

MacBookPro5,2, Mac OS X (10.6), 2.93GHz Core 2 Duo, SSD

Posted on Sep 3, 2009 8:19 AM

Reply
5 replies

Sep 7, 2009 8:06 AM in response to mazcunan

Thanks for the *mysqli_* suggestion. It has helped me locate where the *mysql_* code was tripping up.

It turns out that this version of PHP doesn't like it when you do not pass around the database-handle created by *mysql_connect* to functions that can receive it, like *mysql_select_db*, *mysql_query*, and *mysql_close* for example.

So, to fix the code I posted previously:

*<?php*
*$database = 'localhost';*
*$username = 'root';*
*$password = '';*

*$dbh = mysql_connect($database,$username,$password);*
*mysql_close($dbh);*
*?>*

The PHP documentation does explicitly state that you do NOT need to pass around the handle if you are only making a single connection, so it's definitely a bug.

However, anything that helps avoid ambiguity in your code is beneficial, so it's better practice to pass a handle to functions that expect it -- even if the documentation says they don't require it!

Sep 7, 2009 9:25 PM in response to shuckster

I spent two hours installing/re-installing/reconfiguring MySQL and PHP with SL...only to find that this was the exact problem. Thanks!

To add to your findings as well: I was able to correct the issue by adding the database-handle to the mysql_close function only. I did not need to add it to mysql selectdb, mysql_query, mysql numrows, mysql fetcharray, etc.

Apache, PHP and MySQL - Segmentation faults

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.