Praj's Blog

rmdir with -p

You might already know about this one, but if not, rmdir has a -p option like mkdir which allows it to recurse into any child folders and remove them as well if they are empty (it will throw an error if not empty). Handy if you have a folder structure a few levels deep that is empty and want to quickly remove all the folders in one hit. By the way if you didn’t know about mkdir -p it will let you create directories recursively as many levels deep as you need e.g. mkdir -p level1\level2\level3

Market Index

A great, free site to get information about ASX listed shares, including reports on Dividend Yield and Consensus Recommendations. Also delivered to you as a spreadsheet if you register for updates.

PHP database can’t connect to localhost but 127.0.0.1 works

There are many reasons for this, so first check that MySQL/MariaDB is running properly, that you are using the correct credentials and that the the appropriate user@localhost has access.

Also check you aren’t using skip-name-resolve and bind-address in your my.cnf file if accessing from outside of the localhost. These are things you will find with a google search.

Apart from this one other reason why 127.0.0.1 will work and localhost will not work is if you are using something other than the default socket=/var/lib/mysql/mysql.sock location in your my.cnf.

If that is the case, you’ll need to update the relevant default_socket= parameter in php.ini (for each driver) to match that socket – see the entries under my.cnf [mysql] and [mysqld] sections for the socket parameter. This is more likely to occur if you are using MariaDB for example.

Command line grep

Here’s a pretty nice combination of parameters for command line grep using egrep (which supports regex):

egrep -rHnC1 "bprajb"

This will search in the current directory recursively through all files that match the word “praj” (regex boundary b tags) and return the file name and context (1 line either side of the match) with line numbers.

For example here’s the resut with the matching word highlighted as well on the terminal.

MySQL compare strings by case

Here’s one method of comparing strings by case in MySQL. You can use the md5 function to hash the string and compare to its lower case value.

This example is for checking for usernames that are not lower case in moodle.

select username, md5(username), md5(lower(username))
from mdl_user
where deleted = 0 and suspended = 0
and md5(username) != md5(lower(username));

If the md5 hash when the string is lower cased is different to the current md5 hash you will get a result.