Solved WordPress does not work on FreeBSD (Error establishing a database connection)

Hi, I've installed PHP, Apache, and MariaDB on FreeBSD, and everything seems to be working fine—except that I can’t get WordPress to connect to the database. Here are the steps I’ve taken so far:
Step 1: Install and Configure Apache and PHP

Code:
sudo pkg install apache24
sudo sysrc apache24_enable=yes
sudo service apache24 start
sudo pkg install php82 mod_php82 php82-mbstring php82-zlib php82-curl php82-gd php82-mysqli php82-pear-MDB2_Driver_mysqli

I added the following line to /usr/local/etc/apache24/httpd.conf:
ServerName localhost


Then, I added these lines to /usr/local/etc/apache24/Includes/php.conf:
<IfModule dir_module>
DirectoryIndex index.php index.html

<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>

<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
</IfModule>


I also created a PHP test file at /usr/local/www/apache24/data/index.php:
PHP:
<?php
    phpinfo();
?>

phpinfo.png


After restarting Apache with sudo service apache24 restart, both Apache and PHP worked fine.

Step 2: Install and Configure MariaDB

Code:
sudo pkg install mariadb105-server mariadb105-client
sudo sysrc mysql_enable=yes
sudo service mysql-server start
mysql_secure_installation

I can create and manage databases without any issues. I also installed phpMyAdmin, which works well for database management:

phpmyadmin.png


The database user is wpuser, with a database named wordpress and password myfreebsd. I also tried the root user, but WordPress still doesn’t connect.

Now I can see wordpress shows setup before rename wp-config-sample.php to wp-config.php:

wordpress.png


Step 3: Configure​

I edited wp-config.php (renamed from wp-config-sample.php) with the following database settings:
PHP:
define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wpuser' );
define( 'DB_PASSWORD', 'myfreebsd' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );

Issue​

When I try to access WordPress, I get the error: “Error establishing a database connection.”

error.png


I’ve tried a lot of troubleshooting steps but haven’t been able to solve it. Since this seems more related to FreeBSD than WordPress itself, I thought I’d ask here. Any help would be greatly appreciated!

P.S:
FreeBSD version: FreeBSD 13.3-RELEASE-p3 FreeBSD 13.3-RELEASE-p3 GENERIC amd64
PHP version: PHP 8.2.24
Wordpress version: 6.6.2


I've attached phpinfo full screen.

Any help would be greatly appreciated!
 
Check pdo_mysql.default_socket in php.ini and socket in my.cnf. Make sure they match.
 
Check pdo_mysql.default_socket in php.ini and socket in my.cnf. Make sure they match.
/usr/local/etc/mysql/my.cnf:
#
# This group is read both by the client and the server
# use it for options that affect everything, see
# https://mariadb.com/kb/en/configuring-mariadb-with-option-files/#option-groups
#
[client-server]
port = 3306
socket = /var/run/mysql/mysql.sock

#
# include *.cnf from the config directory
#
!includedir /usr/local/etc/mysql/conf.d/


/usr/local/etc/php.ini:
[Pdo_mysql]
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
pdo_mysql.default_socket=


I changed it to pdo_mysql.default_socket = /var/run/mysql/mysql.sock and restarted Apache, but it still doesn’t work.
 
Welcome!
Yes, sometimes localhost can resolve to ::1 and mysql accept only IPv4 connections.
Or can be other compatibily problems, so using 127.0.0.1 is safe.
 
Welcome!
Yes, sometimes localhost can resolve to ::1 and mysql accept only IPv4 connections.
Or can be other compatibily problems, so using 127.0.0.1 is safe.
Thank you so much... But I don't know this approach worked only once! why?! I have no idea why it doesn't work for other projects; it's really weird! I even copied that folder and changed the database name to something else, but I still got the same error!
 
Thank you so much... But I don't know this approach worked only once! why?! I have no idea why it doesn't work for other projects; it's really weird! I even copied that folder and changed the database name to something else, but I still got the same error!
I changed db_user to root and it works!
 
Thank you so much... But I don't know this approach worked only once! why?! I have no idea why it doesn't work for other projects; it's really weird! I even copied that folder and changed the database name to something else, but I still got the same error!
What do you mean it worked only once? I’ve gotten into the habit of always using 127.0.0.1 for database connection for the reasons stated above.
 
Great. Is that the user that owns the database? I usually create a user for each application using the database, and give the database ownership to that user.
Thank you! The last problem was granting permissions to the wpuser. Now everything works fine.
 
Back
Top