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
owners.

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

mysql>

 

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

mysql>

 

And then,

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql>

 

Exit.

mysql> \q
Bye
sh-3.2# exit
exit

Done.

 

Now try logging in with the new password.

$ mysql -u root -p
Enter password:

 

It works!

Notes: Using MySQL

Remember:

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: Connect PHP5 to MySql in Mac OS X

I came across this error in my Apache error log file found in /var/log/apache2/error_log.

PHP Warning:  mysql_connect(): [2002] No such file or directory
(trying to connect via unix:///var/mysql/mysql.sock)

I figured out that this is due to PHP not being able to find the MySql socket to connect to the MySql database server. To be exact, PHP is looking for a file named mysql.sock.

I turned to look for mysql.sock in the MySql configuration file found in /usr/local/bin/mysql_config.

pkgincludedir='/usr/local/Cellar/mysql/5.6.13/include'
if [ -f "$basedir/include/mysql/mysql.h" ]; then
    pkgincludedir="$basedir/include/mysql"
elif [ -f "$basedir/include/mysql.h" ]; then
    pkgincludedir="$basedir/include"
fi

version='5.6.13'
socket='/tmp/mysql.sock'
ldflags=''

if [ 0 -eq 0 ]; then
    port=0
else
    port=3306
fi

# Create options

PHP was looking for the file /var/mysql/mysql.sock but the file exists in /tmp/mysql.sock.

So what I did was to simply create a symlink for the mysql.sock file in the /var/mysql/ folder.

$ sudo mkdir /var/mysql
$ ln -s /tmp/mysql.sock /var/mysql/mysql.sock

I could have modified the line in the mysql_config file to point to /var/mysql/mysql.sock but this is not good for other applications in Mac OS X that need the mysql.sock file in /tmp/mysql.sock.

Testing:

To test if indeed PHP can connect to MySql, I created a file named ~/Sites/test-php-mysql.php with the following contents.

<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'password';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error: Could not connect to MySql.');

    if ($conn) {
        echo 'Successfully connected to MySql.';
    }

    $dbname = 'mysql';
    mysql_select_db($dbname);
?>

Then I launched Safari and typed http://localhost/~darwin/test-php-mysql.php in the address bar. It showed that the connection was established.

Successfully connected to MySql.

It worked!