AmazonLinux で メールサーバーを作る(その4)
今回は MySQL を設定します。
MySQL(含むserver)のインストールは 第一回 で完了している前提です。
・デーモン起動の設定
Apacheと同じく mysqld も自動起動設定されていないようです。
以下のようにして自動起動設定を行います。
ここでは手動で起動させます。
「support-files/mysql.server を所定の位置にコピーせよ」…というのは、どうやら /etc/init.d/mysql のバッチのことらしいです。これは service コマンド経由で呼び出すものと等価なハズで…起動は成功しているということは コピーそのものは(yumコマンドが?)完了しているということみたいです。
…ということは、残りのパスワード設定(と、不要なテーブルやアカウントの削除)を実施する必要があるようですが…。恐らく、 /usr/bin/mysql_secure_installation を実行すれば代行してくれるものと思われます。
では、さっそく実行してみましょう。
念の為、この状態で mysql をstop -> start させてみます。
・DBアカウントの作成
次に、DB利用者(PHPプログラマと、そのプログラム)に開放するアカウントとDB領域を作成します。
作成するのはMLサービスらしいので、アカウント名、DB領域名ともに mlservice としておきます。
アカウント設定が出来たら、利用可能になっているかどうか確認しておきます。
・PHPからのMySQL接続
MySQLはPHPから利用されることが分かっているので、Apache mod_php と php-cli の両方から接続可能になっているかどうか、確認しておきます。
Apache の document root に以下のようなファイルを作成します。ここでは pdo.php とか名前を付けておきます。
「もしかして、何か足りない? 」と思いつつ yum list installed | grep -i pdo を実行してみると、やはりそれらしいモジュールはありません。きっと、pdo と名前が付くものがあるのでしょう。
yum search all php mysql pdo として検索してみると、以下のようなものが見つかります。
sudo yum install php-pdo.i686 php-mysqlnd.i686 としてインストールします。
インストール後に 先ほどの pdo.php を再実行すると…。
続けて Apache mod_php 側で動作するかどうか確認してみると…。
「ああ」と思い直して、Apache を再起動して再度挑戦すると、ウマク表示されました。
PDO を介して MySQL にもアクセスできるようになったので、これで php->MySQL の接続設定は完了です。
今回は MySQL を設定します。
MySQL(含むserver)のインストールは 第一回 で完了している前提です。
・デーモン起動の設定
Apacheと同じく mysqld も自動起動設定されていないようです。
以下のようにして自動起動設定を行います。
chkconfig で設定しても、次回のOS起動時に自動起動設定されるだけなので、今すぐmysqlを起動させたいのであれば サーバーOSを再起動する必要があります。あるいは mysqld 自体を手動起動しても良いです。$ chkconfig | grep mysql
mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
$ sudo chkconfig mysqld on
$ chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
ここでは手動で起動させます。
起動は成功するようですが、初期設定とパスワード設定を行え…と警告されていますね。$ sudo service mysqld status
mysqld is stopped
$ sudo service mysqld start
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
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 ip-10-150-161-211 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.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
[ OK ]
Starting mysqld: [ OK ]
$
「support-files/mysql.server を所定の位置にコピーせよ」…というのは、どうやら /etc/init.d/mysql のバッチのことらしいです。これは service コマンド経由で呼び出すものと等価なハズで…起動は成功しているということは コピーそのものは(yumコマンドが?)完了しているということみたいです。
…ということは、残りのパスワード設定(と、不要なテーブルやアカウントの削除)を実施する必要があるようですが…。恐らく、 /usr/bin/mysql_secure_installation を実行すれば代行してくれるものと思われます。
では、さっそく実行してみましょう。
いくつか質問されますが、全部 Yes ですね…。root パスワードは、インストール直後はカラッポらしいので、適切なものを再設定させます。$ sudo /usr/bin/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.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
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...
... 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? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
$
念の為、この状態で mysql をstop -> start させてみます。
特に警告は表示されないのでOKでしょう。$ sudo service mysqld stop
Stopping mysqld: [ OK ]
$ sudo service mysqld start
Starting mysqld: [ OK ]
・DBアカウントの作成
次に、DB利用者(PHPプログラマと、そのプログラム)に開放するアカウントとDB領域を作成します。
作成するのはMLサービスらしいので、アカウント名、DB領域名ともに mlservice としておきます。
DB接続用アカウントは、権限別に「運用/管理用」「WEB用」「crontabバッチ用」と分離したいところですが、初心者向けのものなので1つだけにしておきます。実サービスで使われるDBではないので…(^^;mysql> create database mlservice;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on mlservice.*
to mlservice@localhost
identified by 'パスワード';
アカウント設定が出来たら、利用可能になっているかどうか確認しておきます。
とりあえず動作はするので、これでOKと言うことにしておきます。$ mysql -u mlservice -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mlservice |
+--------------------+
2 rows in set (0.00 sec)
mysql> use mlservice
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table test ( x INT );
Query OK, 0 rows affected (0.07 sec)
mysql> show tables;
+---------------------+
| Tables_in_mlservice |
+---------------------+
| test |
+---------------------+
1 row in set (0.00 sec)
mysql> drop table test;
Query OK, 0 rows affected (0.02 sec)
mysql> exit
Bye
・PHPからのMySQL接続
MySQLはPHPから利用されることが分かっているので、Apache mod_php と php-cli の両方から接続可能になっているかどうか、確認しておきます。
Apache の document root に以下のようなファイルを作成します。ここでは pdo.php とか名前を付けておきます。
作成したら、php-cli で実行してみます。しかし「PHP Fatal error: Class 'PDO' not found in /var/www/~/pdo.php on line 7」とかエラーが出て、動作しません。なぜだ…(x_x;<?
$DBhost = 'localhost';
$DBregion = 'mlservice';
$DBuser = 'mlservice';
$DBpassword = 'パスワード';
$pdo = new PDO( "mysql:host=${DBhost};dbname=${DBregion}", $DBuser, $DBpassword );
$statement = $pdo->query( "show grants' );
$rows = $statement->fetchAll( PDO::FETCH_ASSOC );
var_dump( $rows );
// END of FILE //
「もしかして、何か足りない? 」と思いつつ yum list installed | grep -i pdo を実行してみると、やはりそれらしいモジュールはありません。きっと、pdo と名前が付くものがあるのでしょう。
yum search all php mysql pdo として検索してみると、以下のようなものが見つかります。
php-mysql と php-mysqlnd と、どちらも DB接続ドライバーで nd の方が NativeDriver という名前で より高速なものらしいので、ソチラを使うことにします。php-pdo.i686 : A database access abstraction module for PHP applications
php-mysql.i686 : A module for PHP applications that use MySQL databases
php-mysqlnd.i686 : A module for PHP applications that use MySQL databases
sudo yum install php-pdo.i686 php-mysqlnd.i686 としてインストールします。
インストール後に 先ほどの pdo.php を再実行すると…。
それらしい結果が表示されました。$ php -f pdo.php
array(2) {
[0]=>
array(1) {
["Grants for mlservice@localhost"]=>
string(112) "GRANT USAGE ON *.* TO 'mlservice'@'localhost' IDENTIFIED BY PASSWORD 'パスワードハッシュ'"
}
[1]=>
array(1) {
["Grants for mlservice@localhost"]=>
string(64) "GRANT ALL PRIVILEGES ON `mlservice`.* TO 'mlservice'@'localhost'"
}
}
続けて Apache mod_php 側で動作するかどうか確認してみると…。
先ほどと同じエラーが発生しています。なぜ???$curl 'http://~.no-ip.biz/pdo.php'
Fatal error: Class 'PDO' not found in /var/www/~/pdo.php on line 7
「ああ」と思い直して、Apache を再起動して再度挑戦すると、ウマク表示されました。
yum コマンドは、起動中のデーモンに依存関係のあるモジュールをインストールしても、そのデーモンを再起動したりはしてくれないのですね…(^^;$ sudo apachectl -k restart
$ curl 'http://~.no-ip.biz/pdo.php'
array(2) {
[0]=>
array(1) {
["Grants for mlservice@localhost"]=>
string(112) "GRANT USAGE ON *.* TO 'mlservice'@'localhost' IDENTIFIED BY PASSWORD 'パスワードハッシュ'"
}
[1]=>
array(1) {
["Grants for mlservice@localhost"]=>
string(64) "GRANT ALL PRIVILEGES ON `mlservice`.* TO 'mlservice'@'localhost'"
}
}
PDO を介して MySQL にもアクセスできるようになったので、これで php->MySQL の接続設定は完了です。