0
votes

When I run the following PHP code:

<?php
$username = "root";
$password = "allen123";
$conn = new PDO('mysql:host=localhost;dbname=test', $username, $password);
?>

I get the following error.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No >such file or directory' in /Users/idrisk/Colourity/site/index.php:4 Stack trace: #0 >/Users/idrisk/Colourity/site/index.php(4): PDO->__construct('mysql:host=loca...', 'root', >'allen123') #1 {main} thrown in /Users/idrisk/Colourity/site/index.php on line 4

Any ideas? I downloaded the PDO drivers via Macports by the way

EDIT

To use mysqlnd with a local MySQL server, edit /opt/local/etc/php55/php.ini and set     mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket to the path
to your MySQL server's socket file.

For mysql5, use /opt/local/var/run/mysql5/mysqld.sock
For mysql51, use /opt/local/var/run/mysql51/mysqld.sock
For mysql55, use /opt/local/var/run/mysql55/mysqld.sock
For mysql56, use /opt/local/var/run/mysql56/mysqld.sock
For mariadb, use /opt/local/var/run/mariadb/mysqld.sock
For percona, use /opt/local/var/run/percona/mysqld.sock

--->  Cleaning php55-mysql
--->  Updating database of binaries: 100.0%
--->  Scanning binaries for linking errors: 100.0%
--->  No broken files found.
1
I'm going out on a limb here, but I think that the physical path to your pdo_mysql.default_socket defined in my.cnf differs from the physical location of mysqld.sock on the server. - Ohgodwhy
@Ohgodwhy: +1 (but you probably meant php.ini for pdo_mysql.default_socket does not match my.cnf for socket; pdo_mysql.default_socket is not in my.cnf). - Amadan
@user302975 You probably have read/write permissions only for the owner of the file. You need to set write permissions to "other" to be able to write it. However that is very bad. Use sudo. System files should not be world-writable. - Mike
Also, you can open it with any text editor. For example, you can do sudo open -a TextEdit php.ini to edit it as admin in TextEdit (comparable for other editors). You're not limited to nano. - Amadan
Glad you have your machine working, even if I was wrong. As I said, not that much experience with Macports. - Amadan

1 Answers

2
votes

It looks like you have an invalid socket in your php.ini file. Change the pdo_mysql.default_socket setting to an empty string to use the default socket.