Displaying the size of each MySQL table in GB

mysql

Use the below command to display the size of each MySQL table in GB. This will also show the size of the index length, the number of rows and the total data length.

SELECT CONCAT(table_schema, ‘.’, table_name),
CONCAT(ROUND(table_rows / 1000000, 2), ‘M’) rows,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), ‘G’) DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), ‘G’) idx,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), ‘G’) total_size,
ROUND(index_length / data_length, 2) idxfrac
information_schema.TABLES
ORDER BY data_length + index_length DESC
LIMIT 10;

Resetting a forgotten MySQL root password

mysql

When you have forgotten you MySQL root password, with root access it is easy enough to get back into the database and reset the password.

Firstly, bring down your MySQL database. This can be as simple as running:

/etc/init.d/mysqld stop

Then once you have confirmed your MySQL instance is no longer running:

ps -ef | grep -i mysql

You are free to restart the MySQL instance again without using the user tables:

mysqld_safe –skip-grant-tables &

Confirm that the MySQL instance is running again, and it is likely that you will have received a warning had it have not started properly anyway:

ps -ef | grep -i mysql

Then you are free to login to MySQL again:

mysql -uroot

Then once you are successfully back in the database, you can choose whether or not you want to reset the password again:

UPDATE mysql.user SET Password=PASSWORD(‘some_password_here’) WHERE User=’root’;
FLUSH PRIVILEGES; 

Dabbling with iptables to ensure remote MySQL connection works properly

iptables, mysql

I found in a cluster of machines that I couldn’t access MySQL remotely anymore. After some playing around with GRANT PRIVILEGES in MySQL, I realised that the issue wasn’t actually because of the MySQL settings at all.

So, was it the port number that I was connecting to that happened to be wrong?

netstat -tln | grep 3306

Showed that MySQL is running on port 3306 as intended.

telnet xx.xx.xx.xx 3306

From machine A to machine B refused to accept connections, so I knew that we had an issue somewhere else – probably on the firewall – stopping me from connecting remotely.

I didn’t really have much experience with iptables, so had to read a little bit more into it. Firstly, I needed to display all the rules on the firewall with this command:

/sbin/iptables -L -v -n

…under the Chain RH-Firewall-1-INPUT I saw no mention of the open port 3306 for MySQL.  We needed to add two rules, for tcp and udp connections.

/sbin/iptables -I RH-Firewall-1-INPUT -p tcp –dport 3306 -j ACCEPT

/sbin/iptables -I RH-Firewall-1-INPUT -p udp –dport 3306 -j ACCEPT

Afterwards, I was able to connect remotely to the MySQL machine again.

Mac OSX save file dialog expansion

mac osx

On Mac OSX, to change the Save As dialog to ensure that it always expanded, you can run a command from Terminal that will permanently change the setting. I prefer to have a Windows style Save dialog that gives me a list of all of the files and folders, as opposed to the default in Mac OSX that shows just the last/default directory.

defaults write -g NSNavPanelExpandedStateForSaveMode -bool TRUE

GoogleTalk open port listening to connections

security

I noticed that GoogleTalk appeared to be running on my computer and listening to connections, but I couldn’t even remember installing it.

To uninstall it in Mac OSX, from a Terminal run:

/Library/"Application Support"/Google/GoogleVoiceAndVideoUninstaller.app/Contents/MacOS/GoogleVoiceAndVideoUninstaller

screen

linux, screen

I noticed that sometimes even if I would scp files from server to server, the connection would eventually timeout as soon as I logged out of the machine.

One solution is to use the excellent application ‘screen’. This allows you to multiplex more than one virtual console, keeping the terminal alive even if you are disconnected on your connection.

To create a screen, simply run:

screen -R "some_terminal_name"

When you want to return to the original window, you can hold CTRL+A+D. That takes you back to your original terminal. IF you want to return to the screen again, use the same command as mentioned above.

Using perl to remove duplicates from an array

perl

I needed to remove duplicate elements from an array in Perl. The internet has a range of different ways to do this, but I found the best and quickest way to do so was to use a temporary hash.

push @data, $some_fancy_variable;
my %temp_hash = map { $_, 0 } @data;
@unique = keys %temp_hash;

Leaving the new @unique array with the duplicates removed.

 

Fun with exim

exim, linux

exim is a mail transfer agent for Unix environments. exim follows the sendmail design model, and is highly configurable with many features.

Some of my favourite commands are:

exim -bp | exiqsumm
- Generates a summary about all of the messages currently in the queue, including total and time spent in the queue
exiwhat
- Shows what exim is currently doing
exim -Mrm <message-id>
- Remove a message from the queue
exiqgrep -z -i | xargs exim -Mrm
- Delete all frozen messages from the queue