flaskでMySQLに接続しようと思ったのですが、
あれ、パスワードが分かんねー!
ってなって散々苦労して、ようやく解決できたので、
対処法を書いておきます。
環境
Mac 10.15.4
Server version: 8.0.19 Homebrew
MySQLのパスワードが分からない
まずMySQLにrootで入って、パスワードをアップデートを試してみました。
update user set authentication_string=PASSWORD('password') where User='root';
以下のようなエラーができます。
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('xxxxxxxx') where User='root'' at line 1
パスワードの設定
仕方ないので、一度、ストップさせます。
mysql.server stop
セーフモードに変更する。
mysqld_safe --skip-grant-tables
パスワードを使わずに、入ってみます。
mysql -u root
無事に入れました。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 Homebrew
Copyright (c) 2000, 2020, 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>
MySQLデータベースを選択します。
use mysql;
無事に変更できました。
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
先と同じようにパスワードのアップデートを試してみます。
update user set authentication_string=PASSWORD('password') where User='root';
しかし、結局エラーがでました。やはりダメです!
それから数時間、頭を悩ましましたが、
正解はこっちでした!(passwordには任意のパスを指定してください)
ALTER USER 'root'@'localhost' identified BY 'password';
やったー!!!OKがでました!
Query OK, 0 rows affected (0.01 sec)
セーフモードなので、一度出ます。
exit;
MySQLにパスワードでログイン
改めてMySQLをスタートさせます。
mysql.server restart
ここまではいつも通り。
Shutting down MySQL
.. SUCCESS!
Starting MySQL
.. SUCCESS!
いざ、パスワードでログインを試みます!
mysql -u root -p
パスワードを入力します。
Enter password:
きた!!!!入れました!
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 Homebrew
Copyright (c) 2000, 2020, 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>
無事に完了!
まとめ
僕もそうでしたが、MySQL初心者がぶち当たりであろうポイントだと思います。
これがクリアできれば、ようやくpythonやflaskでMySQLを連携させることができるようになるはずです。
【参考にさせていただいたサイト】