Support » Localhost Installs » Apache2 says “Error establishing a database connection”

  • Resolved shiryou


    So I installed WordPress (newest Version) on my local Apache2 server.
    Installing went completely fine with a Localhost DB.
    However, already errors began when I tried establishing a connection on the setup with my DB. After some while, it suddenly worked.
    After that, a few minutes later, I get (yet again) the same error over and over again, and I don’t know why!

    Are you sure you have the correct username and password? Yes, I did. Everything fine when I try connect via terminal.
    Are you sure you have typed the correct hostname? localhost, yes.
    Are you sure the database server is running? Yes? If not, my apache2 server would be down, wouldn’t it?
    If you’re unsure what these terms mean you should probably contact your host. I am hosting it myself and I can confirm: My webserver is up & running completely fine!
    If you still need help you can always visit the WordPress Support Forums. Well, here I am now, asking for help since I don’t know what else to do.

    I already read the Docs ( and didn’t got any clue, what the problem could be. I also read other threads, but none of them had the exact same problem I had, since I followed the steps given as solution, but nothing changed.

    Also, I tried connecting to a non-local DB, same error: “Error establishing a database connection”.

    Also, no problems with the firewall, everything has allowance and ports are opened. However, I encouter the “Error establishing a database connection” with everything that requires one, MyBB, XenForo, everything and I don’t know what’s happening!

    I would be grateful, if anyone could help me out, since my brain is kinda going insane after hours of staring at posts, reading troubleshooting and googling my way through StackOverflow and other various sites…

    Thanks to anyone answering,


    P.S: I am running on latest Apache2, Lubuntu 18.04, Latest PHP thingie, Latest WP package, Latest MariaDB. If you need more information, don’t hesitate to ask, I will try to answer anything just to get this whole DB thing working. ~ Thanks once again.

    • This topic was modified 2 months, 1 week ago by shiryou. Reason: wrong help page url

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • sab


    Hi @shiryou First thing that you should do is set the following in your wp-config.php file

    define( 'WP_DEBUG', true );

    That should give you an idea of what specifically is the reason as to why the database connection in WordPress cannot be setup, and you can then proceed from there.

    The links below should hopefully help a little

    Debugging in WordPress

    WP-Config Editing

    Thread Starter shiryou


    Hey sainikbiswas, I did exactly what you told me.
    It already helped me.
    So, after setting define( 'WP_DEBUG', true );, I got the error message

    Warning: mysqli_real_connect(): (HY000/2002): Connection refused in /var/www/abulafia/blog/wp-includes/wp-db.php on line 1653
    Connection refused

    This is already a great help, thanks.
    Now I tried searching the WP Support forums for this, what I found is creating a “testconnection.php” file with this content:

    if(!($link = mysqli_connect('','wp_user','password','wordpress_db'))){
    die('could not connect: ' . mysqli_error($link));
    } else {
    die("don't have mysqli");
    echo 'connect successfully';

    (I know, this may not be the original text given, but a user said, that this would work out better since you not only will test the connection to MySQLi but to the DataBase directly)
    This didn’t work with a local DB, so I figured trying another DB from a friend which is completely working fine.
    Still, an error, but this time a different one: Network is unreachable
    After browsing the Support forums once again, I came to that conclusion:
    I just gave up, at least for now. After spending over four hours just doing research, I am at my limit. I tried many things, reinstalling MySQL, MariaDB, even PHP and reconfiguring php.ini! Still, the same results. Looks like I’m going to switch to paid hosting for that stuff or use a Flatfile Blogging CMS.

    I really appretiate your help, sainikbiswas, but after not being able to fix this, I’m gonna leave it just at how it is xd

    Sorry, and once again thank you for trying to help,


    Links for references: – testconnection.php

    My thoughts:
    – You mention having both mySQL and MariaDB, they could be tripping over each other, suggest only having one or other installed and not both.
    – Have you installed “phpmyadmin”, it is very useful. Does it run ?
    – Otherwise can you connect to the mySQL server using the command line ? Something like:
    mysql -u <dbUserHere> -p
    man mysql
    for options. Does it work ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.