mySQL: How to reset root password

If the password for the mysql “root” user is lost, it’s still possible reset it to some other value.
The restart of the mySQL server will be necessary, of course.

The following should be done:

Stop the server
It could be done normally:

/etc/init.d/mysql stop

or effectively:

ps -ef | grep mysql
...
mysql 25079 1 0 Jan 31 ? 0:00 /usr/bin/mysqld
...
kill -9 25079

Please ensure that the proper process is killed!

Start MySQL server without security checking

mysqld_safe --skip-grant-tables &

Connect to mySQL without password

mysql -u root

Reset mySQL root password

use mysql;
update user set password=PASSWORD("NEW_PASSWORD") where user='root';
flush privileges;

Use the real new password instead of the string “NEW_PASSWORD”.

Restart mySQL server

/etc/init.d/mysql stop
/etc/init.d/mysql start

Check the connection

mysql -u root -p

Reading password in Unix shell


print -n "Enter Your password:"
stty_orig=`stty -g`
trap "stty ${stty_orig}; exit" 1 2 3 15
stty -echo >&- 2>&-
read PASS
stty ${stty_orig} >&- 2>&-
trap 1 2 3 15
print

trap :catches interruptions. I.e. if the user presses Ctrl+C, the normal stty mode is set before stopping the program
stty -echo :switches off the display echo
>&- 2>&- :helps to avoid “stty: Not a typewriter” message for non-interactive scripts.

Oracle: password function

Find the name of the current password function


SELECT * FROM DBA_PROFILES WHERE RESOURCE_NAME='PASSWORD_VERIFY_FUNCTION';

Change the password function for the profile:


ALTER PROFILE &profile. LIMIT PASSWORD_VERIFY_FUNCTION &function_name.;

Example of the password function:


CREATE OR REPLACE FUNCTION dummy_func
(USERNAME VARCHAR2, PASSWORD VARCHAR2, OLD_PASSWORD VARCHAR2)
RETURN BOOLEAN IS
BEGIN
IF NLS_LOWER(password) = NLS_LOWER(username) THEN
raise_application_error(-20001, 'Password is the same as the username');
END IF;
RETURN(TRUE);
END;