1,000円を切る低価格で登場、「さくらのVPS」をチェック――サーバー構築編 6ページ

MySQLデータベースサーバーの構築

 VPS上でWebアプリケーションを稼働させる場合、データベースサーバーが必要な場合が多い。特にWebアプリケーションの分野ではMySQLが採用されている例が多いようだ。以下では、MySQLのインストールと設定について解説する。

MySQLのインストールと設定

MySQLサーバーのインストール

 MySQLサーバーのインストールは、Apacheと同様「yum install」コマンドで行える。

$ sudo yum install mysql-server

MySQLサーバーの初期設定

 MySQLサーバーをインストールすると、同時に設定ファイル「/etc/my.cnf」がインストールされる。デフォルトでは文字コードの設定がされていないので、UTF-8に設定しておこう。

$ sudo vi /etc/my.cnf  ←設定ファイルを編集
 :
 :
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0
# 「symbolic-links=0」の次に下記を追加
character-set-server=utf8

[mysqldump]
default-character-set=bynary

[client]
default-character-set=utf8
 :
 :

 また、MySQLサーバーの初回インストール時はMySQL権限テーブルの生成が必要だ。次のように「mysql_install_db」コマンドを実行する

$ sudo mysql_install_db

 以上の設定が完了したら、MySQLサーバーを起動してOSの起動時に立ち上がるように設定しておく。

$ sudo /sbin/service mysqld start
$ sudo /sbin/chkconfig mysqld on

 サーバーが起動したら、正しくアクセスできるか確認しておこう。

$ mysql -u root  ←root権限でデータベースにアクセス
Welcome to the MySQL monitor.  Commands end with ; or ¥g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution

Type 'help;' or '¥h' for help. Type '¥c' to clear the buffer.

mysql> exit    ←終了

MySQLのセキュリティ設定

 MySQLのデフォルト設定では、データベースのrootパスワードが設定されていない。「mysql_secure_installation」コマンドを実行してパスワードを設定するとともに、セキュアな設定に変更しておこう(リスト8)。

リスト8 「mysql_secure_installation」コマンドによる初期設定

$ mysql_secure_installation
 :
 :
Enter current password for root (enter for none):   ←Enterキーを押す
OK, successfully used password, moving on...
 :
 :
Change the root password? [Y/n]   ←Enterキーを押す
New password:   ←設定するrootパスワードを入力
Re-enter new password:   ←同じパスワードを再入力
Password updated successfully!
Reloading privilege tables..
 ... Success!
 :
 :
(以降の選択肢ではすべてEnterのみを入力する)
 :
 :
Remove anonymous users? [Y/n]
 ... Success!
 :
 :
Disallow root login remotely? [Y/n]
 ... Success!
 :
 :
Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 :
 :
Reload privilege tables now? [Y/n]
 ... Success!
 :
 :
Thanks for using MySQL!