Error establishing a database connection Amazon EC2 - after moving my wordpress blog

后端 未结 13 1951
無奈伤痛
無奈伤痛 2021-02-01 09:03

I am getting the error: \"error establishing a database connection\" after moving my wordpress blog to Amazon EC2. I\'ve checked the wp-config.php file and settings are ok (loca

相关标签:
13条回答
  • 2021-02-01 09:30

    Got the same problem recently , I use the AWS free tier account. The ec2 instance is t2.micro, which has no swap pre-configured. When i installed wordpress stuff, the memory usage is always higher, even i do nothing. One day, i run a sudo yum update command, the bash show that -bash: fork: Cannot allocate memory error. And my website always got error establishing a database connection too.

    I think the root cause is high memory usage. You can add swap memory following the instruction here: How do you add swap to an EC2 instance?

    0 讨论(0)
  • 2021-02-01 09:32

    I got the same error for my website, I just rebooted my instance and voila, problem solved.

    My case:

    I am using amazon free tier, and some bot requested my website and due to the high number of connections, the sql database could not serve the load. I have hence installed the load balancer too :) Hope it helps

    0 讨论(0)
  • 2021-02-01 09:33

    For me just restarting the mysqld service worked. There was a hitch though, as soon as I executed sudo service mysqld restart the MySql service stopped but didn't start. sudo services mysqld start gave a MySQL Deamon Failed to Start Error. I spent a lot of time trying to start the service but I guess the real culprit was the httpd service. I stopped the httpd service sudo service httpd stop. Now execute sudo service mysqld restart and bingo the wordpress site was back online(remember to start the httpd service). This sounds kind of lame but it worked, from what I figured out the MySQL DB stops for a number of reasons looking at the log and figuring out the actual reason is the only thing that works for me.

    0 讨论(0)
  • 2021-02-01 09:36

    I solved this problem adding a swap to my instance:

    sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
    sudo /sbin/mkswap /var/swap.1
    sudo chmod 600 /var/swap.1
    sudo /sbin/swapon /var/swap.1
    

    If you need more than 1024 then change that to something higher.

    To enable it by default after reboot, add this line to /etc/fstab:

    swap        /var/swap.1 swap    defaults        0   0
    

    To check if your instance is using swap run this command:

    cat /proc/meminfo
    

    The result:

    ubuntu@ip-172-31-24-245:/$ cat /proc/meminfo
    MemTotal:         604340 kB
    MemFree:            8524 kB
    Buffers:            3380 kB
    Cached:           398316 kB
    SwapCached:            0 kB
    Active:           165476 kB
    Inactive:         384556 kB
    Active(anon):     141344 kB
    Inactive(anon):     7248 kB
    Active(file):      24132 kB
    Inactive(file):   377308 kB
    Unevictable:           0 kB
    Mlocked:               0 kB
    

    SwapTotal: 1048572 kB

    SwapFree: 1048572 kB

    Dirty:                 0 kB
    Writeback:             0 kB
    AnonPages:        148368 kB
    Mapped:            14304 kB
    Shmem:               256 kB
    Slab:              26392 kB
    SReclaimable:      18648 kB
    SUnreclaim:         7744 kB
    KernelStack:         736 kB
    PageTables:         5060 kB
    NFS_Unstable:          0 kB
    Bounce:                0 kB
    WritebackTmp:          0 kB
    CommitLimit:     1350740 kB
    Committed_AS:     623908 kB
    VmallocTotal:   34359738367 kB
    VmallocUsed:        7420 kB
    VmallocChunk:   34359728748 kB
    HardwareCorrupted:     0 kB
    AnonHugePages:         0 kB
    HugePages_Total:       0
    HugePages_Free:        0
    HugePages_Rsvd:        0
    HugePages_Surp:        0
    Hugepagesize:       2048 kB
    DirectMap4k:      637952 kB
    DirectMap2M:           0 kB
    
    0 讨论(0)
  • 2021-02-01 09:39

    The error in DB connection could be due to the root passwords (if you are connecting from a browser). Rest your root passowrd by executing $mysql_secure_installation to Secure your database. In the first step press then you can specify a new password for root. Then all should work fine.

    0 讨论(0)
  • 2021-02-01 09:40

    Please click on the security group for your RDS instance.And click on the inbound >Edit add "All TCP" for 0.0.0.0/0

    0 讨论(0)
提交回复
热议问题