Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Så här fixar du error vid upprättande av en anslutning till en database i WordPress

Ser du notisen ”Error establishing a database connection” i WordPress?

Detta är ett fatal error som gör din site otillgänglig för användare. Det inträffar när WordPress inte kan upprätta en anslutning till databasen. Eftersom flera faktorer kan påverka denna anslutning kan error vara lite svårt att troubleshoota för Beginner.

I den här artikeln kommer vi att visa dig hur du enkelt fixar ”Error establishing a database connection” på din WordPress site, steg för steg.

Fix the Error Establishing a Database Connection in WordPress

Vad orsakar error vid upprättande av en anslutning till en database i WordPress?

En database är en programvara som gör det enkelt att lagra, organisera och hämta data från andra program.

Som ett system för hantering av innehåll använder WordPress en database för att lagra all your content och andra data på din website. Den ansluter sedan till databasen varje gång någon besöker din website.

WordPress behöver följande information för att ansluta till databasen:

  • Namnet på din database
  • Database användarnamn och password
  • Server för databaser

Den här informationen lagras i din WordPress-konfigurationsfil, som heter wp-config.php.

Om någon av dessa uppgifter är felaktiga kommer WordPress att misslyckas med att ansluta till din server för databasen och du kommer att se felet ”Error establishing a database connection”.

Database connection error in WordPress

Det är ett av de vanligaste WordPress error. Förutom felaktiga credentials kan detta error också dyka upp om databasens server är nere eller databasfilerna är korrupta.

Med detta sagt, låt oss ta en titt på hur man fixar ”Error establishing database connection” issue i WordPress med steg-för-steg troubleshoot.

1. Kontrollera din WordPress Database Credentials

Den vanligaste orsaken till error i anslutningen till databasen är felaktiga credentials. Om du senaste har flyttat din WordPress site till en new server, så är det troligen detta som är problemet.

Credentials för din WordPress-databas lagras i filen wp-config.php. Det är konfigurationsfilen för WordPress som innehåller viktiga settings, inklusive information om databasen.

Om du ej har redigerat filen wp-config.php tidigare kan du ta en titt på vår guide för nybörjare om hur du redigerar filen wp-config.php för mer instruktioner.

Du kommer att leta efter följande rader i filen wp-config.php:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

Här måste du se till att informationen om databasnamn, användarnamn, password och server är korrekt.

Du kan bekräfta denna information från instrumentpanelen för ditt WordPress dashboard. För denna tutorial kommer vi att använda Bluehost, men processen kommer att vara liknande för andra webbhotell paneler också.

När du har loggat in klickar du på fliken ”Website” i den vänstra kolumnen och växlar sedan till fliken ”Advanced”. Rulla sedan ner till sektionen ”Database” och klicka på knappen ”Manage” bredvid den.

Click the Manage button next to the Database section

Detta kommer att öppna sidan MySQL Databases i cPanel i ett new fönster.

Här kan du hitta ditt databasnamn och användarnamn i sektionen ”Current Database”. Tänk på att copy and paste dessa details till ett anteckningsblock så att du kan add to dem till wp-config-filen.

Find your database name and username in the Current Databases section

Därefter rullar du ner till sektionen ”Current Users”, där du kan clicka på länken ”Change Password” bredvid namnet på din database.

Du kommer då till en new vy där du kan ändra lösenordet för databasen så som du gillar det.

Change your database password

När du har bekräftat all din database details kan du ändra den informationen i din wp-config.php-fil om det behövs.

Efter det kan du försöka besöka din website igen för att se om error i anslutningen till databasen har försvunnit.

Om du fortfarande kan se error betyder det att något annat är fel.

2. Kontrollera informationen om servern för din Database

Om du är säker på att uppgifterna om databasens namn, användarnamn och password är korrekta, är det bra att kontrollera att du använder korrekt information om databasens server.

De flesta webbhotell för WordPress använder localhost som server för din database. Vissa hanterade WordPress-värdföretag använder dock separata servrar för att servera databaser. I så fall kommer din databas serverinformation inte att vara localhost.

Detta innebär att du måste kontakta ditt webbhotell för WordPress för att bekräfta dessa detaljer.

3. Reparera WordPress Database

Nu kanske du får ett annat error i wp-admin adminpanelen som gillar ”One or more database tables are unavailable” eller ”The database may need to be repaired”. I så fall måste du reparera din database.

Du kan göra detta genom att lägga till följande rad i din wp-config.php-fil. Se till att add to den precis före ”That’s all, stop editing! Trevlig blogg”:

define('WP_ALLOW_REPAIR', true);

När du har gjort det kan du se inställningarna genom att besöka den här page: http://www.yoursite.com/wp-admin/maint/repair.php. Se bara till att ersätta yoursite.com med ditt eget domain name.

Här måste du klicka på knappen ”Repair Database” för att starta processen.

Repair WordPress database

Note: Användaren behöver ej vara inloggad för att komma åt reparationssidan för databasen. När du är klar med att reparera och optimera din database, se till att ta bort den här kodraden från din wp-config.php.

Men om du inte vill add to någon kod till din site, kan du också reparera din database från ditt webbhotell kontos cPanel.

Gå vidare och besök ditt webbhotell konto, där du måste öppna sidan ”Websites” från den vänstra kolumnen. Gå sedan till tabben ”Advanced” och klicka på knappen ”Manage” i sectionen ”Database”.

Click the Manage button next to the Database section

Detta kommer att öppna cPanel i en new tab. Här rullar du ner till ”Modify Databases” section och väljer namnet på din database från ”Repair Database” dropdown menu.

När du har gjort det klickar du bara på knappen ”Reparera databas” för att starta processen.

Click the Repair database button

Webbhotellets panel kommer nu automatiskt att reparera din database åt you.

När processen är completed kommer du också att se ett success message.

Database repair success message

You can now visit your WordPress site to see whether the error has been resolved or ej.

4. Kontrollera om din server i databasen är nere

Om allt verkar vara korrekt och WordPress fortfarande inte kan anslutas till databasen, kan din databasserver (MySQL server) vara nere.

Detta kan hända på grund av tung trafik på en server. Din server kan helt enkelt inte hantera hämtar (särskilt när du har delade webbhotell).

På grund av detta kommer din webbplats att bli långsam och kan till och med mata ut error för vissa användare. I det här fallet bör du ta ett telefonsamtal eller chattsupport med din hosting provider och fråga dem om din MySQL server är responsiv.

Om du dessutom har andra webbplatser som körs på samma server kan du kontrollera dessa webbplatser för att bekräfta att din SQL-server är nere.

Om du ej har någon annan site på samma webbhotell account, så går du bara till din dashboard och byter till tabben ”Advanced”.

Därefter klickar du på knappen ”Hantera” bredvid phpMyAdmin section.

Click the Manage button next to the PHPMyAdmin option

Detta öppnar phpMyAdmin i ett nytt fönster, där du måste klicka på alternativet ”Database” högst upp.

Därefter klickar du på namnet på din database för att komma åt dess Settings. Om du kan göra det, är det dags för dig att kontrollera om din databas användare har tillräckliga behörigheter.

Connect PHPMyAdmin with your database

För att göra detta måste du skapa en new fil som heter testconnection.php och klistra in följande kod i den:

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

När du klistrar in koden, se till att ersätta användarnamn och password med dina egna. Du kan nu uploada filen till din website och komma åt den via en web browser.

Om skriptet ansluts utan problem betyder det att din användare har tillräcklig behörighet och att det är något annat som orsakar error.

Nu måste du gå tillbaka till din wp-config-fil och skanna den för att se till att alla detaljer är korrekta och utan några stavfel.

Ytterligare lösningar som har fungerat för användare

Om de ovan nämnda felsökningstipsen inte lyckas fixa felet i anslutningen till databasen på din webbplats kan du prova dessa ytterligare steg.

Som våra användare har rapporterat har dessa steg hjälpt några av dem att lösa error i anslutning till databasen på sina webbplatser.

1. Update URL:en för WordPress site

Du kan försöka updating URL:en för WordPress site med hjälp av phpMyAdmin i cPanel.

Gå bara in på phpMyAdmin från din dashboard på webbhotellet och välj din WordPress database från listan.

Följ detta genom att gå till SQL-menyn högst upp och enter följande MySQL sökning:

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Det ska se ut så här:

Change site URL

Glöm inte att ange din egen site URL och ändra wp_options till ditt eget tabellnamn, eftersom du kan ha ändrat WordPress tabellprefix.

2. Starta om web server

Användare på dedikerade servrar, språkversioner och virtuella privata servrar (VPS) kan försöka starta om sina servrar.

Detta kommer att starta om din web server och database server, vilket kan fixa några tillfälliga problem som orsakar error.

3. Be om hjälp

Om allt annat Misslyckas, då kan du behöva kontakta ditt webbhotell. Alla bra webbhotell för WordPress kommer att hjälpa dig att troubleshoota problemet, peka dig i rätt riktning eller till och med fixa det åt dig.

Du kan också hyra WordPress utvecklare med en plattform som WPBeginner Pro Services för att hjälpa dig fixa detta issue till rimliga priser. Vi har ett dedikerat team av expertutvecklare som du kan lita på för att lösa alla dina WordPress issues.

WPBeginner Pro Services

Dessutom kan du använda tjänsten för underhåll av din webbplats, reparation av hackad webbplats, hastighetsoptimering, ombyggnad av webbplats och sökmotorsoptimering.

Vi hoppas att den här artikeln hjälpte dig att fixa ”Error establishing a database connection” issue i WordPress. Du kanske också vill se vår guide till felsökning av WordPress för tips om hur du löser WordPress issues på egen hand, eller våra expertval av de bästa tools för WordPress frilansare och utvecklare.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

665 kommentarerLämna ett svar

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Mark says

    Cheers for the article and the great tips!
    In my case I simply forgot to create a mysql user. Discovered by logging into mysql and scrolling back through the command line history with the up arrow…
    ”Hmmm something’s missing here….”.
    Up and running in a jiffy!

  3. Hajjalah says

    I used to get this error more often when I was logged into my admin dashboard for a longer period but this article helped me solve the issue. I just updated the WordPress Site URL. Thanks for the useful guide.

  4. Dennis Muthomi says

    I actually encountered this database connection error just recently when working on a staged WordPress site before going live.
    I had just moved a WordPress site from local to a vps. The video tutorial at timestamp 2:53; helped me solve the error :-)

  5. Jiří Vaněk says

    May I ask what the problem is, if I migrate the database to a new one, edit wp-config.php for the new connection, but wordpress does not connect? In the end, I had to delete the old wp-config and create new ones from the installation package. The only thing I found out was that the key stored in wp-config is probably a problem. Am I encrypting somehow connected to the DB and does it have to be removed from the wp-config file during the migration?

    • WPBeginner Support says

      You would want to ensure you have a user on that new database with the correct information that you have in your wp-config for the most common cause for that issue.

      Administratör

    • WPBeginner Support says

      The steps in this guide should still be able to assist you and we would recommend reaching out to the support for the demo you are importing and they can look into what could be the root of the problem you are running into.

      Administratör

  6. Henrik R. says

    I need to change the password for the MySQL database for security reasons.
    Isn’t there a way to login to the WP dashboard and tell WP about the new password, instead of having to change the wp-config.php file?

  7. LN says

    First I would like to say thanks for this guide!
    I am having an issue “Error establishing a database connection“ I have checked the wp-config.php and all the database information is correct. I wanted to try step 4 in this guide but I don’t really understand how. I logged in to phpMyAdmin, but not sure what the next step is. Do you have a video for that? I am not a developer just a new WP beginner.

    • WPBeginner Support says

      You would want to go to your hosting provider’s dashboard or reach out to your hosting provider and they should be able to assist.

      Administratör

  8. Daniel says

    I almost have given up after having one issue after the other, till i read this. I had this feeling that ”this is it” this is the solution I have been looking for. tried it and it worked perfectly, thanks a lot.

  9. Hrutik vijay kumthekar says

    I’m getting this error when entered the URL of my site:

    There has been a critical error on your website.

  10. jennifer mulder says

    I am getting this error
    Warning: mysqli_real_connect(): (HY000/2002): Connection refused in /home/f5853789/public_html/wp-includes/wp-db.php on line 1635
    Connection refused

    what do I do

    • WPBeginner Support says

      That is a different error, you would want to reach out to your hosting provider for assistance with that specific error.

      Administratör

  11. Tejas Misra says

    I am a beginner to WordPress, and I am using MAMP to install WordPress but I’m still getting this error. My credentials are correct, and none of the steps shown above appeared to work. The database is repaired too. I don’t know what to do.

  12. sain says

    Hi, I have just moved my WordPress site to a new hosting provider via ftp when this error popped up. Is there anything I’m to update after a migration? Thanks

  13. KWS Adams says

    Adding this line ”””define(’WP_ALLOW_REPAIR’, true);”” helped me get my site back after 14 hours of being down. Thought it was my hosts problem. Thanks for the info.

  14. Zubair Rahman says

    How to increase the connection limit in the MySQL database? My website is intermittently giving database error as it is exceeding the max connection limit allocated on the server.

    • WPBeginner Support says

      You would need to reach out to your hosting provider for changing that and they would be able to assist.

      Administratör

  15. Nimra afridi says

    I have installed a wordpress . i also make post but sometime it shows me that ”please install wordpress”. Why it showing me that?
    Thank you.

    • WPBeginner Support says

      There are a few possible reasons, you may want to reach out to your hosting provider to ensure there are no hiccups on their end.

      Administratör

  16. alex says

    my website is hosted on aws and i accessed the MYSQL folder via filezila but i couldn’t find the file that contains database username , password and hostname. is there any specific name of that file?

  17. Deborah N. says

    Thanks. Adding the repair code to my wp config file worked. I appreciate the article. It was straight forward and easy to follow.

  18. bloggeryanke says

    your article was helpful i had changed my database password so it wasnt connecting to my blog properly but all thanks to your article the issue has been fixed

  19. Joe Marzi says

    Thank you for sharing such a nice blog.
    It will help the beginners of WordPress who are facing the problem regarding Error Establishing in the database connection. Apart from that, It gives a step by step solution so, every reader can easily solve the problem.

  20. Irwin says

    I got this error after forcefully shutting my computer. After trying all these suggested methods and failing, I rebooted my computer and the database error was fixed.
    You should give it a try too if things go south.

  21. Nick Aninsley says

    Thank you so much for sharing this tutorial. Actually, had installed WordPress, it shows me “Error Establishing A Database Connection” and I don’t know what to do. Then I have searched on Google and I found your this article and really it is
    very helpful for me and also many people who have no idea about this things. and my site were facing many other problems and only one of them has been solved. Thank for article.

  22. Emily says

    I can’t back up my site or do any of what you suggest, because I cannot get into the administrative section of my blog. I can view it, as a viewer, but I have no access to anything more. I also cannot find anyway to contact people from wordpress! Help!?!!

  23. Waqas Gulzar says

    Guys,

    I have fixed it now for my website

    It was due to insufficient PHP memory
    I have updated my php.ini to increase the PHP memory

  24. ken says

    I’m on BlueHost and just upgraded to WP 5.2 and got the dreaded error message:

    ”Your PHP installation appears to be missing the MySQL extension which is required by WordPress.”

    I tried with PHP 5.6, 7.0 & 7.1 – all give the same response. Any ideas what I can try? My site is completely blocked by the error.

  25. ravendra says

    hello I need your help please help me my website is showing Error establishing a database connection
    how can I solve I don’t know please help me

    • WPBeginner Support says

      You would want to follow the troubleshooting steps in this article for that error.

      Administratör

  26. Daniel says

    I had this very same issue while using Xampp. After using the ”testconection.php” file and through some further trouble shooting, I discovered that my user account was set to connect to ”Any host” and by changing it to ”Localhost” the problem was solved. I suspect this error came about when I updated Xampp and recreated the same named user.

    • Della says

      For my case the correct response was by Daniel.
      …I discovered that my user account was set to connect to “Any host” and by changing it to “Localhost” the problem was solved.

      Thanks so much!

    • WPBeginner Support says

      The methods in this article should be able to help with localhost sites as well as hosted sites.

      Administratör

  27. Cosme Fulanito says

    After succeed the ”Check your Web Host (MySQL Server)” test I change the host from 127.0.01 to localhost on the config.php file.

    That worked for me.

  28. Vinay Mourya says

    hi, it is happening to my WordPress site only sometimes and when I reload the page site starts working again please suggest me a solution, for now, I am using Cloudflare as cdn and wp rocket for caching plugin, the theme is newspaper premium.

    • WPBeginner Support says

      If it is intermittent like that, you would want to reach out to your hosting provider and let them know and they should be able to assist.

      Administratör

  29. Gee says

    Same exact error i got “Error establishing a database connection” but not all of the sollution you mentíon helped me.

    For me the MySql services was disabled: I went to services and start the MySQL to automatic. The other thing was the ” log on ” to user was administrator and changed it to the account i created.

    Now my wordpress site is working again.

  30. Umar says

    I have a weird situation is, that the error establishing a database connection is only happening to one post in my blog while other posts are not showing this issue.

  31. rabda says

    I am receiving this error one time on 3 and not always! I do not understand why and how to solve the problem

    • WPBeginner Support says

      You may want to try reaching out to your hosting provider to see if their error logs have information that can help find the root of the issue.

      Administratör

  32. Ryan says

    thanks so much, worked great, really appreciate how fast and to the point the video was, instead of a 7-10min extended one, boom, stright to the point!

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.