1

Very strange problem.

On my Mac I've setup apache, mysql etc to do some wordpress / PHP development locally.

mysql installed fine, this is the version below

mysql  Ver 8.0.22 for osx10.16 on x86_64 (Homebrew)

And I can connect to it via the app sequel pro, create / manage databases.

sequel pro connects

However, when I setup a wordpress website or test the connection via PHP.. I can't for the life of me connect to it.

This is the db section of my wordpress config.php file:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'root' );

/** MySQL database password */
define( 'DB_PASSWORD', 'password' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

And this is the page I get when trying to proceed with a wordpress install:

enter image description here

I also tried via a simple PHP script as follows:

<?php

$c = new mysqli("localhost", "root", "password", "wordpress") or die('Could not connect the database : Username or password incorrect');
echo 'Database Connected successfully';

?>

and I get this error:

Warning: mysqli::__construct(): (HY000/2002): No such file or directory

If I change localhost to 127.0.0.1 I get this error:

Warning: Packets out of order. Expected 0 received 1. Packet size=68 in /users/asdasda/test.php on line 3
Warning: mysqli::__construct(): MySQL server has gone away in /users/asdasda/test.php on line 3
Warning: mysqli::__construct(): Error while reading greeting packet. PID=36547 in /users/asdasda/test.php on line 3
Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

Ok, so I figured it out.

I had to create a php.ini file in /etc/ with the following lines:

mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
pdo_mysql.default_socket=/tmp/mysql.sock
-3

As per this answer here.

You should avoid using localhost and use an IP instead. localhost uses the IP address 127.0.0.1

  • 1
    That’s a really bad approach as it forces the database communication to use TCP/IP instead of a local socket (which would be much more efficient). The correct approach is to configure PHP to point to the correct socket file. – Quentin Dec 01 '20 at 00:10