汎用的に使える自宅Linuxサーバを安価で手軽に構築(8)

はじめに

前回Apacheを導入してWebサーバを構築しました。今回はMySQLを導入してDBサーバを構築します。

MySQLのインストール

mysql-serverパッケージをインストールします*1

# yum install mysql-server

設定

/etc/my.cnfを編集します。my.cnfにはサーバー側の設定とクライアント側の設定の両方が含まれています。前者は[mysqld]セクションに、後者は[mysql]セクションに記述します。記述位置に注意しましょう。

[mysqld]
# mysqlサーバの設定
# 文字コードの設定を追加
default-character-set = utf8

[mysql]
# mysqlクライアントの設定
# 文字コードの設定を追加
default-character-set = utf8

サービスの起動設定

setupコマンドを使って設定します。DBサーバはWebサーバからのみ参照されることを想定しているので、ファイアウォールの設定は不要です。

# setup
[サービス]を選択して[mysqld]を有効に

起動

# /etc/init.d/mysqld start

これでMySQLのインストールと設定は完了です。

root(MySQLユーザ)のパスワード設定

MySQLにrootユーザでログインします。Linuxのユーザはrootでなくてかまいません。

$ mysql -uroot

現在のMySQLユーザを確認します。localhostとsabaにrootユーザがいることを確認します。両方のrootユーザのパスワードを変更してください。

mysql> SELECT user, host, password FROM mysql.user;
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |          |
| root | saba      |          |
|      | saba      |          |
|      | localhost |          |
+------+-----------+----------+
4 rows in set (0.00 sec)

mysql> SET PASSWORD FOR root@localhost=password('********');
Query OK, 0 rows affected (0.00 sec)

mysql> SET PASSWORD FOR root@saba=password('********');
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT user, host, password FROM mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | ***************************************** |
| root | saba      | ***************************************** |
|      | saba      |                                           |
|      | localhost |                                           |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

exit

今設定したパスワードでのログインを確認します。パスワード付きのユーザでログインするには-pオプションをつけてmysqlコマンドを実行します。

# mysql -uroot -p

匿名ユーザの削除

先ほどMySQLのユーザを確認したときに、user列が空欄のユーザがいました。これらは匿名ユーザと呼ばれるユーザです。特に匿名ユーザを使う予定がない場合は削除してしまいましょう。

mysql> SELECT user, host, password FROM mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | ***************************************** |
| root | saba      | ***************************************** |
|      | saba      |                                           |
|      | localhost |                                           |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> DELETE FROM mysql.user WHERE user='';
Query OK, 2 rows affected (0.00 sec)

mysql> SELECT user, host, password FROM mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | ***************************************** |
| root | saba      | ***************************************** |
+------+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

デフォルトで作られているtestデータベースは不要なので削除します。

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> DROP DATABASE test;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

MySQLユーザの作成

新しいMySQLユーザを作成するにはGRANT文を用います。次の例は、全てのDBに対して全権限を持つymknというユーザをlocalhostとその他全てのホストに対して作成します。

GRANT ALL PRIVILEGES ON *.* TO ymkn@"%" IDENTIFIED BY '********';
GRANT ALL PRIVILEGES ON *.* TO ymkn@localhost IDENTIFIED BY '********';

GRANT文の結果を反映させるために、次のSQL文を発行します。

FLUSH PRIVILEGES;

PHPのインストール

phpパッケージをインストールします。マルチバイト文字を扱うためのphp-mbstringやmysqlを扱うためのphp-mysqlも併せてインストールします。

# yum install php php-mbstring php-mysql

設定

/etc/php.iniを編集します。mbstring関連の設定を追加します。コメントアウトされているのでコメントを外して有効にしましょう。

[mbstring]
; language for internal character representation.
mbstring.language = Japanese

; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
mbstring.internal_encoding = UTF-8

; http input encoding.
mbstring.http_input = auto

; http output encoding. mb_output_handler must be
; registered as output buffer to function
mbstring.http_output = UTF-8

; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
mbstring.encoding_translation = Off
; automatic encoding detection order.
; auto means
mbstring.detect_order = auto

; substitute_character used when character cannot be converted
; one from another
mbstring.substitute_character = none;

; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
mbstring.func_overload = 0

; enable strict encoding detection.
mbstring.strict_encoding = Off

Webサーバ設定反映

# /etc/init.d/httpd reload

これでPHPのインストールは完了です。

おわりに

以上でLAMP環境の導入は完了しました。これで世の中に溢れるPHPで書かれたWebアプリケーションのほとんどを動作させられます。

あとは用途に合わせてカスタマイズしましょう。たとえば、ファイルマネージャを導入してファイル共有をしてみたり、SNSを設置して家族や友人との連絡帳を作ってみたり、使い方はいろいろです。

*1:mysqlパッケージではないことに注意