Skip to content

php hangs when auth_socket is set for the user #19222

Open

Description

@jonashrem

Description

The following code:

<?php
$mysqli = new mysqli("localhost", "root", "...", "mysql");

echo $mysqli->host_info . "\n";

with a local MySQL 8.4 User like this:

*************************** 6. row ***************************
                    Host: localhost
                    User: root
...
                  plugin: auth_socket

Results in php hanging until MySQL timeouts instead of connecting to MySQL

Changing the auth plugin to caching_sha2_password solves the situation. I did not test what happens when I don't set the password or explicitly use the socket.

PHP Version

PHP 8.4.1 (cli) (built: Nov 25 2024 18:04:19) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.1, Copyright (c), by Zend Technologies

Operating System

Ubuntu 24.04

Activity

  1. ndossche commented on Jul 23, 2025

    @ndossche
    Member

    I created a user authenticated via auth_socket.
    Then I tried connecting with the unix domain socket (/var/run/mysqld/mysqld.sock), and it works fine.
    Then I tried connecting via localhost without the mysqlnd socket specified in the constructor, and it correctly throws a mysqli_sql_exception.
    So I can't reproduce the behaviour.
    Something is probably off in your setup.
    Also please note that PHP 8.4.1 is very outdated. Please retry on a more recent version.

  2. jonashrem commented on Jul 23, 2025

    @jonashrem
    Author

    ah sorry regarding the php version. It is the same in 8.4.10. I downgraded it to test if this can be reproduced in older versions.

    I didn't explictly specify the socket but a username and password.

    As my my.cnf contained the socket

    # CLIENT #
    [client]
    port                           = 3306
    socket                         = /var/run/mysqld/mysqld.sock
    
    

    'localhost' is automatically rewritten to using the socket through here but I'd still expected getting an error not a timeout.

  3. ndossche commented on Jul 23, 2025

    @ndossche
    Member

    Not specifying the socket also worked for me fine: I get an error instead of a timeout.
    Please check the mysqli.default-socket php.ini setting.
    Also please check if explicitly specifying the socket works, and double check your environment settings.
    If nothing stands out, you can try running php under strace (i.e. strace php script.php) to see all the system calls being executed and see where it hangs.

  4. jonashrem commented on Jul 24, 2025

    @jonashrem
    Author

    Here is the relevant part from strace. Looks for me like php is trying to authenticate with caching_sha2_password while already logged in through socket.

    Did you name your account the same as your local username? With a different user I get an error as well.

    connect(4, {sa_family=AF_UNIX, sun_path="/var/run/mysqld/mysqld.sock"}, 29) = 0
    fcntl(4, F_SETFL, O_RDWR)               = 0
    poll([{fd=4, events=POLLIN|POLLERR|POLLHUP}], 1, 86400000) = 1 ([{fd=4, revents=POLLIN}])
    recvfrom(4, "I\0\0\0\n8.4.5\0[\0\0\0\35\26-\10p\16Z\"\0\377\377\340\2\0\377\337\25\0\0\0\0\0\0\0\0\0\0YaV+:4[#a\35\25\27\0caching_sha2_password\0", 32768, MSG_DONTWAIT, NULL, NULL) = 77
    sendto(4, "\217\0\0\1\215\242\32\0\0\0\0\300\340\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0root\0 !.\324\350\347\226\"\4\252\322\20\246\r7\303\367^zTX\324\335:\315+\363\343>h\17)8mysql\0caching_sha2_password\0,\f_client_name\7mysqlnd\f_server_host\tlocalhost", 147, MSG_DONTWAIT, NULL, 0) = 147
    poll([{fd=4, events=POLLIN|POLLERR|POLLHUP}], 1, 86400000) = 1 ([{fd=4, revents=POLLIN}])
    recvfrom(4, "\7\0\0\2\0\0\0\2\0\0\0", 32768, MSG_DONTWAIT, NULL, NULL) = 11
    poll([{fd=4, events=POLLIN|POLLERR|POLLHUP}], 1, 86400000
    
  5. mariusklocke commented on Jul 25, 2025

    @mariusklocke

    I can reproduce this as well. It only happens when using a non-empty string as password in the mysqli Constructor. It works fine when using an empty string as password in mysqli Constructor.

    Since I don't like to install MySQL on my workstation, I use an LXC Container for reproduction. My steps:

    1. lxc launch ubuntu24 u24
    2. Add the MySQL vendor repository
    3. apt install mysql-client mysql-server (use empty password when prompted)
    4. add-apt-repository ppa:ondrej/php
    5. apt install php8.4-cli php8.4-mysql
    6. Run the script provided by @jonashrem

    Versions:

    • Ubuntu 24.04.02 LTS
    • MySQL 8.4.6
    • PHP 8.4.10
  6. ojuschugh1 commented on Oct 21, 2026

    @ojuschugh1

    Reproduced on 8.5.11 and master against MySQL 8.4.11, with an account IDENTIFIED WITH auth_socket AS 'root' and PHP running as root:

    // php -d mysqlnd.net_read_timeout=5
    new mysqli('localhost', 'gh19222', 'secret', '', 0, '/var/run/mysqld/mysqld.sock');
    // after 5 s: Unexpected server response while doing caching_sha2 auth: 0

    For that account the server sends the OK right after the handshake response, and with a password mysqlnd_caching_sha2_handle_server_response() reads it as the fast auth status. The default branch sets the error but still returns PASS, so the caller then waits for an OK that already came.

    I'd accept that OK the way libmysqlclient does and have the caller skip the read, which needs a new parameter on handle_server_response, so it would go to master. For PHP-8.4 the default branch could return FAIL instead, so it errors out right away. @kamil-tekiela @jonashrem does that sound right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions