MySQL の導入

LAMP アプリケーションなどで非常に人気のある MySQL データベースの導入手順を説明します。
LAMP アプリケーションとは Linux、Apache、MySQL、PHP で構成されるアプリケーションの事で、 それぞれの頭文字をとって LAMP アプリケーションと呼ばれています。

本サイトでは、IPTables log analyzer や BASE などで MySQL を使用するため導入します。

インストール

1. MySQL をインストールします。

# yum -y install mysql-server
Loading "fastestmirror" plugin
Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
Loading mirror speeds from cached hostfile
Reading repository metadata in from local files
Excluding Packages in global exclude list
Finished
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for mysql-server to pack into transaction set.
mysql-server-5.0.27-1.fc6 100% |=========================|  34 kB    00:00
---> Package mysql-server.i386 0:5.0.27-1.fc6 set to be updated
--> Running transaction check
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for perl-DBD-MySQL to pack into transaction set.
perl-DBD-MySQL-3.0007-1.f 100% |=========================| 8.5 kB    00:00
---> Package perl-DBD-MySQL.i386 0:3.0007-1.fc6 set to be updated
--> Running transaction check

Dependencies Resolved

=============================================================================
 Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
 mysql-server            i386       5.0.27-1.fc6     updates            10 M
Installing for dependencies:
 perl-DBD-MySQL          i386       3.0007-1.fc6     core              147 k

Transaction Summary
=============================================================================
Install      2 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total download size: 10 M
Downloading Packages:
(1/2): perl-DBD-MySQL-3.0 100% |=========================| 147 kB    00:00
(2/2): mysql-server-5.0.2 100% |=========================|  10 MB    00:05
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: perl-DBD-MySQL               ######################### [1/2]
  Installing: mysql-server                 ######################### [2/2]

Installed: mysql-server.i386 0:5.0.27-1.fc6
Dependency Installed: perl-DBD-MySQL.i386 0:3.0007-1.fc6
Complete!

2. MySQL 設定ファイルを編集します。

# vi /etc/my.cnf
[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

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

3. MySQL を起動します。

# service mysqld start
Initializing MySQL database:  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 fc6.orangesignal.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  ]
Starting MySQL:                                            [  OK  ]

4. MySQL の自動起動を有効にします。

# chkconfig mysqld on

設定

1. MySQL にログインして管理者ユーザのパスワード設定や不要なデータベースの削除などを行います。
※SQL 構文の大文字/小文字は区別されません。ここでは SQL キーワードを大文字で表記しています。

# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.0.27

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

mysql>

ユーザとパスワードの一覧を確認します。
mysql> SELECT host, user, password FROM mysql.user;
+-------------------------+------+----------+
| host                    | user | password |
+-------------------------+------+----------+
| localhost               | root |          |
| server.orangesignal.com | root |          |
| server.orangesignal.com |      |          |
| localhost               |      |          |
+-------------------------+------+----------+
4 rows in set (0.00 sec)

root@localhost ユーザのパスワードを設定します。
mysql> SET PASSWORD FOR root@localhost=PASSWORD('パスワード');
Query OK, 0 rows affected (0.00 sec)

root@server.orangesignal.com ユーザのパスワードを設定します。
mysql> SET PASSWORD FOR root@server.orangesignal.com=PASSWORD('パスワード');
Query OK, 0 rows affected (0.00 sec)

ユーザ名のない匿名ユーザを削除します。
mysql> DELETE FROM mysql.user WHERE user='';
Query OK, 2 rows affected (0.00 sec)

これまでのユーザ操作を確認します。
mysql> SELECT host, user, password FROM mysql.user;
+-------------------------+------+------------------+
| host                    | user | password         |
+-------------------------+------+------------------+
| localhost               | root | **************** |
| server.orangesignal.com | root | **************** |
+-------------------------+------+------------------+
2 rows in set (0.00 sec)

データベースの一覧を確認します。
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

test データベースは不要なので削除します。
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> exit
Bye

2. 設定したパスワードでログインできることを確認します。

パスワードなしで root@localhost ユーザがログインできないことを確認します。
# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

パスワードなしで root@server.orangesignal.com ユーザがログインできないことを確認します。
# mysql -u root -h server.orangesignal.com
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

パスワードを指定して root@localhost ユーザがログインできることを確認します。
# mysql -u root -p
Enter password: パスワード
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10 to server version: 5.0.27

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

ログアウトします。
mysql> exit
Bye

パスワードを指定して root@server.orangesignal.com ユーザがログインできることを確認します。
# mysql -u root -h server.orangesignal.com -p
Enter password: パスワード
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11 to server version: 5.0.27

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

ログアウトします。
mysql> exit
Bye

Google