I did this to set my root password in initial set up of MySQL in OSx. Open a terminal.
sudo sh -c 'echo /usr/local/mysql/bin > /etc/paths.d/mysql'
Close the terminal and open a new terminal. And followings are worked in Linux, to set root password.
sudo /usr/local/mysql/support-files/mysql.server stop
sudo mysqld_safe --skip-grant-tables
(sudo mysqld_safe --skip-grant-tables : This did not work for me in first time. But second try, out was success.)
Then login to MySQL
mysql -u root
FLUSH PRIVILEGES;
Now change the password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Restart MySQL:
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start