skip to Main Content

I am learning Laravel which I am using as the backend, with a wordpress database. I am at the step of building a Laravel API to communicate with the WP database.
So, I created a WP project and took note of the database settings in cPanel, which I then used to update the Laravel .env file. I ran “php artisan serve”, and the laravel page was there. All fine and dandy. So I proceeded to add wordpress files inside the laravel project and configured wordpress inside laravel public/index.php file using define(‘WP_USE_THEMES’, false); then required the wp-blog-header.php file…I got an error saying the wp-config file was missing. So, I downloaded the wp-config.php file from my WP cPanel, and added it to Laravel wordpress folder, leaving the wp-config-example.php file as is. This brought me to the database error: “Error connecting a database connection”. The strange thing though is that my WordPress site that has the database where I am trying to connect Laravel loads fine, no errors, error only appears on Laravel’s localhot:8000. I am not sure if this is a WP or Laravel issue but I have tried debugging from either approach.

Setup: WordPress 5.1.1, PHP7.2.1 Laravel5+

I have tried a few solutions on here and on google to no avail.Eg problems with database connection in laravel 5

https://www.rosehosting.com/blog/error-establishing-a-database-connection/

https://www.wpbeginner.com/wp-tutorials/how-to-fix-the-error-establishing-a-database-connection-in-wordpress/

I think it is important to mention that the initial .env file edit(ie using cPanel database credentials) to connect to the WP database worked fine. But I noticed that the wp-config file that I had downloaded from cPanel, had different DB settings from those in cPanel and hence the .env, execept for the database name. So I edited the wp-config to look like .env, and vice-versa, still no dice. Any solutions?

Here is the .env file using cPanel credentials.

DB_CONNECTION=mysql
DB_HOST=sql201.epizy.com
DB_PORT=3306
DB_DATABASE=epiz_23436343_w767
DB_USERNAME=epiz_23436343
DB_PASSWORD=KJYN75fiOnM5S

Here is the wp-config. (I have left both cPanel and the original credentials there for comparison, but I was commenting out as neeeded)

/** The name of the database for WordPress */
define( 'DB_NAME', 'epiz_23436343_w767' );

/** MySQL database username */
define( 'DB_USER', '23436343_3' );
define( 'DB_USER', 'epiz_23436343' );
/** MySQL database password */
define( 'DB_PASSWORD', '(po5n5S(85' );
define( 'DB_PASSWORD', 'KJYN75fiOnM5S' );

/** MySQL hostname */
define( 'DB_HOST', 'sql201.byetcluster.com' );
define( 'DB_HOST', 'sql201.epizy.com' );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );

I am officially stuck and any help would be much appreciated.

2

Answers


  1. larwell require different config format, eg for version 4.2:

    'mysql' => array(
        'read' => array(
            'host' => '192.168.1.1',
        ),
        'write' => array(
            'host' => '196.168.1.2'
        ),
        'driver'    => 'mysql',
        'database'  => 'database',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
    

    If you want you can iclude in larewel config wp config, and use wp constraints

    Login or Signup to reply.
  2. Is your Laravel project hosted on the same server as your WordPress environment? If not, this could just be due to the fact that cPanel is blocking external access to your database.

    Make sure that the IP address for your host running Laravel is allowed as a remote MySQL connection.

    This can be managed by clicking on Remote MySQL under databases in cPanel.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search