SQLSTATE[HY000] [2002] Can't connect to local MySQL server

For programming and general questions on Zend Framework
Post Reply
mattymcmatt
Posts: 4
Joined: Wed Aug 05, 2009 2:42 pm

SQLSTATE[HY000] [2002] Can't connect to local MySQL server

Post by mattymcmatt » Wed Aug 05, 2009 2:48 pm

I have recently started using Zend Framework. I started developing on my local XAMPP server which went fine but when I uploaded to my 1and1 account, I keep getting the following error:
Message: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Stack trace:

#0 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Adapter/Pdo/Mysql.php(96): Zend_Db_Adapter_Pdo_Abstract->_connect()
#1 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Adapter/Abstract.php(444): Zend_Db_Adapter_Pdo_Mysql->_connect()
#2 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Adapter/Pdo/Abstract.php(232): Zend_Db_Adapter_Abstract->query('DESCRIBE `video...', Array)
#3 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Adapter/Pdo/Mysql.php(156): Zend_Db_Adapter_Pdo_Abstract->query('DESCRIBE `video...')
#4 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Table/Abstract.php(727): Zend_Db_Adapter_Pdo_Mysql->describeTable('videos', NULL)
#5 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Table/Abstract.php(770): Zend_Db_Table_Abstract->_setupMetadata()
#6 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Table/Abstract.php(877): Zend_Db_Table_Abstract->_setupPrimaryKey()
#7 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Table/Select.php(100): Zend_Db_Table_Abstract->info()
#8 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Table/Select.php(78): Zend_Db_Table_Select->setTable(Object(Model_DbTable_Videos))
#9 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Table/Abstract.php(912): Zend_Db_Table_Select->__construct(Object(Model_DbTable_Videos))
#10 /homepages/46/d195055874/htdocs/soap/library/Zend/Db/Table/Abstract.php(1194): Zend_Db_Table_Abstract->select()
#11 /homepages/46/d195055874/htdocs/soap/application/models/DbTable/Videos.php(8): Zend_Db_Table_Abstract->fetchAll('public = 1', 'dateAdded DESC', 15)
#12 /homepages/46/d195055874/htdocs/soap/application/controllers/IndexController.php(14): Model_DbTable_Videos->getRecent()
#13 /homepages/46/d195055874/htdocs/soap/library/Zend/Controller/Action.php(512): IndexController->indexAction()
#14 /homepages/46/d195055874/htdocs/soap/library/Zend/Controller/Dispatcher/Standard.php(288): Zend_Controller_Action->dispatch('indexAction')
#15 /homepages/46/d195055874/htdocs/soap/library/Zend/Controller/Front.php(945): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#16 /homepages/46/d195055874/htdocs/soap/library/Zend/Application/Bootstrap/Bootstrap.php(77): Zend_Controller_Front->dispatch()
#17 /homepages/46/d195055874/htdocs/soap/library/Zend/Application.php(328): Zend_Application_Bootstrap_Bootstrap->run()
#18 /homepages/46/d195055874/htdocs/soap/public/index.php(36): Zend_Application->run()
I have tried changing the
resources.db.params.unix_socket
line in my application.ini file to the different ones mentioned in phpinfo but with no success. I found here (http://stackoverflow.com/questions/7114 ... -connect-t) that someone answered
Is the MySQL server on the same host as the web application? If not, you can't use a socket connection, and will need to connect via TCP.
I think that is the case with my web host. Is there something I can change in ZF where I can connect via TCP instead?

Thanks

michaelle
Posts: 173
Joined: Sun Dec 21, 2008 1:30 pm
Contact:

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL server

Post by michaelle » Sun Aug 09, 2009 12:39 pm

You probably need to use 127.0.0.1 instead of 'localhost'

More information can be found here :

http://framework.zend.com/manual/en/zend.db.html

mattymcmatt
Posts: 4
Joined: Wed Aug 05, 2009 2:42 pm

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL server

Post by mattymcmatt » Sun Aug 09, 2009 5:25 pm

Thanks. I eventually found out that my host restricts the .htaccess settings you can set and the SetEnv setting wasn't working. I set it in index.php and then it connected.

derrida
Posts: 2
Joined: Thu Jan 28, 2010 6:20 am

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL server

Post by derrida » Tue Mar 02, 2010 7:26 am

Hi

i have the same issue i think. can you please give a detailed answer to how exactly you fixed the problem?

have you removed the setEnv from the htaccess? and what did you did to the index file in the public folder?

best regards

dennislan
Posts: 6
Joined: Tue Mar 23, 2010 8:02 am

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL server

Post by dennislan » Tue Mar 23, 2010 9:49 am

maybe u can change adapter type to "mysqli"
in application.ini

Code: Select all

resources.db.adapter         = "Mysqli"
	resources.db.isdefaulttableadapter = true

remyvaartjes2
Posts: 1
Joined: Tue Aug 03, 2010 8:57 pm

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL server

Post by remyvaartjes2 » Tue Aug 03, 2010 9:05 pm

I was trying to make the quickstart tutorial but with mysql

in my case the problem was that in my.cnf 'skip-networking' was in there.

So change to: # skip-networking
and save and restart mysql afterwards

I am on mac os x (I used textwrangler to edit my.cnf):
sudo open -b com.barebones.textwrangler /usr/local/zend/mysql/data/my.cnf
sudo /usr/local/zend/bin/zendctl.sh restart-mysql

in application.ini:

resources.db.adapter = PDO_MYSQL
resources.db.params.host = 127.0.0.1
resources.db.params.username = root
resources.db.params.password = xxxxx
resources.db.params.dbname = zendtest

Took a few hours to figure out, so hope this helps others.

treii28
Posts: 23
Joined: Fri Jun 03, 2011 5:56 pm

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL server

Post by treii28 » Fri Jun 03, 2011 11:30 pm

I can't speak as to why, but I tried the parameter:

resources.db.params.unix_socket = "/var/run/mysqld/mysqld.sock"

in the application.ini and still got an error (but a number showed up after in parenthesis)
So I removed it for kicks and created the symlink in /tmp - again got an error, this time with
a different number in parens.

So I tried modifying my /etc/my.cnf to create a /tmp/mysql.sock, removed the symlink I'd
previously created and restarted mysql - it worked. Of course this worried me that other
ubuntu-ish things might be looking in var/run/mysqld so for chuckles, I changed my.cnf back
the way it was, restarted mysqld again and added the parameter back to application.ini
- for whatever reason, it works now.

So try restarting mysqld if you are still getting an error with the extra unix_socket pointer

treii28
Posts: 23
Joined: Fri Jun 03, 2011 5:56 pm

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL server

Post by treii28 » Tue Jun 07, 2011 5:06 pm

I grabbed the ubuntu source package for the php extensions and changed the pointer for the mysql.sock to properly point to the var/run/mysql/mysqld.sock for ubuntu based configurations. I haven't extensively tested them but they appear to work as designed without need for any extra configuration options. Built as shared against a standard ubuntu repository mysql install (using /bin/mysql_config and --with-{extension}=shared configuration options)
Use at your own risk, be sure to back up *mysql*.so before installing.
Attachments
ubuntu+zend+php5-3_mysql-extensions.tar.bz2
(890 KiB) Downloaded 439 times

jjordaan
Posts: 1
Joined: Tue Jul 26, 2011 10:21 pm

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL serv

Post by jjordaan » Tue Jul 26, 2011 10:21 pm

Changing localhost to 127.0.0.1 worked for me.
Thanks :-D

smithdg
Posts: 41
Joined: Sun Mar 22, 2009 3:32 am

Re: SQLSTATE[HY000] [2002] Can't connect to local MySQL serv

Post by smithdg » Thu May 17, 2012 4:59 pm

Where is the application.ini file? I am running the latest mac os x, (lion), and have the latest version of zend.

Post Reply