Starting with Zend Tutorial - Zend_DB_Adapter thro

2019-01-01 11:03发布

问题:

i start learning Zend Framework with the Book \"Zend Framework in Action\" in German.

Right there where it start to get interesting my PHP Unit Test throws this Error: \"Zend_Db_Adapter_Exception: SQLSTATE[HY000] [2002] No such file or directory\"

I cant find any hints at google. I did everything like in the book. Can anyone give me a hint where to search my fault?

Is this a usual beginner mistake?

回答1:

I would say that you have a problem connecting from PHP to MySQL...

Something like PHP trying to find some socket file, and not finding it, maybe ?
(I\'ve had this problem a couple of times -- not sure the error I got was exactly this one, though)


If you are running some Linux-based system, there should be a my.cnf file somewhere, that is used to configure MySQL -- on my Ubuntu, it\'s in /etc/mysql/.

In this file, there might be something like this :

socket = /var/run/mysqld/mysqld.sock


PHP need to use the same file -- and, depending on your distribution, the default file might not be the same as the one that MySQL uses.

In this case, adding these lines to your php.ini file might help :

mysql.default_socket = /var/run/mysqld/mysqld.sock
mysqli.default_socket = /var/run/mysqld/mysqld.sock
pdo_mysql.default_socket = /var/run/mysqld/mysqld.sock

(You\'ll need to restart Apache so the modification to php.ini is taken into account)

The last one should be enough for PDO, which is used by Zend Framework -- but the two previous ones will not do any harm, and can be useful for other applications.


If this doesn\'t help : can you connect to your database using PDO, in another script, that\'s totally independant of Zend Framework ?

i.e. does something like this work (quoting) :

$dsn = \'mysql:dbname=testdb;host=127.0.0.1\';
$user = \'dbuser\';
$password = \'dbpass\';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo \'Connection failed: \' . $e->getMessage();
}

If no, the problem is definitly not with ZF, and is a configuration / installation problem of PHP.

If yes... Well, it means you have a problem with ZF, and you\'ll need to give us more informations about your setup (like your DSN, for instance ? )



回答2:

Do not assume your unix_socket which would be different from one to another, try to find it.

First of all, get your unix_socket location.

$ mysql -u root -p

Enter your mysql password and login your mysql server from command line.

mysql> show variables like \'%sock%\';
+---------------+---------------------------------------+
| Variable_name | Value                                 |
+---------------+---------------------------------------+
| socket        | /opt/local/var/run/mysql5/mysqld.sock |
+---------------+---------------------------------------+

Your unix_soket could be diffrent.

Then change your php.ini, find your php.ini file from

<? phpinfo();

You maybe install many php with different version, so please don\'t assume your php.ini file location, get it from your \'phpinfo\';

Change your php.ini:

mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock

pdo_mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock

Then restart your apache or php-fpm.



回答3:

Try setting host=127.0.0.1 on your db settings file, it worked for me! :)

Hope it helps!



回答4:

i had this problem when running the magento indexer in osx. and yes its related to php problem when connecting to mysql through pdo

in mac osx xampp, to fix this you have create symbolic link to directory /var/mysql, here is how

cd /var/mysql && sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock

if the directory /var/mysql doesnt exist, we must create it with

sudo mkdir /var/mysql


回答5:

Im also suffered from this problem & simply, by adding port number after the ip address saved me.

$dsn = \'mysql:dbname=sms_messenger;host=127.0.0.1:8889\';
$user = \'root\';
$password = \'root\';

This works for Mac OS

If you don\'t know your mysql port number, then
You can easily find the port number on MAMP home page

OR

Type following command while running the MAMP server to switch the terminal into mysql

OMBP:mamp Omal$ /Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot

Then type

mysql> SHOW GLOBAL VARIABLES LIKE \'PORT\';


回答6:

This error because mysql is trying to connect via wrong socket file

try this command for MAMP servers

cd /var/mysql && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock

or

cd /tmp && sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock

and this commands for XAMPP servers

    cd /var/mysql && sudo ln -s /Applications/XAMPP/tmp/mysql/mysql.sock

or

    cd /tmp && sudo ln -s /Applications/XAMPP/tmp/mysql/mysql.sock


回答7:

It looks like mysql service is either not working or stopped. you can start it by using below command (in Ubuntu):

service mysql start

It should work! If you are using any other operating system than Ubuntu then use appropriate way to start mysql