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:
$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
解决方案
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.
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.