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.

Killing a mass of queries from MySQL

mysql

to kill a whole bunch of mysql queries in one go, I used something like this that you can adapt for your own use:

select concat('KILL ',id,';') from information_schema.processlist where Host like 'some-sexy-server-ws%';
select concat('KILL ',id,';') from information_schema.processlist where Host like 'some-sexy-server-ws%' into outfile '/tmp/a.txt';
source /tmp/a.txt

sorting mysql processlist by order of date

mysql

How to set a mysql processlist to display by Time of query to help locate long running queries:

mysql> \P awk -F\| '$2 ~ /[0-9]+/ && $6 ~ /Sleep/' | sort -t \| -k 7 -n -r
PAGER set to 'awk -F\| '$2 ~ /[0-9]+/ && $6 ~ /Sleep/' | sort -t \| -k 7 -n -r'
mysql> show processlist;

mysql install from a binary distro

mysql

Installing mySQL from a binary is very simple. Download the binary to your server.

tar zxvf mysql-binary.tar.gz
cd mysql-binary
mkdir /mysql/partition/here
./configure --prefix==/mysql/partition/here
make
make install

Alternatively, even easier:

tar zxvf mysql-binary.tar.gz
cd /mysql/partition/here
chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql;
cd .;
./bin/mysqladmin -uroot -pSoMePasSwOrdHeRe

installing mySQL from the cli

mysql

Installing mySQL from the command line couldn’t be easier. Installing from root would ensure that everything will be in place.


yum groupinstall "MySQL Database"
/usr/bin/mysql_install_db --user=mysql
service mysqld start
/usr/bin/mysql_secure_installation
chkconfig mysqld on