• Resolved trini0

    (@trini0)


    Hello all! First time poster.

    I am attempting to install the latest version of WordPress for a client and I’m running into an issue with connecting to the database. I’ve been trying to figure out what is wrong and I am out of ideas:
    The environment (latest versions as of 7/24):
    CentOS 7, PHP 8, nginx 1.24, MySQL 5.7
    This environment is in Google Cloud and both the web server VM and Cloud SQL do not have public IPs. External access to the website is via an external load balancer and the plumbing to connect everything internally have been completed.

    I downloaded/extracted the wordpress files into the web root directory and edited the wp-config.php with the relevant data to match my environment. A DB and DB user (w/root DB permissions) were created from the data used in the config file. Navigating to the installer at https://fake-domain.com/wp-admin/install.php and I get the following error:
    Error establishing a database connection

    I turned on the debug flag and it is reporting “Permission denied”.

    If I were to ssh into the VM and execute php commands to connect to the DB, I have no issues. If I hard coded values for mysqli_real_connct() in /wp-includes/class-wpdb.php (line 2019) I get the same error. I’m not sure if WP has some trickery to keep compatibility across various installation scenarios, but I cannot figure it out how to get the site up unless I pull all the code apart.

    Below is some evidence of environment make up and findings. Any help/assistance would be greatly appreciated.

    Thanks

    CentOS:
    bash-4.2$ cat /etc/os-release | grep 'PRETTY_NAME'
    PRETTY_NAME="CentOS Linux 7 (Core)"
    
    PHP:
    bash-4.2$ php -v
    PHP 8.2.8 (cli) (built: Jul  4 2023 14:52:54) (NTS gcc x86_64)
    Copyright (c) The PHP Group
    Zend Engine v4.2.8, Copyright (c) Zend Technologies
        with Zend OPcache v8.2.8, Copyright (c), by Zend Technologies
    
    PHP MYSQL extensions:
    bash-4.2$ php -m | grep mysql
    mysqli
    mysqlnd
    pdo_mysql
    
    PHP MySQL client:
    bash-4.2$ php -r 'print_r(mysqli_get_client_info()); echo "\n";'
    mysqlnd 8.2.8
    
    nginx version:
    bash-4.2$ nginx -v
    nginx version: nginx/1.24.0
    
    PHP/nginx running as nginx:
    bash-4.2$ ps aux | grep nginx | head -5
    root      9585  0.0  0.0  49072  1188 ?        Ss   00:59   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    nginx     9586  0.0  0.2  96668  4348 ?        S    00:59   0:00 nginx: worker process
    nginx     9587  0.0  0.2  96572  4140 ?        S    00:59   0:00 nginx: worker process
    nginx     9599  0.0  1.7 906088 32340 ?        S    00:59   0:00 php-fpm: pool www
    nginx     9600  0.0  0.7 510416 14436 ?        S    00:59   0:00 php-fpm: pool www
    
    bash-4.2$ ps aux | grep php-fpm | head -5
    root      9598  0.0  1.2 503816 23172 ?        Ss   00:59   0:01 php-fpm: master process (/etc/php-fpm.conf)
    nginx     9599  0.0  1.7 906088 32340 ?        S    00:59   0:00 php-fpm: pool www
    nginx     9600  0.0  0.7 510416 14436 ?        S    00:59   0:00 php-fpm: pool www
    nginx     9601  0.0  0.7 510412 14424 ?        S    00:59   0:00 php-fpm: pool www
    nginx     9602  0.0  1.5 906088 29624 ?        S    00:59   0:00 php-fpm: pool www
    
    Logged in user:
    bash-4.2$ whoami
    nginx
    
    Example web root and file permissions:
    bash-4.2$ pwd
    /var/www/wpdocs
    bash-4.2$ stat wp-config.php | grep 'Access' | head -1
    Access: (0644/-rw-r--r--)  Uid: (  997/   nginx)   Gid: (  995/   nginx)
    bash-4.2$ stat ../wpdocs/ | grep 'Access' | head -1
    Access: (0755/drwxr-xr-x)  Uid: (  997/   nginx)   Gid: (  995/   nginx)
    
    Connectivity to CloudSQL:
    bash-4.2$ nping --tcp-connect --dest-port 3306 w.x.y.z
    
    Starting Nping 0.6.40 ( https://nmap.org/nping ) at 2023-07-27 03:04 UTC
    SENT (0.0015s) Starting TCP Handshake > w.x.y.z:3306
    RECV (0.0026s) Handshake with w.x.y.z:3306 completed
    SENT (1.0040s) Starting TCP Handshake > w.x.y.z:3306
    RECV (1.0048s) Handshake with w.x.y.z:3306 completed
    SENT (2.0062s) Starting TCP Handshake > w.x.y.z:3306
    RECV (2.0070s) Handshake with w.x.y.z:3306 completed
    SENT (3.0082s) Starting TCP Handshake > w.x.y.z:3306
    RECV (3.0091s) Handshake with w.x.y.z:3306 completed
    SENT (4.0103s) Starting TCP Handshake > w.x.y.z:3306
    RECV (4.0111s) Handshake with w.x.y.z:3306 completed
     
    Max rtt: 1.087ms | Min rtt: 0.743ms | Avg rtt: 0.879ms
    TCP connection attempts: 5 | Successful connections: 5 | Failed: 0 (0.00%)
    Nping done: 1 IP address pinged in 4.01 seconds
    
    PHP script showing CloudSQL version and SQL grants:
    bash-4.2$ php -r '$dbcon=mysqli_init();  mysqli_real_connect($dbcon, "w.x.y.z", "temp-user", "fake-password", NULL, NULL, NULL, 0);  $result = $dbcon->query("SELECT VERSION()");  print_r($result->fetch_all());' 
    Array
    (
        [0] => Array
            (
                [0] => 5.7.42-google-log
            )
    )
    
    bash-4.2$ php -r '$dbcon=mysqli_init();  mysqli_real_connect($dbcon, "w.x.y.z", "temp-user", "fake-password", NULL, NULL, NULL, 0);  $result = $dbcon->query("SHOW GRANTS FOR CURRENT_USER");  print_r($result->fetch_all());'
    Array
    (
        [0] => Array
            (
                [0] => GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO 'temp-user'@'%' WITH GRANT OPTION
            )
    )
    
    Error logged when attempting to setup WP:
    bash-4.2$ tail /var/log/php-fpm/www-error.log 
    [26-Jul-2023 21:51:49 UTC] PHP Warning:  mysqli_real_connect(): (HY000/2002): Permission denied in /var/www/wpdocs/wp-includes/class-wpdb.php on line 2019
    [26-Jul-2023 22:22:06 UTC] PHP Warning:  mysqli_real_connect(): (HY000/2002): Permission denied in /var/www/wpdocs/wp-includes/class-wpdb.php on line 2019
    
    
    
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello, just to double-check, could you confirm please that wp-config.php database settings match your cloud SQL instance connections, i.e. does it show w.x.y.z, which I assume is a private IP? Please make a backup of wp-config.php before proceeding with any changes. After making sure that wp-config.php database settings are correct, restart NGINX/PHP on the web server VM. Good luck!

    Thread Starter trini0

    (@trini0)

    Hello Gerry,

    Thanks for following up!
    The wp config files, php, nginx were all correct. But I did figure out the source of the problem. SELinux was blocking httpd services (nginx included) from connecting to databases. Once I granted the right, all went swimmingly well.

    ### View policies that affect webservers
    getsebool -a | grep -E “^httpd_(unified|can_network_connect)?(_db)?\s”
    httpd_can_network_connect –> off
    httpd_can_network_connect_db –> off
    httpd_unified –> off

    ### Allow these specific policies (-P flag to persist reboots)
    sudo setsebool -P httpd_can_network_connect 1
    sudo setsebool -P httpd_can_network_connect_db 1
    sudo setsebool -P httpd_unified 1

    ### Validate the change is in effect
    getsebool -a | grep -E “^httpd_(unified|can_network_connect)?(_db)?\s”
    httpd_can_network_connect –> on
    httpd_can_network_connect_db –> on
    httpd_unified –> on

    There may be better ways of configuring the policy, but this change was enough for me to continue with wp setup.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘First time install issue: Error HY000/2002’ is closed to new replies.