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

    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

    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!

    • WPBeginner Support

      Glad to hear you found and solved the issue :)

      Administratör

  3. Hajjalah

    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.

    • WPBeginner Support

      You’re welcome, glad our guide was helpful!

      Administratör

  4. Dennis Muthomi

    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 :-)

    • WPBeginner Support

      Glad our guide was able to help you :)

      Administratör

  5. Jiří Vaněk

    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

      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

  6. Raquel Lynn

    Thank you!!! Your YouTube video worked perfectly, I repaired my site : )

    • WPBeginner Support

      Great to hear! :)

      Administratör

  7. Anoop

    When i am importing demo Error establishing a database connection message is showing

    • WPBeginner Support

      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

  8. Eddy

    Great video, great tutorial, excellent presentation. thanks a lot.

    • WPBeginner Support

      You’re welcome, glad you liked our content!

      Administratör

  9. Henrik R.

    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?

    • WPBeginner Support

      There is not a simple alternative we would recommend at the moment.

      Administratör

  10. Nwabufor John

    I changed my WordPress database username and password would it affect my websites SEO?

    • WPBeginner Support

      No, that should not affect your site’s SEO

      Administratör

      • Nwabufor John

        Thank you, I was scared when it happened

  11. varun kamath

    Thank you so much

    • WPBeginner Support

      Glad our guide was helpful :)

      Administratör

  12. LN

    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

      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

  13. IK

    Thanks for this wonderful article. it has really helped me

    • WPBeginner Support

      Glad our guide was helpful :)

      Administratör

  14. Daniel

    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.

    • WPBeginner Support

      Glad our guide was helpful :)

      Administratör

  15. Paulo

    In my case, as I use digital ocean, I had to restart the DROPLET

    • WPBeginner Support

      Thanks for sharing what worked for you :)

      Administratör

  16. Hrutik vijay kumthekar

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

    There has been a critical error on your website.

  17. Arunya

    Thank you very much This Article really helpful

    • WPBeginner Support

      Glad you found our guide helpful :)

      Administratör

  18. Sal

    running out of space has done this to me a few times!

    • WPBeginner Support

      Thanks for sharing that should someone else run into this issue :)

      Administratör

  19. jennifer mulder

    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

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

      Administratör

  20. Tejas Misra

    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.

  21. sain

    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

  22. KWS Adams

    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.

    • WPBeginner Support

      You’re welcome, glad the recommendation was helpful :)

      Administratör

  23. Maxine

    Thanks! Step 1 worked for me.

    • WPBeginner Support

      You’re welcome, glad our guide was able to help :)

      Administratör

  24. Zubair Rahman

    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

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

      Administratör

  25. Agung Sudomo

    I use google cloud as hosting and encounter this problem. What should I do?

    • WPBeginner Support

      You should still be able to follow the steps in this article

      Administratör

  26. Anita

    Thank you so much for this! The solution for me was fixing url in sql. Thank you for the code!

    • WPBeginner Support

      You’re welcome, glad our guide could be helpful :)

      Administratör

  27. Nimra afridi

    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

      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

  28. alex

    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?

    • WPBeginner Support

      The file with that information would be your wp-config.php file

      Administratör

  29. Chuck Whelon

    Very helpful thank you.

    • WPBeginner Support

      You’re welcome :)

      Administratör

  30. Deborah N.

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

    • WPBeginner Support

      You’re welcome, glad our recommendation was helpful :)

      Administratör

  31. bloggeryanke

    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

    • WPBeginner Support

      Glad our recommendations could help :)

      Administratör

  32. Joe Marzi

    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.

    • WPBeginner Support

      You’re welcome, glad our articles can be helpful :)

      Administratör

  33. Deepak

    Thanks for such amazing information. I really liked your article.

    • WPBeginner Support

      You’re welcome, glad you liked our article :)

      Administratör

  34. Irwin

    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.

    • WPBeginner Support

      Thanks for sharing the solution that worked for you :)

      Administratör

  35. Erik

    Awesome! Very easy to follow and took minutes to solve.

    • WPBeginner Support

      Glad our guide could be helpful :)

      Administratör

  36. Bob

    Yet again you guys come to my rescue. Fixed in a couple of minutes. Well done.

    • WPBeginner Support

      Glad our guide could help :)

      Administratör

  37. Nick Aninsley

    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.

    • WPBeginner Support

      Glad our article could help :)

      Administratör

  38. Emily

    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!?!!

  39. Waqas Gulzar

    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

  40. ken

    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.

    • WPBeginner Support

      If you reach out to BlueHost they should be able to resolve that issue for you

      Administratör

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.