Jump to content

PDO Connection to mysql


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello all, this is just one of those questions that i think that have an rather quick awnser. I m concerned about the PDO connection i made witch takes some time to stablish, i was using an self made class where dynamicly i create the dsn get the values etc etc .. when i stabilish the connection takes up to 1.5 seconds to finish (where without the line that makes the connection takes 0.01s for the script to finish), so i tested out with just these 4 lines of code and i get the same result as my class.

 

 

    $dsn = 'mysql:host=localhost;port=3306;dbname=MyDatabase;charset=UTF8';
    $username = 'root';
    $passwd = '';
    $sqlhandler = new PDO($dsn, $username, $passwd);

 

The code i m using to mesure the the finish time (i found this in the php website):

 

    function convert($size){
        $unit=array('b','kb','mb','gb','tb','pb');
        return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
    }
 
echo '<b>Memory used</b>: '. convert(memory_get_usage(true)).'<br>';

 

My "development" Enviroment is Windows 8.1 with EasyPHP 13.1 Dev Server (with PHP 5.5). My question is it's normal that just the connection takes this much to get completed? Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/284838-pdo-connection-to-mysql/
Share on other sites

  • Solution

Does it improve if you use 127.0.0.1 instead of (localhost

$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=MyDatabase;charset=UTF8';

 

The code i m using to mesure the the finish time (i found this in the php website):

That code has got nothing to do with how long the script took to execute. It returns the memory usage. You should not be using this to determine how long your script took to execute.

 

EDIT: To time script execution you'll want to use something like the examples here

Edited by Ch0cu3r

Does it improve if you use 127.0.0.1 instead of (localhost

$dsn = 'mysql:host=127.0.0.1;port=3306;dbname=MyDatabase;charset=UTF8';

That code has got nothing to do with how long the script took to execute. It returns the memory usage. You should not be using this to determine how long your script took to execute.

 

EDIT: To time script execution you'll want to use something like the examples here

 

hi, i dont know what to say but wierdly enough ... it does help, the connection is nearly instant using 127.0.0.1. In terms of networking i know localhost is the same as 127.0.0.1, do you mind explaining why with localhost takes 'way longer'? The reason why i m asking is mainly for self study ... and tyvm for your reply, i will mark as solved if anyone can explain me ...

It is a domain resolution problem.  Computers need to convert a domain name into an address in order to now where the device is located. The domain resolution is done via DNS this is what could be causing the delay.

Edited by Ch0cu3r

IPv6 enabled Windows, such as Windows 8 resolve localhost to an IPV6 address ::1.  If your application is not IPV6 then the connection must timeout and then the IPV4 address 127.0.0.1 is resolved and used.

He should bind this IPv6 to the mysql server conf file as well to be able to use it.

 

Here is a part of mine db server: ( mysql 5.5/ CentOS 6.4)

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
bind-address = ::


// connection
[lxc@lxc1 ~]$ mysql -h ::1 -u lxc -ppassword --port=3306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.35 MySQL Community Server (GPL) by Remi

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.