MySQL 5.6 をインストールしたら最初に行うセットアップ

投稿者 : OSCA

[PR] "東野・岡村の旅猿"で登場したロケ地を紹介するファンサイト「あの場所へ行こう!」はこちら。

概要

 別稿では MySQL 5.6 をインストールする手順について解説しましたが、本稿では、MySQL 5.6 をインストール後に行うべきセットアップについて解説します。 MySQL 5.6 は、インストールが完了した時点では、安全に利用するためのセットアップが行われていませんので、本稿で紹介する手順により、安全に利用するための設定を行うことができます。

初期セットアップをしよう

 インストールした MySQL 5.6 によって違いはありますが、例えば CentOS にインストールした MySQL 5.6 を始めての起動の際には、MySQL が以下のようなメッセージを表示してくれます。 親切にも「ちゃんと初期設定した方が良いよ」と教えてくれています。

# service mysqld start
...(略)...
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  /usr/bin/mysqladmin -u root password 'new-password'
  /usr/bin/mysqladmin -u root -h centos65 password 'new-password'

Alternatively you can run:

  /usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.
...

 ここに書いてあることは、次のいずれかを行った方が良いということ。

  1. root ユーザーのパスワードを設定する。
  2. mysql_secure_installation を実行する。

ということで、この2つの設定について解説します。

root ユーザーのパスワード設定

 特に重要性を説明する必要はないかと思いますが、MySQL 5.6 をインストールした初期状態では MySQL の管理者となる root ユーザーのパスワードが設定されていない為、MySQL を安全に運用するためにパスワードを設定しましょう。 上記の CentOS 6 の例だと /usr/bin/mysqladmin -u root password ‘new-password’ コマンドでパスワードを設定することができます。(次のコマンドの例だと「new-password」が新しく設定するパスワードです)

# mysqladmin -u root password 'new-password'

 一度パスワードを設定した場合は、次のように -p を付けて、古いパスワードを入力してパスワードを変更します。(次の例だと「old-password」が現在のパスワード、「new-password」が新しく設定するパスワードです)

# mysqladmin -u root -p password 'new-password'
old-password

 このように root ユーザーのパスワードを設定しておくことは最低限必要でしょう。

mysql_secure_installation

 次に mysql_secure_installation について解説します。 実行する前にこのコマンドがどのようなものなのかを解説しましょう。このコマンドで設定できる内容は以下の通りです。

  • root ユーザーパスワードの変更
  • anonymous ユーザーの削除
  • リモートホストから root ユーザーでログインするのを禁止する
  • testデータベースの削除 (存在する場合)

 mysql_secure_installation はローカルでの開発で MySQL を利用するような場合は必要ないかもしれませんが、実際に運用する環境にインストールしたのであれば行うべきでしょう。

 このスクリプトでも安全の為、root ユーザーのパスワードを変更するかを聞いてくれます。 既にパスワードを変更していれば改めてパスワードを変更する必要はありません。

 また、MySQL の初期状態では anonymous という匿名用ユーザーが設定してあります。 実際に運用するには安全ではないユーザーのため、削除することを推奨します。

 また、リモートホストから root ユーザーでアクセスすることを禁止します。 基本的にはリモートから MySQL にアクセスすることを許す場合は、それ専用のユーザーを作成して許可する方が良いでしょう。

 そして、MySQL の初期状態に入っている test という名称のデータベースを削除します。

 実際のコマンドは次のようになります。 対話式ですので、Yes(Y) か No(n) で答えれば良いです。

# mysql_secure_installation
...
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
... skipping.

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? [Y/n] 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? [Y/n] 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? [Y/n] Y
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- 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? [Y/n] Y
... Success!

All done! If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Cleaning up...

おわりに

 これで MySQL をインストールして最初に行うべきセットアップが終了しました。 これで安心して MySQL を利用できますね。

MySQL 入門へ戻る

MySQLのトップへ戻る

著者 : OSCA

OSCA

Java, PHP 系のWEBエンジニア。 WEBエンジニア向けコミュニティ「WEBエンジニア勉強会」を主催。 個人として何か一つでも世の中の多くの人に使ってもらえるものを作ろうと日々奮闘中。
@engineer_osca