Error establishing a database connection

For some reasons my blog has been getting this error every now and then. I knew it had something to do with my recent setup of a new server, even though I copied over all my old settings. After digging around in my ubuntu 14, I found that it was a lack-of-memory issue.

My ubuntu 14 box is running the standard lamp stack: apache, mysql and php5, btw.

So first you wanna see if you can simply restart your apache:

sudo service apache restart

If your website comes back alive, then lucky you. You are done. cough cough…

If it complains about mysql, then try to restart your mysql:

sudo service mysql restart

If it lets you, then ah… luck you. What that means is, the problem “goes away”.

If my case, I had to dig deeper. The restart failed, so I opened up the mysql log file.

sudo tail /var/log/mysql/error.log

You may see something like this:

141229 7:03:52 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
141229 7:03:52 [Note] Plugin ‘FEDERATED’ is disabled.
141229 7:03:52 InnoDB: The InnoDB memory heap is disabled
141229 7:03:52 InnoDB: Mutexes and rw_locks use GCC atomic builtins
141229 7:03:52 InnoDB: Compressed tables use zlib 1.2.8
141229 7:03:52 InnoDB: Using Linux native AIO
141229 7:03:52 InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
141229 7:03:52 InnoDB: Completed initialization of buffer pool
141229 7:03:52 InnoDB: Fatal error: cannot allocate memory for the buffer pool
141229 7:03:52 [ERROR] Plugin ‘InnoDB’ init function returned error.
141229 7:03:52 [ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed.
141229 7:03:52 [ERROR] Unknown/unsupported storage engine: InnoDB
141229 7:03:52 [ERROR] Aborting

141229 7:03:52 [Note] /usr/sbin/mysqld: Shutdown complete

See that error line? InnoDB: mmap(137363456 bytes) failed; errno 12. Um… it turns out while trying to allocate memory for InnoDB, the process failed. So that means, my box doesn’t have that much RAM to do so. Yea… I signed up for a small plan.

birdchan@birdhome-ubuntu14:/etc/mysql$ free -h
             total       used       free     shared    buffers     cached
Mem:          484M       452M        31M        55M       6.0M       109M
-/+ buffers/cache:       337M       147M
Swap:           0B         0B         0B

You see how I only got 31M free RAM? Upping the RAM surely is an option, but I want to stay cheap, haha. So… let’s try the swap file? You may want to double check and see you’ve got the space needed in your file system. I only allowed myself to use 1Gb space for the swap file.

df -h

You will need root access for the following steps. Do a sudo su or add sudo before all the following commands.

The following command creates a swap file swap.dat at the root directory path. Each block size is 1024 bytes, which is 1Kb. I am creating 1 million blocks, so that makes 1Gb. Feel free to play with the count parameter to create the right size for your swap file.

dd if=/dev/zero of=/swap.dat bs=1024 count=1M

Double check you have your swap file with ls -lh /swap.dat. Then we will need to enable the swap file.

mkswap /swap.dat
swapon /swap.dat

Lastly, edit /etc/fstab and add the following line to the file.

/swap.dat      none    swap    sw      0       0 

Now when you run free -h, you should see your swap file being recognized by the OS.

root@birdhome-ubuntu14:/# free -h
             total       used       free     shared    buffers     cached
Mem:          484M       478M       6.2M        56M       1.0M        86M
-/+ buffers/cache:       390M        93M
Swap:         1.0G         0B       1.0G

One more thing. Open up your /etc/mysql/my.cnf and reduce the default InnoDB buffer pool size from 128Mb to 64Mb. Why 64Mb? Um… well, it could be anything you want. =)

So look for [mysqld], then add the following line.

innodb_buffer_pool_size=64M

Then restart mysql, you should be good to go.

root@birdhome-ubuntu14:/# sudo service mysql restart
stop: Unknown instance: 
mysql start/running, process 14156

Remember to exit out from your root shell!!! =)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s