# Snow Leopard PHP & MySQL, can't connect



## waiting_for_OSX (Sep 4, 2009)

I've enabled php in httpd.conf, copied php.ini.default to php.ini, and it works fine with  
<?php phpinfo(); ?>

However, when I attempt to connect to the running MySQL 5.1.38 database ( installed today, 64 bit version ) via php, I get an "unable to connect" error from this code:

<html> 
<head> 
<basefont face="Arial"> 
</head> 
<body> 

<?php 

// set database server access variables: 
$host = "localhost"; 
$user = "root"; 
$pass = "mypw"; 
$db = "mydb"; 

// open connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 

etc...

I can connect using
mysql -h localhost -u root

Any suggestions would be welcome.


----------



## ElDiabloConCaca (Sep 4, 2009)

Are your usernames and passwords correct?

What I mean is that you're trying to connect to the mySQL database with the username "root" and the password "mypw" and trying to connect to the database "mydb."  Are these correct, or did you "lift" this code from somewhere without changing those username/password/database values to match your mySQL setup?


----------



## waiting_for_OSX (Sep 4, 2009)

Yes, I did take the code from the web, however, I made sure to change the parameters to make certain they were correct for my installation. I did change the parameter values again, for posting purposes.


----------



## waiting_for_OSX (Sep 4, 2009)

I suspect that there is something I need to configure for PHP to properly connect to MySQL, however I do not know what that might be.


phpinfo() outputs:

mysql

MySQL Support	enabled
Active Persistent Links	0
Active Links	0
Client API version	mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $
Persistent cache	enabled
put_hits	0
put_misses	0
get_hits	0
get_misses	0
size	2000
free_items	2000
references	2

Directive	Local Value	Master Value
mysql.allow_local_infile	On	On
mysql.allow_persistent	On	On
mysql.cache_size	2000	2000
mysql.connect_timeout	60	60
mysql.default_host	no value	no value
mysql.default_password	no value	no value
mysql.default_port	no value	no value
mysql.default_socket	/var/mysql/mysql.sock	/var/mysql/mysql.sock
mysql.default_user	no value	no value
mysql.max_links	Unlimited	Unlimited
mysql.max_persistent	Unlimited	Unlimited
mysql.trace_mode	Off	Off


mysqli

MysqlI Support	enabled
Client API library version	mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $
Active Persistent Links	0
Inactive Persistent Links	0
Active Links	0
Persistent cache	enabled
put_hits	0
put_misses	0
get_hits	0
get_misses	0
size	2000
free_items	2000
references	2

Directive	Local Value	Master Value
mysqli.allow_local_infile	On	On
mysqli.allow_persistent	On	On
mysqli.cache_size	2000	2000
mysqli.default_host	no value	no value
mysqli.default_port	3306	3306
mysqli.default_pw	no value	no value
mysqli.default_socket	/var/mysql/mysql.sock	/var/mysql/mysql.sock
mysqli.default_user	no value	no value
mysqli.max_links	Unlimited	Unlimited
mysqli.max_persistent	Unlimited	Unlimited
mysqli.reconnect	Off	Off

mysqlnd

mysqlnd	enabled
Version	mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $
Command buffer size	2048
Read buffer size	32768
Collecting statistics	Yes
Collecting memory statistics	No


----------



## waiting_for_OSX (Sep 4, 2009)

Problem solved.

Edit the /etc/php.ini file. Everywhere you find 

/var/mysql/mysql.sock

change to

/tmp/mysql.sock

then restart the web server.


----------



## rd2pardise (Sep 8, 2009)

I had the same problem. 

I followed your advise after which php actually worked. I jumped for joy only to come back later and have the same problem all over again. 

What's the deal? 

Please help!!


----------



## MtnBiker (Jan 12, 2010)

waiting_for_OSX: Solution worked for me with Snow Leopard.

Although I haven't tested after a reboot. And as is I have to restart my Web Server after each computer reboot.

Thanks


----------

