MySQLのインストール直後は、セキュリティ上問題がある設定となっています。
また、日本語を扱う際に文字化けが発生する可能性があります。
今回は、CentOS6にMySQL5.7をインストールしたらやっておくべき初期設定について説明します。
※以下の作業環境を例に説明します。
- サーバOS:CentOS6
- MySQL:MySQL5.7
1.MySQLのrootユーザの初期パスワード確認
MySQL5.7では、インストール直後、MySQLのrootユーザの初期パスワードが、「/var/log」フォルダ配下にある「mysqld.log」ファイル(MySQLのログファイル)に出力されます。
MySQLのrootユーザのパスワード変更を行う際に使うので、事前にこの初期パスワードを確認しておきましょう。
① まず、サーバにrootユーザでログインします。
[root@ ~]#
② 次に、「mysqld.log」ファイルがある「/var/log」フォルダへ移動します。
「cd /var/log」と入力してEnterを押します。
[root@ ~]# cd /var/log
「/var/log」フォルダへ移動できました。
[root@ log]#
③ 「mysqld.log」ファイルを開いて内容を確認します。
「view mysqld.log」と入力してEnterを押します。
[root@ log]# view mysqld.log
「mysqld.log」ファイルの内容が表示されます。
「A temporary password is generated for root@localhost:」の後に出力されている文字列がMySQLの初期パスワードになります。
2016-12-26T14:55:52.310859Z 1 [Note] A temporary password is generated for root@localhost: B-jRFs5ocTbP
この文字列をコピーして保存しておきましょう。
2.mysql_secure_installation
MySQLのインストール直後は、セキュリティ上問題がある設定となっています。
そこで、「mysql_secure_installation」を使って対応します。
「mysql_secure_installation」では、以下の設定を行うことができます。
- MySQLのrootユーザのパスワード変更
- 匿名ユーザの削除
- 外部からのrootユーザでのログイン禁止
- testデータベースの削除
① まず、MySQLを起動します。
「service mysqld start」と入力してEnterを押します。
[root@ log]# service mysqld start
MySQLが起動しました。
mysqld を起動中: [ OK ]
② 「mysql_secure_installation」を実行します。
「mysql_secure_installation」と入力してEnterを押します。
[root@ log]# mysql_secure_installation
それでは、以降の手順に従って、一気に編集していきましょう。
2-1.MySQLのrootユーザのパスワード変更
① まず、MySQLのrootユーザのパスワードを聞かれます。
上記の手順1.でコピー・保存したMySQLの初期パスワードを入力してEnterを押します。
Securing the MySQL server deployment.
Enter password for user root:MySQLの初期パスワード
② 次に、MySQLのrootユーザのパスワード変更を行うかどうかを聞かれます。
「y」を入力してEnterを押します。
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :y
③ MySQLのrootユーザの新しいパスワードを聞かれます。
任意のパスワードを入力してEnterを押します。
The existing password for the user account root has expired. Please set a new password.
New password:新しいパスワード
④ 再度、MySQLのrootユーザの新しいパスワードを聞かれます。
上記③の手順で入力したパスワードを再度入力してEnterを押します。
Re-enter new password:新しいパスワード
⑤ MySQLのrootユーザのパスワード変更を行うかどうかを聞かれます。
「y」と入力してEnterを押します。
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :y
これで、MySQLのrootユーザのパスワード変更が完了しました。
2-2.匿名ユーザの削除
MySQLのrootユーザのパスワード変更が完了すると、匿名ユーザを削除するかどうかを聞かれます。
「y」と入力してEnterを押します。
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.
2-3.外部からのrootユーザでのログイン禁止
匿名ユーザの削除が完了すると、外部からのrootユーザでのログインを禁止するかどうかを聞かれます。
「y」と入力してEnterを押します。
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
外部からのrootユーザでのログインを禁止できました。
Success.
2-4.testデータベースの削除
外部からのrootユーザでのログイン禁止の設定が完了すると、testデータベースを削除するかどうかを聞かれます。
「y」と入力してEnterを押します。
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
testデータベースを削除できました。
- Dropping test database... Success. - Removing privileges on test database... Success.
2-5.権限テーブルの即時リロード
testデータベースの削除が完了すると、権限テーブルを即時リロードするかどうかを聞かれます。
「y」と入力してEnterを押します。
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_secure_installationによる設定はすべて完了です。
3.my.cnfのバックアップ
mysql_secure_installationによる設定が完了したら、MySQLの設定ファイルによる設定を行います。
MySQLの設定ファイルによる設定は、「/etc」フォルダ配下にある「my.cnf」ファイル(MySQLの設定ファイル)で設定します。
「my.cnf」ファイルを編集する前に、「my.cnf」ファイルのバックアップを行いましょう。
① 「my.cnf」ファイルがある「/etc」フォルダへ移動します。
「cd /etc」と入力してEnterを押します。
[root@ ~]# cd /etc
「/etc」フォルダへ移動できました。
[root@ etc]#
② 編集前の「my.cnf」ファイルをバックアップするため、「my.cnf」ファイルをコピーして、「my.cnf.yyyymmdd」というファイル名で保存します。
「cp my.cnf my.cnf.yyyymmdd」と入力してEnterを押します。
なお、「yyyymmdd」は設定変更を行う日など任意の年月日とします。
(以下は、20170325とした例)
[root@ etc]# cp my.cnf my.cnf.20170325
これで、「my.cnf」ファイルをバックアップできました。
4.my.cnfの編集
「my.cnf」ファイルをバックアップできたら、「my.cnf」ファイルを編集していきます。
「vim my.cnf」と入力してEnterを押します。
[root@ etc]# vim my.cnf
「my.cnf」ファイルの内容が表示されます。
以降の手順に従って、一気に編集していきましょう。
4-1.日本語環境に関する設定
サーバ側が使用するデフォルトの文字コードを指定します。
デフォルト値は「latin1」です。
日本語を使うので「utf8」を指定します。
以下、[mysqld]、[client]、[mysql]、[mysqldump]のすべてのセクションで「utf8」を指定します。
[mysqld] character_set_server
[mysqld]セクションに「character_set_server=utf8」を設定します。
[mysqld] character_set_server=utf8
[client] default-character-set
[client]セクションに「default-character-set=utf8」を設定します。
[client]セクションはデフォルトではないので「[client]」も設定します。
[client] default-character-set=utf8
[mysql]default-character-set
[mysql]セクションに「default-character-set=utf8」を設定します。
[mysql]セクションはデフォルトではないので「[mysql]」も設定します。
[mysql] default-character-set=utf8
[mysqldump]default-character-set
[mysqldump]セクションに「default-character-set=utf8」を設定します。
[mysqldump]セクションはデフォルトではないので「[mysqldump]」も設定します。
[mysqldump] default-character-set=utf8
これで、my.cnfの設定は完了です。
5.my.cnfの有効化
my.cnfの設定が完了したら、設定を保存して有効化します。
① vimで開いた「my.cnf」ファイルの設定を保存し、ファイルを閉じます。
「:wq」と入力してEnterを押します。
:wq
② 設定した内容を有効化するために、MySQLを再起動します。
「service mysqld restart」と入力してEnterを押します。
[root@ etc]# service mysqld restart
MySQLの再起動が完了しました。
mysqld を停止中: [ OK ] mysqld を起動中: [ OK ]
これで、MySQLの設定が有効化されました。