今回はMySQLのインストール〜MySQLでタイムゾーンを設定するまで、記載したいと思います。
何気に手こずったので…macでの手順となります。
環境
OS/ミドルウェア | バージョン |
---|---|
macOS Sierra | 10.12.6 |
MySQLのインストール
Homebrew でインストールします。Homebrew自体が入ってなければ、Homebrewの公式ページどおり、次のようにインストール
1 |
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
※ 執筆時とコマンド内容が変わってるかもしれないので、公式ページを確認ください。
インストール済みの人は、飛ばしてください。
[MySQLのインストール]
1 |
$ brew install mysql |
[MySQLのバージョン確認]
1 2 |
$ mysql --version mysql Ver 8.0.17 for osx10.12 on x86_64 (Homebrew) |
[MySQLのインストールされた場所]
1 2 |
$ which mysql /usr/local/bin/mysql |
[MySQLの起動]
1 2 3 |
$ mysql.server start Starting MySQL .. SUCCESS! |
[MySQLの接続]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.17 Homebrew Copyright (c) 2000, 2019, 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>
プロンプトで exit
を入力し接続を切ります。
MySQL のセキュリティ設定
rootユーザのパスワードなどを変更するため、次のコマンドで以下の変更を行っていきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# mysql_secure_installationを実行 $ 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? # VALIDATE PASSWORDというコンポーネントをインストールするか。 # ゆるいパスワード(abc/passwdなど)を登録したい場合はn Press y|Y for Yes, any other key for No: y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file # ログインパスワードの強度指定。 Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 Please set the password for root here. # パスワード入力 New password: # 確認のためもう一度入力します Re-enter new password: Estimated strength of the password: 100 # 入力したパスワードで先に進みたければy Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. # 匿名ユーザを削除しますか? Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. # リモートログインを許可しない設定にしますか? Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. # テスト用データベースを削除してもいいですか? Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - Removing privileges on test database... Success. 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! |
MySQLにログインできるか確認
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ 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.17 Homebrew Copyright (c) 2000, 2019, 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でタイムゾーンを設定する
初期設定のままだと、time_zoneは「SYSTEM」になっているので、「Asia/Tokyo」に変更します。
[タイムゾーンの確認]
1 2 3 4 5 6 7 8 |
mysql> SHOW VARIABLES LIKE '%time_zone%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | JST | | time_zone | SYSTEM | +------------------+--------+ 2 rows in set (0.04 sec) |
[タイムゾーンデータの確認]
1 2 |
mysql> select * from mysql.time_zone; Empty set (0.06 sec) |
タイムゾーンデータがない場合は、タイムゾーンデータのインポートを行います。
[タイムゾーンデータのインポート]
1 2 |
$ mysql_tzinfo_to_sql /usr/share/zoneinfo/ | mysql -u root mysql -p Enter password: パスワードの入力 |
[タイムゾーンデータの確認]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
mysql> select * from mysql.time_zone; +--------------+------------------+ | Time_zone_id | Use_leap_seconds | +--------------+------------------+ | 1 | N | | 2 | N | | 3 | N | | 4 | N | | 5 | N | 省略 | 590 | N | | 591 | N | | 592 | N | | 593 | N | | 594 | N | | 595 | N | +--------------+------------------+ 595 rows in set (0.00 sec) |
[my.cnfを変更]
/usr/local/etc/my.cnf
1 2 |
[mysqld] default-time-zone = 'Asia/Tokyo' |
[MySQLを再起動]
1 2 3 4 5 6 7 |
$ mysql.server stop Shutting down MySQL .... SUCCESS! $ mysql.server start Starting MySQL .. SUCCESS! |
[タイムゾーンの確認]
1 2 3 4 5 6 7 8 |
mysql> SHOW VARIABLES LIKE '%time_zone%'; +------------------+------------+ | Variable_name | Value | +------------------+------------+ | system_time_zone | JST | | time_zone | Asia/Tokyo | +------------------+------------+ 2 rows in set (0.01 sec) |