Confusing PDO-only problem : Can't connect through socket/Access denied/Can't connect to server (shared host)

前端 未结 14 1886
失恋的感觉
失恋的感觉 2020-12-08 12:07

So the problem changed from what it was, i\'ll leave the original question below to prevent bad reviews on answers like I had after someone editing his question I answered :

相关标签:
14条回答
  • 2020-12-08 12:27

    What worked for me was specifying the port number like so:

    mysql:hostname;port=3306;dbname=dbname;

    This got it to work when connecting to a local database. Now I'm working on getting it to work with a remote db.

    0 讨论(0)
  • 2020-12-08 12:28

    I just solved a similar issue. My guess is you probably replaced your mysql_connect() statement with the PDO equivalent. Don't forget you still have lots of other code dependent on that old connection statement. Try keeping the mysql_connect in place while writing in the PDO code.

    0 讨论(0)
  • 2020-12-08 12:29

    try:

    exec('`which mysql_config` --socket');
    

    this should show you the configured socket.

    0 讨论(0)
  • 2020-12-08 12:30

    This was already marked as answered, but not really solved (without changing databases). So, just in case someone like me also experiences this problem...

    The easiest way to fix this is to first get the socket path (either by looking in the php.ini file or by using: phpmyadmin or the console (or construct it in mysql or mysqli)

    ...to run the following query (anything but PDO):

    show variables like 'socket';       //as mentioned by symcbean
    

    THEN, in the PDO connection string, change it to use the socket instead of a hostname:

    $dbc = new PDO("mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=$DBName", $User, $Password, array(PDO::ATTR_PERSISTENT => true)); // using persistent connections

    This worked for me.

    0 讨论(0)
  • 2020-12-08 12:30

    FWIW, I had this issue and changed my host from 'localhost' to '127.0.0.1'.

    I have no clue why localhost wasn't working, but that did the trick.

    Odd thing is, we have tons of servers and it works on almost every one using 'localhost'

    0 讨论(0)
  • 2020-12-08 12:30

    My problem may be different to the OP, but I thought it was worth posting. I did a software upgrade on a VM, then rebooted and got the OP's error message. It turned out to be an out-of-memory problem preventing mysql from starting. Deleting a few large files made the problem go away.

    0 讨论(0)
提交回复
热议问题