Pocket
LINEで送る

ローカルでRedmine使用する前にMySQLインストールする必要あり、パスワードの設定とかでつまずいたので、その備忘録。

OS | macOS Mojave 10.14.2

MacにhomebrewでMySQLインストールする際、パスワード設定ができなかったので、書いておきます。

$ brew install mysql
==> Installing dependencies for mysql: openssl
==> Installing mysql dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2q.mojave.bottl

######################################################################## 100.0%

==> Pouring openssl-1.0.2q.mojave.bottle.tar.gz
==> Caveats

・・・

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"
For pkg-config to find openssl you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/openssl/lib/pkgconfig"
==> mysql

We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default
To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql

Or, if you don't want/need a background service you can just run:
  mysql.server start

// 完了
$ mysql.server start
Starting MySQL

. SUCCESS!

もちろん、この状態ではパスワードなくてログインできません↓。(Optionコマンド -p)

$ mysql -u root -p
Enter password:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

パスワード無しならログイン可能です↓。

$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.13 Homebrew
Copyright (c) 2000, 2018, 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> quit
Bye

とは言っても、念の為パスワードはつけておきたいですね。

そこで、rootパスワードの設定方法調べてみると、

mysql> update mysql.user set password=password('設定パスワード') where user = 'root';

実行しろ、と書いてあったので、実行しました。

mysql> update mysql.user set 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 '('password') where user = 'root'' at line 1

へっ!?

一応、ユーザが存在しているのかも確認。

mysql> SELECT Host, User FROM mysql.user;

+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+

4 rows in set (0.00 sec)

ちゃんといますね。

このあといろいろ試しましたが、結局わからず、、、

っで、最終的にわかったのが、

MySQL5.7以上はPASSWORD関数が使えないとのこと。先言ってくれよ!!→ちゃんと公式見てやったのに気づかない私が悪い。

ってことで、スーパーユーザーになってmysql_secure_installationでパスワードを設定。

ちなみに、MySQLインストールしたときにはっきりと書いてありましたね。(上参照)

We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

読んでませんでした。。。

$ sudo -s // スーパーユーザになって、

Password: // PCログインパスワードを入力。

bash-3.2# mysql_secure_installation

Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are

secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: 0 // パスワードレベルの設定。わからない人は、とりあえず0入力して置けば良いでしょう。(0=設定パスワード8文字以上に設定する。)

Please set the password for root here.
New password: // ようやくパスワード入力。
Re-enter new password: // もう一回!

// 以下でてくるものは全て y → Enter → y → Enter → ・・・でおK!
・・・

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! // 完了

bash-3.2# exit
exit

完了したら、確認。

$ mysql -u root -p

Enter password: // さっき設定したパスワード入れる

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.13 Homebrew

Copyright (c) 2000, 2018, 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> // ログインできたら成功〜

お疲れ様でした!

スポンサーリンク
おすすめの記事