1

I'm adding PHP (php-fpm) to an existing Nginx Ubuntu 10.04 server and can't get pdo_mysql to work. I'm trying to connect to a MySQL server someplace else, but all the Googling answers I found are in regards to MySQL not working on the local server, so I'm not sure how to proceed.

I don't have mysqld installed, so I'm not sure if I need it, or if there's a way around this? Also I'm wondering if it can't create mysqld.sock because there is mysql user on the server?

Error:

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Fatal error: Call to a member function query() on a non-object

My PHP connection code:

$this->PDO = new PDO('mysql:host=' . $this->config->getValue('db_host') .
                             ';dbname=' . $this->config->getValue('db_name'),
                             $this->config->getValue('db_user'),
                             $this->config->getValue('db_pass'));

My Query line it fails on:

$PDOStatement = $this->PDO->query($query);

Appreciate any help, thanks.

Slowfib
  • 291
  • 1
  • 4
  • 15
  • PDO doesn't create the socket file. The MySQL server does. If the socket can't be located, either MySQL isn't running, or your PHP configuration is pointing at the wrong location. – Marc B Jul 16 '11 at 22:30
  • With the following doc, you may try 'unix_socket' instead of 'db_host' : https://www.php.net/manual/zh/ref.pdo-mysql.connection.php – vrqq Feb 23 '22 at 16:35

3 Answers3

1

Couple of things you should check

  • D you have mysql-client installed? if you are connecting to remote machine. you don't need mysql running on your box but I think you will still need the client.
  • Make sure the host value is correct. Double & triple check

  • Lastly, Try connecting manually using command line, by the following command.

$ mysql -u <INSERT_USER_HERE> -p -h <INSERT_IP_OF_REMOTE_MACHINE_HERE> <INSERT_DB_NAME_HERE>

If it does not work. come back with exact error message.

CuriousMind
  • 33,537
  • 28
  • 98
  • 137
  • The `mysql-client` binary is not used, nor needed, by PHP. –  Jul 16 '11 at 23:25
  • Yup, mysql-client that's what I needed! I couldn't remember for the life of me what it was I needed. Thanks very much Gaurish, appreciate the quick response! – Slowfib Jul 16 '11 at 23:26
0

Your server is attempting to connect to a local MySQL instance. If your database is on a different remote server, make sure that the the db_host value of your configuration file points to the correct hostname of the MySQL server.

You do not need mysqld installed, but you do need MySQL client libraries.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
0

Are you sure that $this->config->getValue('db_host') is returning the correct value? The behavior you're describing sounds a lot like it's returning an empty or null value.