Notes: Reset Expired root Password for MySQL 5.7 on Mac OS X

I installed MySQL 5.7 from a .dmg package installer for Mac OS X. I followed the installer taking note of the temporary root password given. After the installation, I cannot login as root because the password expired. Here’s what I did.


I stopped MySQL in System Preferences.

MySQL in System Preferences

MySQL in System Preferences


Switched user to root.

$ sudo su


Started MySQL in safe mode.

$ sudo mysqld_safe --skip-grant-tables


I opened another terminal tab/window and lauched mysql shell there.

sh-3.2# mysql -u root

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.



Changed root password.

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE user='root';
Query OK, 0 rows affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 0  Warnings: 1



And then,

Query OK, 0 rows affected (0.02 sec)




mysql> \q
sh-3.2# exit



Now try logging in with the new password.

$ mysql -u root -p
Enter password:


It works!

Notes: Using MySQL


Replace everything that starts with “a_”.


Login to MySQL console.

$ mysql -u root -p

Backup a MySQL database.

$ mysqldump -u a_username a_database > a_file.sql

Restore from backup.

$ mysql -u a_username -p a_database < a_file.sql

Update MySQL user password.

mysql> UPDATE USER.master SET PASSWORD = AES_ENCRYPT('a_password', 
'a_passkey') WHERE username = 'a_username'

It works!

Notes: Using PostgreSQL


Bash v4.2
PostgreSQL v9.5


Replace everything that starts with “a_”.


launch psql.

$ sudo su postgres
bash-4.2$ psql


postgres=# \q

List databases.

postgres=# \l

Create user.

postgres=# CREATE ROLE a_username LOGIN PASSWORD 'a_password';

Change user password.

postgres=# ALTER ROLE postgres WITH PASSWORD 'a_password';

Create database.

postgres=# CREATE DATABASE a_database WITH OWNER a_username;

Drop database.

postgres=# DROP DATABASE a_database;

Drop user.

postgres=# DROP ROLE a_username;

Adding PostGIS extension.

$ sudo su postgres
$ psql -d a_database
a_database=> CREATE EXTENSION postgis;


Backup as dump file (tested to work in migrating servers).

$ pg_dump -Fc -h localhost -U a_username -f a_file.dump a_database

To create a non-compressed backup.

$ pg_dump -h localhost -U a_username -f a_file.sql a_database

To create a schema-only database backup.

$ pg_dump -s -h localhost -U a_username -f a_file.sql a_database


To restore a database from a dump (works with backups as dump only). The database should already exist. If an error occurs for some reason, use postgres as the username.

$ pg_restore -c -h localhost -U a_username -d a_database a_file.dump

To restore a database from a non-compressed backup.

$ psql -h localhost -U a_username a_database < a_file.sql


To recreate a fresh initdb’d PostgreSQL instance.

$ pg_dropcluster


Database management commands.

$ psql -h localhost -U a_username -d a_database
sampledb=> select * from a_tablename where id=168;

It works!