13

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 (localhost, password and username).

I've read many of the questions and answers here but nothing helped me.

I have shell access so I can check everything that you need me to do. Maybe I should look at the configuration file for phpmyadmin to see what db connection settings are there, because phpmyadmin works, but I don't know where it is.

Thanks

Idan Shechter
  • 9,779
  • 25
  • 105
  • 203
  • Can you tell us where the database is configured in your wp-config.php? If it runs on localhost, did you check that mysql is running locally? If it runs on a remote host, is it reachable from your ec2 instance? Typically if the adress is a 10.x.x.x or 192.168.x.x it will probably not work ( unless you have an infrastructure setup, but you can try to ping it anyway). If it is a dns name also check you can resolve it from your ec2 instance. – OlivierS Sep 06 '11 at 06:00
  • I am getting here since sym linking to a different location on the server. Before it happened occasionally and that was still a worry and restarting did the trick but it isnt great and suggests mal configuration for us ec2 users IMO. – landed Jun 20 '16 at 12:38

14 Answers14

13

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.

Karthik
  • 961
  • 8
  • 18
  • Does anyone have any more insight on this? This happened to me today, and the fix described worked exactly as mentioned. I had to restart httpd before I could restart the database. My logs show no errors, so I'm not feeling too good about how to prevent this in the future. – Grant H. Mar 22 '13 at 14:26
  • 3
    Im having the same exact issue. It keeps happening. – ThomasReggi Apr 04 '13 at 13:15
  • This worked perfectly for. Have no reason how it happened in the first place though as I don't touch this machine. – ecMode Jan 02 '14 at 21:00
  • This actually happens to me more often than not, and although the foregoing solution works, I was wondering if there is a permanent solution to this – Alby Jan 28 '15 at 23:55
  • Why does this happen tho having a nightmare with WordPress on a EC2 t2.micro database permanently keeps falling over tried swap file adding cron job to restart. – user1503606 Jun 17 '16 at 08:38
  • Well, I suggest that you should get a bigger server or better to keep the DB on a separate server. This is a common problem for WordPress sites on EC2 t2.micro instances. Otherwise, you can easily restart the instance from the AWS console which is faster than SSH to the server and type commands at the command line I think. – Atanas Beychev Feb 21 '20 at 11:37
7

Increase ur swap memory in ec2 that solves the problem. http://digitizor.com/2011/02/06/create-swap-file-ubuntu-linux/

tk120404
  • 2,496
  • 1
  • 27
  • 28
  • May I know that how much swap memory to be created. What are the pros and cons for creating swap memory. – Richie May 14 '18 at 08:51
4

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

Charles Caldwell
  • 15,009
  • 4
  • 37
  • 47
4

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?

Community
  • 1
  • 1
weiheng
  • 346
  • 4
  • 16
2

You did not indicate whether you are moving your blog from myolddomain.com to mynewdomain.com, and whether you are using normal WordPress or multi-site WordPress. But here are some tips that might help:

  • First, you need to make sure your wp-config.php is configured correctly (database server, username, password...)

  • Secondly, WordPress uses database to store some information about the current host, look at tables wp_site, wp_options,wp_sitemeta and replace all rows containing myolddomain.com by mynewdomain.com.

  • Make sure your EC2 security group is configured correctly.

This article might help, too.

Lucas
  • 174
  • 3
  • 11
1

I had same error. Every time my EC2 server rebooted due to lack of memory. Mysql did not restart causing my websites to crash by showing "Error establishing a database connection" At first, I restart my server from WHM, that fixed the error immediately. The error return next time the server rebooted again. I also tried to ssh auto restart mysql database on boot commands, but that was not problem. All though my security group was setup to accept all traffic, I had to change my security group to include mysql, and reboot my instances to fix the error. I not sure if this will work for everyone with EC2. That worked for me

ethan6
  • 51
  • 2
1

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
1

Make sure you have installed mqsql client on your EC2 to establish connection to RDS MySQL(or any RDS DB). And install the DB dependencies, you would require to access RDS from Apache. Follow the link : https://aws.amazon.com/getting-started/hands-on/deploy-wordpress-with-amazon-rds/5/

0

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.

kkuldeep
  • 19
  • 2
0

If the server keep crashing after restart maybe you should increase your memory, upgraded the server from t2.micro to t2.medium do the trick for me.

H.IHYA
  • 17
  • 1
  • 1
  • 7
0

you might consider closing your ports.

In AWS you can do this in the Security Group. For example if you have SSH, HTTP and HTTPS ports open (22, 80 etc..) try closing them to only your IP address. (by default they are 0.0.0.0, open to everyone). You should especially do this for SSH port.

bots are always trying to gain access to open ports... hitting you thousands of times a day if not more to test if you have open vulnerabilities. Depending on the size of your instance, you may not be able to handle this traffic. this could be the source of your server constantly crashing.

petor
  • 1
0

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

Port range 0-65535

0

The below fixed it for me:

amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2 &&
service httpd restart
Suraj Rao
  • 28,186
  • 10
  • 88
  • 94
0

I'm not sure which distribution your using so here's how I generally solve these types of issues.
1. install a program called nmap (it scans ports) 2. run 'nmap localhost' (replace localhost with whichever server is running your database if its not local) 3. check to see if your database(mysql) is running

If not, then do something to the effect of 'services mysqld restart' (this will restart the mysql daemon). If this fixes it, then look into have that service autostart. If you get an error with that command than you may not have mysql installed.

Lostsoul
  • 21,503
  • 39
  • 122
  • 205