17 Sept 2015

Error Installing mysql-server-5.6 on Ubuntu 14.04 [FIX]

I was trying to install MySQL on one of my VMs late last night and the installation kept failing near the end (and at start up). Eventually, I realized that this system only has 256mb RAM (I have no shame) and that was resulting in the following failure:

start: Job failed to start  
invoke-rc.d: initscript mysql, action "start" failed.  
dpkg: error processing package mysql-server-5.6 (--configure):  
 subprocess installed post-installation script returned error exit status 1  
Setting up libhtml-template-perl (2.95-1) ...  
Setting up mysql-common-5.6 (5.6.16-1~exp1) ...  
Processing triggers for libc-bin (2.19-0ubuntu6) ...  
Processing triggers for ureadahead (0.100.0-16) ...  
Errors were encountered while processing:  
 mysql-server-5.6  
E: Sub-process /usr/bin/dpkg returned an error code (1)
There are a few solutions to working around the failure, this one is the fastest and easiest. No one really wants to hack around the config when they're rushing to get something installed.

I found a simple solution at AskUbuntu and the clever trick was:

On Ubuntu 14.04, I do the following to solve the problem:
Create a 4G swap file:
 sudo fallocate -l 4G /swapfile
Change its permission to only root could access and change:
 sudo chmod 600 /swapfile
Make it swap:
 sudo mkswap /swapfile
Activate:
 sudo swapon /swapfile

My VM failed at the step in red. The error:
 
swapon failed: Operation not permitted

But I'm root. What gives?

OpenVZ doesn't allow you to add swap. After all, you are paying by the amount of RAM made available to you. 

Then I came across this post at linux problem solver to create 'fake swap'.  This little script, will solve your MySQL installation failure. Just put the following into a file and name it fakeswap.sh:

 
#!/bin/bash 

SWAP="${1:-512}"

NEW="$[SWAP*1024]"; TEMP="${NEW//?/ }"; OLD="${TEMP:1}0"

umount /proc/meminfo 2> /dev/null

sed "/^Swap\(Total\|Free\):/s,$OLD,$NEW," /proc/meminfo > /etc/fake_meminfo

mount --bind /etc/fake_meminfo /proc/meminfo

free -m
Then:
 chmod +x fakeswap.sh
Though you don't actually get the swap, MySQL will pass its installation stage. If you're running this on Ubuntu which has bash as the default shell, you'll need to make sure that at step [root@server] sh fakeswap.sh (at the original link) you actually run:
 ./fakeswap.sh