今回はMySQLのrootユーザーのパスワードを設定していきます。
rootパスワードの確認
MySQL5.6以降は初期パスワードが乱数で自動生成させるようになったようで、/root/.mysql_secretに記述されています。
以下のコマンドを実行してみると。
[bash] [root@localhost ~]# sudo cat /root/.mysql_secret [/bash]
以下のように表示されます。
[bash]
The random password set for the root user at Wed Jul 20 15:13:27 2016 (local time): 1GvoL1gpgyAo15ox
[/bash]
初期パスワードでログイン
以下のコマンドでログインしてみます。 [bash] [root@localhost ~]# mysql -u root -p [/bash]
以下のようにパスワードが聞かれるので、上記で表示されたパスワードを入力します。 [bash] Enter password: [/bash]
ログインができました。
[bash] Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 5.6.25
Copyright (c) 2000, 2015, 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.
初期パスワードからパスワードを変更する
以下のコマンドで自分の好きなパスワードを変更します。 [bash] mysql> SET PASSWORD FOR root@localhost=PASSWORD('自分の設定したいパスワード'); [/bash]
以下のように表示されればパスワード設定OKです。実際にログインするために、一旦ログアウトします。 [bash] mysql> exit [/bash] ※入力を誤って、Enterを押しても開業されて「->」がひたすら表示されるような状態の場合、[\c][Enter]の押下で「mysql>」の状態に戻ることができます。
自分の設定したパスワードでログイン
以下のコマンドでログインしてみます。 [bash] [root@localhost ~]# mysql -u root -p [/bash]
以下のようにパスワードが聞かれるので、上記で表示されたパスワードを入力します。 [bash] Enter password: [/bash]
ログインができると思います。
現在あるデータベースを確認する
せっかくログインできたので、現在あるデータベースをついでに確認してみます。 [bash] mysql> show databases; [/bash]
以下のように表示されます。 [bash] +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ [/bash]