» Linux ディストリビューション » CentOS » MySQL

データベースサーバーを構築する(MySQL)

 データベースサーバーを構築するときによく使用される MySQL パッケージは、オープンソースで開発されたデータベースですが他のオープンソース・データベースと比較すると高速性に定評があり、更新処理より問い合せ(照会)処理の使用頻度が高いアプリケーションに向いています。

パッケージのインストール

 データベースサーバーパッケージは、yum コマンドを使用してインストールします。

[root@web ~]# yum install mysql-server

[ CentOS 6 / CentOS 5 ]

パッケージインストール後の設定

 MySQL に関する設定を変更します。

[root@web ~]# vi /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
# データベースサーバーの文字コード指定(mysql-server-5.1.52 の場合)
character-set-server=utf8
# データベースサーバーの文字コード指定(mysql-server-5.0.77-4 の場合)
default-character-set=utf8

[ CentOS 6 / CentOS 5 ]

データベースサーバーの起動

 パッケージをインストールした段階では、セキュリティを考慮しているため自動起動されません。 システムの再起動後に データベースサーバーを自動起動するための設定を行います。

 なお、データベースサーバーを起動する前に、手動でシステムデータベースの作成を行います。

システムデータベースを作成する

[root@web ~]# mysql_install_db

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 web.i2kt.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 mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

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

データベースサーバーを起動する

[root@web ~]# /etc/rc.d/init.d/mysqld start

MySQL を起動中:                                            [  OK  ]

データベースサーバーの自動起動を設定する

[root@web ~]# chkconfig mysqld on

[root@web ~]# chkconfig --list mysqld

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

[ CentOS 6 / CentOS 5 ]

データベースサーバーの操作

データベースサーバーに接続する(アカウントのパスワードが未設定の場合)

[root@web ~]# mysql -u root

データベースサーバーに接続する(アカウントのパスワードが設定済みの場合)

[root@web ~]# mysql -u root -p

Welcome to the MySQL monitor.  Commands end with ; or ¥g.
Your MySQL connection id is 4
Server version: 5.1.52 Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement.

mysql>

root アカウントのパスワードを設定する

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

匿名アカウントを削除する

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> delete from user where user='';
Query OK, 2 rows affected (0.00 sec)

mysql> select host, user, password from user;
+-----------------+------+-------------------------------------------+
| host            | user | password                                  |
+-----------------+------+-------------------------------------------+
| localhost       | root | *2786F5355041048FF123A2B1FC8977E80A246BED |
| web.i2kt.com    | root |                                           |
| 127.0.0.1       | root |                                           |
+-----------------+------+-------------------------------------------+
3 rows in set (0.00 sec)

[ CentOS 6 ]