■概要

データベースサーバーは、サーバー上のデータベースをクライアントから操作できるようにするためのサーバー。
ここでは、リレーショナル型データベースのデータベースサーバーであるMySQLを使用する。


■MySQLインストール

[root@centos ~]# yum -y install mysql-server ← mysql-serverインストール

■MySQL設定

[root@centos ~]# vi /etc/my.cnf ← MySQL設定ファイル編集
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
default-character-set = utf8 ← 追加(MySQLサーバーの文字コードをUTF-8にする)

以下を追加(MySQLクライアントの文字コードをUTF-8にする)
[mysql]
default-character-set = utf8

■MySQL起動

[root@centos ~]# /etc/rc.d/init.d/mysqld start ← MySQL起動
MySQL データベースを初期化中:  Installing all prepared tables
Fill help tables

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 centos.wakuwakustation.com password 'new-password'
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 the benchmarks in the 'sql-bench' directory:
cd sql-bench ; perl run-all-tests

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
MySQL を起動中:                                            [  OK  ]

[root@centos ~]# chkconfig mysqld on ← MySQL自動起動設定

[root@centos ~]# chkconfig --list mysqld ← MySQL自動起動設定確認
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off ← ランレベル2~5のonを確認

■MySQLデータベース初期設定

(1)rootユーザへのパスワード設定
MySQLの管理ユーザであるrootユーザ(システムのrootユーザとは別)にはデフォルトではパスワードが設定されていないため、パスワードを設定する
[root@centos ~]# mysql -u root ← MySQLサーバーへrootユーザでログイン
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10 to server version: 4.1.12

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

mysql> select user,host,password from mysql.user; ← 登録済ユーザ、パスワード確認
+------+----------------------+----------+
| user | host                 | password |
+------+----------------------+----------+
| root | localhost            |          | ← ホスト名がlocalhostのrootユーザにパスワードが設定されていない
| root | centos.wakuwakustation.com |          | ← ホスト名が自ホストのrootユーザにパスワードが設定されていない
| root | 127.0.0.1            |          | ← ホスト名が127.0.0.1のrootユーザにパスワードが設定されていない
+------+----------------------+----------+
3 rows in set (0.00 sec)

mysql> set password for root@localhost=password('rootパスワード'); ← ホスト名がlocalhostのrootユーザにパスワード設定
Query OK, 0 rows affected (0.00 sec)

mysql> set password for root@'centos.wakuwakustation.com'=password('rootパスワード'); ← ホスト名が自ホストのrootユーザにパスワード設定
Query OK, 0 rows affected (0.00 sec)

mysql> set password for root@127.0.0.1=password('rootパスワード'); ← ホスト名が127.0.0.1のrootユーザにパスワード設定
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user; ← 登録済ユーザ、パスワード確認
+------+----------------------+------------------+
| user | host                 | password         |
+------+----------------------+------------------+
| root | localhost            | **************** | ← ホスト名がlocalhostのrootユーザにパスワードが設定された
| root | centos.wakuwakustation.com | **************** | ← ホスト名が自ホストのrootユーザにパスワードが設定された
| root | 127.0.0.1            | **************** | ← ホスト名が127.0.0.1のrootユーザにパスワードが設定された
+------+----------------------+------------------+
3 rows in set (0.00 sec)

mysql> exit ← MySQLサーバーからログアウト
Bye

[root@centos ~]# mysql -u root -h localhost
 ← ホスト名がlocalhostのrootユーザでパスワードなしでMySQLサーバーへログインできないことを確認
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@centos ~]# mysql -u root -h 'centos.wakuwakustation.com'
 ← ホスト名が自ホストのrootユーザでパスワードなしでMySQLサーバーへログインできないことを確認
ERROR 1045 (28000): Access denied for user 'root'@'centos.wakuwakustation.com' (using password: NO)

[root@centos ~]# mysql -u root -h 127.0.0.1
 ← ホスト名が127.0.0.1のrootユーザでパスワードなしでMySQLサーバーへログインできないことを確認
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@centos ~]# mysql -u root -h localhost -p ← MySQLへrootでログイン
Enter password:  ← MySQLのrootパスワード応答
ホスト名がlocalhostのrootユーザでパスワードありでMySQLサーバーへログインできることを確認
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18 to server version: 4.1.12

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

mysql> exit ← MySQLサーバーからログアウト
Bye

[root@centos ~]# mysql -u root -h 'centos.wakuwakustation.com' -p
Enter password:  ← MySQLのrootパスワード応答
ホスト名が自ホストのrootユーザでパスワードありでMySQLサーバーへログインできることを確認
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19 to server version: 4.1.12

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

mysql> exit ← MySQLサーバーからログアウト
Bye

[root@centos ~]# mysql -u root -h 127.0.0.1 -p ← MySQLへrootでログイン
Enter password:  ← MySQLのrootパスワード応答
ホスト名が127.0.0.1のrootユーザでパスワードありでMySQLサーバーへログインできることを確認
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18 to server version: 4.1.12

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

mysql> exit ← MySQLサーバーからログアウト
Bye

※ホスト名を指定してログインできない場合の対処
ホスト名を指定してログインできない原因は、サーバーが自身のホスト名を名前解決(ホスト名→IPアドレス)できないことが原因。
サーバーが自身のホスト名を名前解決できるように、サーバーの/etc/hostsにホスト名とIPアドレスの対応を追加する。
[root@centos ~]# grep `hostname` /etc/hosts ← /etc/hostsにホスト名が定義してあるか確認
なにも表示されない=ホスト名が定義されていないことを確認

[root@centos ~]# echo 127.0.0.1 `hostname` >> /etc/hosts ← /etc/hostsにホスト名の定義を追加

[root@centos ~]# grep `hostname` /etc/hosts ← /etc/hostsにホスト名が定義してあるか確認
127.0.0.1 centos.wakuwakustation.com ← ホスト名定義追加を確認

(2)testデータベース削除
MySQLにはデフォルトでtestという空のデータベースが登録されているが、不要のため、このデータベースを削除する
[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン
Enter password:  ← MySQLのrootパスワード応答
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7 to server version: 4.1.12

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


mysql> show databases; ← 登録データベース確認
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
| test               | 
+--------------------+
3 rows in set (0.00 sec)

mysql> drop database test; ← testデータベース削除
Query OK, 0 rows affected (0.00 sec)

mysql> show databases; ← 登録データベース確認
+--------------------+
| Database           |
+--------------------+
| information_schema | 
| mysql              | 
+--------------------+
2 rows in set (0.00 sec)

mysql> exit ← ログアウト
Bye
※mysqlデータベースはシステム管理用のデータベースなので削除しないこと

■MySQL確認

[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン
Enter password:  ← MySQLのrootパスワード応答
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19 to server version: 4.1.12

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


mysql> grant all privileges on test.* to centos@localhost identified by 'centospass';
 ← testデータベースへの全てのアクセス権限を持った、新規ユーザcentosを登録
Query OK, 0 rows affected (0.00 sec)

mysql> select user from mysql.user where user='centos'; ← centosユーザ登録確認
+--------+
| user   |
+--------+
| centos |
+--------+
1 row in set (0.00 sec)

mysql> exit ← ログアウト
Bye

[root@centos ~]# mysql -u centos -pcentospass ← centosユーザでMySQLサーバーへログイン
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18 to server version: 4.1.12

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

mysql> create database test; ← testデータベース作成
Query OK, 1 row affected (0.00 sec)

mysql> show databases; ← データベース作成確認
+----------+
| Database |
+----------+
| mysql    |
| test     |
+----------+
2 rows in set (0.01 sec)

mysql> use test ← testデータベースへ接続
Database changed

mysql> create table test(num int, name varchar(50)); ← testテーブル作成
Query OK, 0 rows affected (0.01 sec)

mysql> show tables; ← テーブル作成確認
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.00 sec)

mysql> insert into test values(1,'山田太郎'); ← testテーブルへデータ登録
Query OK, 1 row affected (0.00 sec)

mysql> select * from test; ← データ登録確認
+------+----------+
| num  | name     |
+------+----------+
|    1 | 山田太郎 |
+------+----------+
1 row in set (0.00 sec)

mysql> update test set name='山田次郎'; ← testテーブル内データ更新
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test; ← データ更新確認
+------+----------+
| num  | name     |
+------+----------+
|    1 | 山田次郎 |
+------+----------+
1 row in set (0.01 sec)

mysql> delete from test where num=1; ← testテーブル内データ削除
Query OK, 1 row affected (0.03 sec)

mysql> select * from test; ← データ削除確認
Empty set (0.00 sec)

mysql> drop table test; ← testテーブル削除
Query OK, 0 rows affected (0.00 sec)

mysql> show tables; ← テーブル削除確認
Empty set (0.00 sec)

mysql> drop database test; ← データベースtest削除
Query OK, 0 rows affected (0.00 sec)

mysql> show databases; ← データベース削除確認
+----------+
| Database |
+----------+
| mysql    |
+----------+
1 row in set (0.00 sec)

mysql> exit ← ログアウト
Bye

[root@centos ~]# mysql -u root -p ← MySQLへrootでログイン
Enter password:  ← MySQLのrootパスワード応答
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21 to server version: 4.1.12

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

mysql> revoke all privileges on *.* from centos@localhost; ← centosユーザから全てのデータベースへのアクセス権限を剥奪
Query OK, 0 rows affected (0.00 sec)

mysql> delete from mysql.user where user='centos' and host='localhost'; ← centosユーザ削除
Query OK, 1 row affected (0.01 sec)

mysql> select user from mysql.user where user='centos'; ← centosユーザ削除確認
Empty set (0.00 sec)

mysql> flush privileges; ← centosユーザの削除をMySQLサーバーへ反映
Query OK, 0 rows affected (0.01 sec)

mysql> exit ← ログアウト
Bye