I have the following script to connect to the database:
$db = new PDO("mysql:dbname=dbname;host=127.0.0.1;charset=utf8mb4", 'root', '',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'"));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
I am using xampp and intermittently it will take 3+ seconds to load the above (When it isn't doing the above it loads instantly).
I have verified it is the above by using XDebug, please see screenshots below:

In google developer tools the TTFB (Time to First Byte) also matches the time it takes to connect to the database.
Things I have tried:
- Setting the ServerName property in http.conf to ServerName 127.0.0.1:80
- Using host 127.0.0.1 instead of localhost in the connect script
- In http.conf setting the HostnameLookups to Off
- In http.conf change the Listen port to 8080 (Listen 127.0.0.1:8080) this one just caused it to say localhost refused to connect though
- Disabling IPv6 in regedit
- Changed the host file to have the records 127.0.0.1 localhost and 127.0.0.1 127.0.0.1
- Disabling XDebug doesn't make a difference
- I have BitDefender Anti Virus installed and have tried disabling that
- Adding skip-name-resolve to my.ini
- Adding bind-address="127.0.0.1" to my.ini
- Flushing the DNS Cache
And probably other things I am forgetting.
I am using Google Chrome but have the same issues with all browsers.
How can I make the connection to the database consistently fast instead of hanging for 3+ seconds?
new PDO($dsn, $usr, $pass);? so many options? xD - treyBake