Categories
code

How To Recover Your MySQL root Password

I just needed to recover a lost MySQL root password on a clients server recently and thought I’d share the method I used.

[ad name=”Adsense – text only”]

Actually it’s not a method for recovering but you can change the forgotten password to a new one.
All you need is to follow this 6 easy steps:

Step 1: Stop the mysql service

# /etc/init.d/mysql stop

Expected Output:

Stopping MySQL database server: mysqld.

Step 2: Start the MySQL server without password

# mysqld_safe --skip-grant-tables &

Expected Output:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started

Step 3: Connect to your MySQL server using the MySQL client

# mysql -u root

Expected Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Step 4: Setup a new MySQL password for the root user

mysql> use mysql;
mysql> update user set password=PASSWORD("YOUR-NEW-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit;

Expected Output:

Database changed

Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Bye

Step 5: Stop the MySQL Server

# /etc/init.d/mysql stop

Expected Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+  Done                    mysqld_safe --skip-grant-tables

Step 6: Start the MySQL server and test it

# /etc/init.d/mysql start
# mysql -u root -p

Expected Output:

Starting service MySQL         done

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is xx
Server version: x.x.xx XXXXX MySQL RPM

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Now you should be all done!
I hope this helps you as much as it did for me.

Categories
code

Human readable and by size sorted disk usage (du) in BASH

This is a quick tip to fix a problem that has always bugged me.
When showing disk usage in a human readable form (KB, MB, GB) for each subdirectory using du -sh *, how can you properly sort it by size.

If you just want the solution here it is as “one-liner”:

function duf {
    du -sk "$@" | sort -n | while read size fname; do for unit in k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done; done
}

Just put this function into your ~/.bashrc to make it permanent.

[ad name=”Adsense – text only”]

The expanded code:

function duf {

    // get usage in KBytes and sort
    du -sk "$@" | sort -n | while read size fname;
   
    // loops for each size
    do for unit in k M G T P E Z Y;
   
        // if size<1024 we found the correct suffix
        if [ $size -lt 1024 ];
       
        // display the size
        then echo -e "${size}${unit}\t${fname}";
       
    // line completed
    break;
    fi;
   
    // for each sizes suffix divide by 1024
    size=$((size/1024));
   
    done;
    done
}

When done, you can use your new function like this: duf /*.