1

After an upgrade to El Capitan I'm having issues connecting to MySql. I have a basic PHP file called index.php with the following code:

<?php
  $conn = new mysqli("127.0.0.1", "xxxx", "xxxx");
  if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); 
  echo "Connected successfully";
?>

Now when I visit the page in my browser, I get the following message:

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in /Users/rich/Documents/DESIGN/test/index.php on line 3
Connection failed: Connection refused

I can't understand what's going wrong here. I should mention that I also had this problem on Yosemite before upgrade. The problem began when I wiped my computer to do a fresh install and had to setup Apache and PHP config again. I think something must have been left out of one of the files.

EDIT I've also tried using localhost in the connection instead of 127.0.0.1. That changes the error slightly to:

Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /Users/rich/Documents/DESIGN/test/index.php on line 4
Connection failed: No such file or directory
CaribouCode
  • 13,998
  • 28
  • 102
  • 174
  • Can you set the port? – FirstOne Oct 27 '15 at 14:50
  • What should the port be? – CaribouCode Oct 27 '15 at 14:50
  • possible duplicate of other questions around "no such file": [this](http://stackoverflow.com/questions/12584762/mysql-connect-no-such-file-or-directory) or [that](http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi)... – pascal Oct 27 '15 at 15:00

1 Answers1

2

Try using localhost instead of 127.0.0.1 and also make sure the username/password you are using are correct and exist on the mysql server. Also check mysqld is running and that your firewall is allowing the mysql database through.

Edit your php.ini file and make sure extension extension=php_mysqli.dll is enabled on Windows or extension=php_mysqli.so on Linux.

Try the answer here: PHP - MYSQL - test database server

There are two settings you have to check for this to work (assuming you have MySQL server installed of course):

Check the value of mysql.default_socket in your PHP configuration.

Check the value of socket in your MySQL configuration file under the [mysqld] heading.

Those values have to be identical; if they're not, change one to match the other and restart respective service.

Community
  • 1
  • 1
codeguy
  • 662
  • 4
  • 21
  • Using `localhost` instead changes the error slightly to: `Warning: mysqli::mysqli(): (HY000/2002): No such file or directory`. I have my firewall turned off so it can't be that. Username and password are correct because I can connect through sequel pro still for some reason. – CaribouCode Oct 27 '15 at 14:50
  • I've enabled `extension=php_mysql.dll` (by the way I'm on Mac not Windows or Linux). This still hasn't worked. – CaribouCode Oct 27 '15 at 14:54
  • Yes but you need to enable mysqli for the mysqli functionality. With mysql you can only use mysql_calls – codeguy Oct 27 '15 at 14:55
  • Is there a reason why someone has given me negative on this answer? these are all things to check when geting these types of errors. You do need mysqli to use mysqli calls. – codeguy Oct 27 '15 at 14:56
  • Not sure why someone downvoted my question either... As far as I can tell I have all mysqli things enabled but this still isn't working. Is there something else I need to do? – CaribouCode Oct 27 '15 at 14:58
  • By the way I only have a `php.ini.default` file, should I remain this to `php.ini`? – CaribouCode Oct 27 '15 at 14:59
  • YES :) That may be the issue! – codeguy Oct 27 '15 at 15:00
  • And restart services so the config is loaded. – codeguy Oct 27 '15 at 15:01
  • Slight progress made there. I now only get a simply error: `Connection failed: Connection refused` – CaribouCode Oct 27 '15 at 15:03
  • Sounds like mysql service is not running. Try starting it or restarting it. – codeguy Oct 27 '15 at 15:04
  • I think it might be something to do with connecting to the socket but not sure. – CaribouCode Oct 27 '15 at 15:04
  • I've restarted MySql and apache – CaribouCode Oct 27 '15 at 15:05
  • No fraid not. I'm in my file `usr/local/mysql/support-files/my-default.cnf` and everything is commented out by default. I've uncommented the `socket` line and changed it to `socket=/var/mysql/mysql.sock` – CaribouCode Oct 27 '15 at 15:13
  • Then in `php.ini` I've set: `pdo_mysql.default_socket=/var/mysql/mysql.sock` so they match. Same error still shows after restarting apache. – CaribouCode Oct 27 '15 at 15:13
  • You must have a my.cnf file for mysql config. not my-default.cnf, this wont be loaded! – codeguy Oct 27 '15 at 15:15
  • OK I've duplicated that file and called it `my.conf` (why don't these files exist by default?!?!) Now I've discovered that /var/mysql/mysql.sock doesn't exist. What do I do now? – CaribouCode Oct 27 '15 at 15:18
  • When you restart your services, these sock files are created automatically. They come like this so you can configure them to your liking. – codeguy Oct 27 '15 at 15:19
  • See if this helps @Coop http://stackoverflow.com/questions/5784791/cant-connect-to-mysql-on-mac-missing-mysql-sock-file – codeguy Oct 27 '15 at 15:25
  • I ended up taking drastic measures and wiping my computer for a fresh El Capitan install for this reason and many others. That solved this problem but I'm now having mysql password issues. Thanks for your help though. – CaribouCode Oct 28 '15 at 09:53