How to address “Error establishing a database connection” in WordPress

“Error establishing a database connection” is one the  most common error for custom WordPress installation. It could be due to one of the following reasons

  1. MySQL username and password is incorrect in wp-config.php file.
  2. MySQL database is down
  3. MySQL database is corrupted ( Don’t get scared by this reason. This is least likely reason)

MySQL username and password is incorrect in wp-config.php file.

Most likely you will get this error during installation of wordpress on your server or local machine if you have not updated database username and password in wp-config.php.

Please check following section in wp-config.php file.

/** The name of the database for WordPress */
define('DB_NAME', 'DATABASENAME');

/** MySQL database username */
define('DB_USER', 'DATABASENAME_USERNAME');

/** MySQL database password */
define('DB_PASSWORD', 'DATABASENAME_PASSWORD');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

 

To avoid getting this error, please follow WordPress installation guide.

MySQL database is down

There could be multiple reasons for database to be done. Simply restart MySQL database using following command

sudo service mysql start

Most likely this will solve your issue. If it does not solve, it will throw following error.

$sudo service mysql start
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

Here is the output of both the commands

$sudo systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: inactive (dead) (Result: exit-code) since Sun 2018-04-29 02:10:42 UTC; 53s ago
Process: 10667 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid (code=exited, status=1/FAILURE)
Process: 10626 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 3812 (code=killed, signal=KILL)

Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: Failed to start MySQL Community Server.
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Unit entered failed state.
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Failed with result 'exit-code'.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: mysql.service: Service hold-off time over, scheduling restart.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: Stopped MySQL Community Server.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: mysql.service: Start request repeated too quickly.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: Failed to start MySQL Community Server
$ sudo journalctl -xe
-- Unit mysql.service has begun starting up.
Apr 29 02:10:41 ubuntu-512mb-server audit[10666]: AVC apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/sbin/mysqld" pid=10666 comm="apparmor_parser"
Apr 29 02:10:41 ubuntu-512mb-server kernel: audit: type=1400 audit(1524967841.661:66): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="/usr/sbin/mysqld" pid=10666 comm="apparmor_parser"
Apr 29 02:10:41 ubuntu-512mb-server mysqld[10667]: Initialization of mysqld failed: 0
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Control process exited, code=exited status=1
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: Failed to start MySQL Community Server.
-- Subject: Unit mysql.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysql.service has failed.
-- 
-- The result is failed.
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Unit entered failed state.
Apr 29 02:10:41 ubuntu-512mb-server systemd[1]: mysql.service: Failed with result 'exit-code'.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: mysql.service: Service hold-off time over, scheduling restart.
Apr 29 02:10:42 ubuntu-512mb-server systemd[1]: Stopped MySQL Community Server.
-- Subject: Unit mysql.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit mysql.service has finished shutting down.

This tells me this is not norma mysql shutdown and I need to check further logs. Use following command after trying MySQL restart.

$sudo tail -30 /var/log/mysql/error.log
2018-04-29T02:15:31.641869Z 0 [ERROR] Failed to initialize builtin plugins.
2018-04-29T02:15:31.641872Z 0 [ERROR] Aborting

2018-04-29T02:15:31.643813Z 0 [Note] Binlog end
2018-04-29T02:15:31.643871Z 0 [Note] Shutting down plugin 'MyISAM'
2018-04-29T02:15:31.644102Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

2018-04-29T02:15:32.135955Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-29T02:15:32.138090Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.21) starting as process 10937 ...
2018-04-29T02:15:32.141779Z 0 [Note] InnoDB: PUNCH HOLE support available
2018-04-29T02:15:32.141810Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-04-29T02:15:32.141818Z 0 [Note] InnoDB: Uses event mutexes
2018-04-29T02:15:32.141825Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2018-04-29T02:15:32.141831Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-04-29T02:15:32.141837Z 0 [Note] InnoDB: Using Linux native AIO
2018-04-29T02:15:32.142157Z 0 [Note] InnoDB: Number of pools: 1
2018-04-29T02:15:32.142291Z 0 [Note] InnoDB: Using CPU crc32 instructions
2018-04-29T02:15:32.144099Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2018-04-29T02:15:32.144141Z 0 [ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12
2018-04-29T02:15:32.144149Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool
2018-04-29T02:15:32.144154Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2018-04-29T02:15:32.144161Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2018-04-29T02:15:32.144165Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2018-04-29T02:15:32.144169Z 0 [ERROR] Failed to initialize builtin plugins.
2018-04-29T02:15:32.144172Z 0 [ERROR] Aborting

2018-04-29T02:15:32.146255Z 0 [Note] Binlog end
2018-04-29T02:15:32.146304Z 0 [Note] Shutting down plugin 'MyISAM'
2018-04-29T02:15:32.146569Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

Please note the error statements highlighted in bold.

Root cause.

This error is coming because my system is falling short of RAM memory. Normally when you use server with 512 MB RAM, this kind of issues occur.

Solution

  1. Increase the RAM of the server by upgrading your server capacity
  2. Add swapfile. I solved above problem by adding swapfile. Please refer to this excellent guide to add swap file.

 

MySQL database is corrupted

If your MySQL installation is corrupted, I hope you have some database backup available with you. There are options to recover data. If you have backup data available, best option is to reinstall MySQL. Please follow how to install MySQL on Ubuntu guide.

How to make background of an image transparent using gimp

When I was creating logo for one of my website, I faced an problem with logo background. No matter what I do, background stayed white and it looked out of sync whenever page had any other colour than white. But his is not a big issue, by using GIMP we can easily change background to transparent  and this takes hardly five minutes. Follow along below steps

Step#1 Add an alpha channel

You can add alpha channel two ways

Right click on image  Layer → Transparency → Add Alpha Channel

OR

From main menu Layer → Transparency → Add Alpha Channel

Step#2 Bucket fill with colour

Select “Bucket fill” option from “Toolbox” or simple use Shift+B
select Fill transparent areas
select BG colour fill and Sample merged
choose color and fill it. Make sure you select color which is not present in the image because we can going to make these areas as transparent

Step#3 Select by color

From menu  choose “select–>by color”

select Fill transparent areas as well as Sample merged.
You don’t have to select apply Antialiasing nor Feather edges.
then click on the color that you want to replace

GIMP how to make image background transparent
GIMP how to make image background transparent
Step#4 Make it transparent.

This is final and simplest stage. Simple press “delete” key and it will make your image background transparent.

Please let me know if you faced any issues wile doing this.

 

Basic SEO for WordPress

In this article we will look at basic SEO setup for WordPress website. Once you have installed WordPress, you would be looking to announce your web page to the world but problem is how ?

There are many ways to do this, you can pay Google or Facebook to display your page to targeted users (aka advertisement) but there is nothing like your content getting displayed to the users for free.

Having great content just is not the only requirement, you need to follow some basic steps to make sure your content is reaching target audience. Please follow below mention check points. This is not by any means must do list but most of the points are desirable and they will help you get more eyeballs.

  • Add google analytics to WordPress
  • Add WordPress website to google webmaster
  • Add WordPress  website to Bing webmaster
  • Add sitemap.xml ( Use Google XML Sitemaps plugin). Once sitemap is generated, make sure this is submitted to google and Bing webmaster. Once you add it in webmaster, it will be in pending status till its processed.
  • Add robots.txt file – You don’t need to login to your server to place robots.txt file. If you are using yoast SEO plugin got to Tools–>File Editor. Here you can create robots.txt file if its not already created. If its already created, you can edit the same.
  • Check for mobile friendliness

https://www.bing.com/webmaster/tools/mobile-friendliness

https://search.google.com/test/mobile-friendly

  • Make your website secure. You can add encryption using easy encrypt
  • Check website speed using following tool

https://developers.google.com/speed/pagespeed/insights/

  • Minify CSS and JS files by using plugin Fast Velocity Minify
  • Add a caching pluging to speed up your page . W3 Total Cache

How to use Onedrive as CDN for media files (Images, videos)

Although you can use built in media manager of wordpress, using OneDrive can be another option.

You can consider using OneDrive if you are short f space on your hosted server or your free account as well. OneDrive offers 5GB of free storage. Please follow following simple steps to use OneDrive as CDN for media files.

Step#1. Create an account on OneDrive and upload an image.

Step#2 Select that image and use click on embed option as show in below screenshot (btw, this image is hosted on OneDrive)

Once you select on embed, you need to click on generate code. This option is at right side.  On the same screen, you can choose the filesize to be shared.

 

Step#3 Now finally to insert images into your wordpress post, you “add Media” option but select option, “insert from url”

Once done, you are all set.

Before you start using, I must tel you disadvantage of this option is that, if wordpress is blocked at some place, these images wont be visible. I was using google drive as CDN but later found that google drive is blocked at many corporate networks however this issue is not there for OneDrive.

How to add google fonts to wordpress

  • Find right font

Access google fonts @ https://fonts.google.com/. You can browse or search to find the required font at this website.

  • Select the customisation as per your requirements.

Lets suppose you selected a font called “Crimson Text”. Click on “select this font” and it will open following window. please select the customisation required.

This will create a url, copy this url

 

  • Enqueueing google fonts in wordpress

Use the url copied in previous step and copy in into functions file with following code.

As you can see in below code, multiple fonts can be copied as below. In below function “Poppins” and “Crimson” is copied

function startwordpress_google_fonts() {
wp_register_style('Poppins', 'https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700');
wp_enqueue_style( 'Poppins');
wp_register_style('Crimson', 'https://fonts.googleapis.com/css?family=Crimson+Text:400,400i,600,600i,700');
wp_enqueue_style( 'Crimson'); 
}

add_action('wp_print_styles', 'startwordpress_google_fonts');

 

And you are all set. You can use this font in your world press CSS with the name that you have provided while registering this font. Normally you the same name while registering the font name to avoid the confusion.