2つ方法があることを知ったのでメモ書き
下記の1の方法しか知らなかった。
1.skip-grant-tablesオプションを使う
--skip-networkingをするとなお良し
mysqld --skip-grant-tables --skip-networking --user=mysql &
MySQLサーバーにログイン後、FLUSH PRIVILEGESをして、ALTER USERする。FLUSH しないとエラーになる
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
2.init-fileオプションを使う
使用したinit-fileは削除する
mysqld --init-file=/path/to/init --user=mysql &で起動する。
initファイルの中身は
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
5.7.5から前のバージョンだと以下のコマンド
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');