mysqlサーバ構築

ウェブサービスをやろうとした場合、自作して小さい規模ではじめるならファイルに保存するなど、
データベースは必要ないかもしれませんが、たいていのオープンソースのツールでは
データベースを必要とします。

DBにはmysql,postgres,sqlite,sqlserver,oracleなどいくつか種類がありますが、
多くのCMSサイト構築ツールがDBとして採用しているmysqlを使って、
DBサーバを構築します。

mysql-serverインストール

yumを使ってmysql-serverをインストールします。

# yum -y install mysql-server
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package mysql-server.i386 0:5.0.45-6.fc8 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
 Package                 Arch       Version          Repository        Size 
=============================================================================
Installing:
 mysql-server            i386       5.0.45-6.fc8     updates           9.8 M

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

Total download size: 9.8 M
Downloading Packages:
(1/1): mysql-server-5.0.4 100% |=========================| 9.8 MB    00:07     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: mysql-server                 ######################### [1/1] 

Installed: mysql-server.i386 0:5.0.45-6.fc8
Complete*

mysqlの設定

mysqlの設定は、/etc/my.cnfで行います。
my.cnf内の[mysqld]と書かれている部分は、mysqlサーバの設定、[mysql]と書かれている部分は、mysqlクライアントの設定になります。
ここでは、[mysql]と[mysqld]の文字コードをutf8に指定します。
それぞれの設定に

default-character-set=utf8

を追加します。
以下のような形。

[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
default-character-set=utf8
[mysql]
default-character-set=utf8

mysql起動

/etc/rc.d/init.d/mysqld start
としてmysqlデーモンを起動します。

# /etc/rc.d/init.d/mysqld start
MySQL データベースを初期化中:  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 www.longearth.net 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
                                                           [  OK  ]
MySQL を起動中:                                            [  OK  ]

mysqlの自動起動登録

mysqlを電源ON時に自動起動させるために、軌道登録をします。
chkconfigでランレベルを確認。

# chkconfig --list mysqld
mysqld          0:off   1:off   2:off   3:off   4:off   5:off   6:off

すべてのランレベルでoffとなっていたら、2-5のレベルで起動するよう登録する。

# chkconfig mysqld on

登録されたか確認する。

# chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

rootの設定

defaultでは、mysqlのrootユーザはパスワードが設定されていません。
危険なので、パスワードの設定をします。
パスワードがないので、以下のコマンドでログインできます。

# mysql -u root

mysqlのユーザ情報は、mysqlスキーマのuserテーブルで管理されています。
以下のSQLを発行して内容を確認してみる。

mysql> select user , host , password from mysql.user;
+------+-------------------+----------+
| user | host              | password |
+------+-------------------+----------+
| root | localhost         |          | 
| root | www.longearth.net |          | 
| root | 127.0.0.1         |          | 
+------+-------------------+----------+
3 rows in set (0.00 sec)

ユーザはroot一人だけですが、hostがlocalhost,www.longearth.net,127.0.0.1の3つが設定されています。
hostとは、mysqlへ接続する時のホスト名になります。
mysqlでは、このユーザ、ホストごとに権限を設定することができます。
例えば、同じrootユーザでもローカルからの接続に対しては、全ての権限を与えるが、
リモート(別マシン)からの接続の場合は、select権限しか与えないといった具合です。

とりあえず、3ホスト分パスワードを登録します。
以下では、passwordという文字列がpasswordとして登録されます。
root@localhostという部分が、rootユーザのlocalhostからの接続に対して、という意味です。

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

mysql> set password for root@www.longearth.net=password('password');
Query OK, 0 rows affected (0.00 sec)

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

確認してみる

設定されたか確認してみます。

mysql> select user , host , password from mysql.user;
+------+-------------------+------------------+
| user | host              | password         |
+------+-------------------+------------------+
| root | localhost         | 7259fa7604f34644 | 
| root | www.longearth.net | 7259fa7604f34644 | 
| root | 127.0.0.1         | 7259fa7604f34644 | 
+------+-------------------+------------------+
3 rows in set (0.00 sec)

暗号化されていますが、パスワードが設定されていることが分かります。

ユーザ追加してみる

mtスキーマの全テーブルに対して、全権限を持ったmtユーザ(localhostから接続)で、
mtというパスワードを設定して、ユーザの追加を行う場合、以下のようなSQLを発行します。

mysql> grant all privileges on mt.* to mt@localhost identified by 'mt' with grant option;
Query OK, 0 rows affected (0.00 sec)

grant all privilegesで全ての権限を指定しています。select,insert権限のみを与える場合は、grant select,insertとなります。
on mt.*でmtスキーマの全テーブルを。testテーブルのみに権限を与える場合は、on mt.testとなります。
mt@localhostは上述の通りです。
identified by 'mt'でパスワードにmtが設定されます。
with grant optionでは、このgrant構文を使う許可が与えられます。

ユーザを削除してみる

grant許可ユーザなんていらないので、早々に削除します。
削除時は通常のdelete文と同様です。

mysql> delete from mysql.user where user='mt';
Query OK, 1 row affected (0.00 sec)

以上。



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2008-04-05 (土) 01:13:40 (708d)