views:

2490

answers:

2

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)

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 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. How can I change ZF to connect via TCP instead? I am currently using PDO_MYSQL.

+2  A: 

How you connect to MySQL depends on the parameters you pass to mysql_connect(). If you pass it a file path, ie. /var/run/mysqld/mysqld.sock, it's going to attempt a socket connection. If you pass it a hostname, it will attempt a TCP connection.

You need to find the hostname (or IP) of the mysql server, and connect to it like this:

mysql_connect('127.0.0.1:3306',$username,$password);

Odds are for ZF, you can just specify this in the configuration file where you store your database settings, or when you connect to the database using the host setting:

$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    'host'     => '127.0.0.1',
    'username' => 'webuser',
    'password' => 'xxxxxxxx',
    'dbname'   => 'test'
));

Edit: If you are passing a proper hostname/port as the host parameter to mysql_connect() and getting this message, then it is most likely a server configuration issue.

Try adjusting the mysql.default_socket setting, either in php.ini or at the top of your application code using ini_set()

ini_set('mysql.default_socket','/path/to/real/socket.sock');

You'll have to figure out where the socket file is. Often it's '/var/lib/mysql/mysql.sock', but I think it's system dependent.

zombat
I am passing it a database host name. This is setup in the application.ini file along with configurations for database name, username and password.
Matt McCormick
It's going to be a server configuration issue then. Give my edited answer a try, see if that works. If not, you might have to contact your hosting company and ask them to fix the socket location. If you have access yourself, sometimes symlinking the real socket to where MySQL thinks it will be can work.
zombat
Unfortunately, I have tried these - setting the socket in application.ini, php.ini and .htaccess and none of them work although I do see the socket path change in the error message so I know it is trying to read the different paths I put.Do you happen to know where ZF uses the application.ini settings to initialize the database? Perhaps I can make a change there but I can't seem to find the code for it. I am using the latest version.
Matt McCormick
Are you specifying a `unix_socket` option at the same time that you are trying to use a regular TCP connection? The `unix_socket` portion of a PDO data source name (DSN) shouldn't be used at all if you specify a host parameter: http://www.php.net/manual/en/ref.pdo-mysql.connection.php
zombat
I have tried both ways. Currently in my application.ini file I have:resources.db.adapter = PDO_MYSQLresources.db.params.host = xxx.xxx.netresources.db.params.username = <username>resources.db.params.password = <password>resources.db.params.dbname = <dbname>
Matt McCormick
Maybe 1and1 has a different db host? Double check your hostname and your portnumber.
wenbert
I can guarantee you that 1and1 does not use sockets. They have a bunch of SQL servers, you have you find the the correct hostname and port in your control panel. They also assign DB names.
jason
A: 

I had left the line:

defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

in my index.php as is but had changed public/.htaccess to

SetEnv APPLICATION_ENV production

It looks like the .htaccess was not being read. I changed the else statement in index.php to 'production' to use the production database settings and it worked. Quite frustrating. I wonder why the environment variable was not being read. Thanks for your help.

Matt McCormick