User has exceeded the ‘max_user_connections’ resource error in Heroku

Today morning, after changing my codes I pushed the changes to production only to find out that my app is crashing.

When I checked the logs all I found is that:

Mysql2::Error (User ‘xxxxxxxx’ has exceeded the ‘max_user_connections’ resource (current value: 15)).

I am using ClearDB addon to connect Mysql in Heroku.

Also I am using unicorn as a web server for my application, which is running multiple dynos.

I ran up to find the solutions of this issue and I came across the article which gave me relief.

Here is the link to it:

I have made changes to get rid of Bad connections error which was happening because some of the queries are taking so much times that they are eventually timing out. They are eating up the resources of DB.

Steps to fix-

– Modify the queries

– Add indices to tables and double-check to confirm

– Change unicorn.rb

Hope this will help you too.

Contact me for further assistance.


Paperclip cropping error using jcrop and s3

This one is a very specific and somewhat common problem while cropping images via paperclip.

Problem description:

I took inspiration from a railscasts episode:

I am using s3 you can follow this link to setup:

However, I have to make some changes as that railscasts episode is based on old paperclip versions. Here are some code snippets and changes to be made in that code:

Inside production.rb:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
  :bucket => ENV['AWS_BUCKET'],
  :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
  :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']

I have a model as follows:

class UserImage < ActiveRecord::Base
  attr_accessible :user_id, :user_image, :crop_x, :crop_y, :crop_w, :crop_h
  attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

  has_attached_file :user_image,
   :styles => { :original => '360x360!', :large => "90x90>"},
   :path => "/user_images/profile_images/:id/:style/:basename.:extension",
   :url => "/user_images/profile_images/:id/:style/:basename.:extension",
   :processors => [:cropper]

  belongs_to :user

  validates_attachment_content_type :user_image,
    :content_type => ["image/jpg","image/png","image/jpeg","image/bmp","i  mage/pjpeg"]

  after_update :reprocess_user_image, :if => :cropping?

  def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?

  def photo_geometry(style = :original)
    @geometry ||= {}
    path = (user_image.options[:storage]==:s3) ? user_image.url(style) : user_image.path(style)
    @geometry[style] ||= Paperclip::Geometry.from_file(path)

  def reprocess_user_image

The customized lib/paperclip_processors/cropper.rb

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')

    def crop_command
      target = @attachment.instance
      if target.cropping?
        ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]

When user submits the form with the crop area defined (crop_x, crop_y, crop_h, crop_w), the form submits to a controller action.

Inside controller action:


This code is intended to upload and image and crop the image according to the crop area selected by user.

But this code is running into an infinite loop where the convert command is executed infinite times in round-robin fashion for all custom sizes.


After a long time, luckily I was able to observe that there some problem with the gem version I was using. If you had came across a similar issue, here is the snapshot of my Gemfile:

gem 'paperclip', '3.1.4'
gem "mini_magick", "~> 3.4"
gem 'cocaine', '0.3.2'
gem 'rmagick'

After making changes, run bundle command to install/modify gems.
Cocaine and paperclip were making the issues because of conflicting versions.

Hope this will help resolve your issue too.

Import MySQL Tables To Heroku

There are some situations where you need to migrate the table data in the MySQL table on your local or staging server to the Heroku database.

This is very simple to do. Here I show you how.

I’m using the ClearDB MySQL Database add-on of Heroku which you can configure using this link

Now you have ClearDB setup. You can re-check your settings using:

heroku config

This will output something like:


Next step is to take a dump of your MySQL database locally:

mysqldump -u user -h hostname -ppassword database_name table_name > file_name.sql

So we have the dump and we have the Heroku config. Finally, we need to import the data to Heroku.
Use the following command to achieve this:

mysql --user=user --password=password --reconnect heroku_xxxxxx < file_name.sql

Now you can check the database to make sure everything is migrated.
That’s it.