1 MySQLとは何か?
1.1 MySQLについて
MySQLはオープンソースのSQLデータベースです。
高速性と堅牢性を追求したマルチユーザ・マルチスレッドのDBです。
Sourceforgeなどのオープンソースの世界では、ほとんどのシステムがDBとしてMySQLを採用しています。
2 MySQLをインストールする
2.1 ユーザーの追加
セキュリティのためにMySQLを動作させるユーザーを作成します。
次の手順で、ユーザー"mysql"を作成します。
- su でrootになる
[atsushifx@agartha atsushifx]$ su
Password: ****
[root@agartha atsushifx]# - グループ"mysql"を追加する
[root@agartha atsushifx]# /usr/sbin/groupadd -g 301 mysql - ユーザー"mysql"を追加する
[root@agartha atsushifx]# /usr/sbin/useradd -u 301 -g mysql mysql
以上で、ユーザーの作成は終了です。
2.2 MySQLのインストール
VineLinuxでのインストールのため、aptを使ってインストールします。
インストールの手順は次のようになります。
- aptを使ってパッケージをダウンロードし、インストールします。
[root@agartha atsushifx]# apt-get install MySQL-server MySQL-client MySQL-shared パッケージリストを読みこんでいます... 完了 依存情報ツリーを作成しています... 完了 以下のパッケージが新たにインストールされます: MySQL-client MySQL-server MySQL-shared アップグレード: 0 個, 新規インストール: 3 個, 削除: 0 個, 保留: 1 個 2942kB/13.6MB のアーカイブを取得する必要があります。 展開後に 32.7MB のディスク容量が追加消費されます。 取得:1 http://updates.vinelinux.org 3.1/i386/plus MySQL-shared 4.0.23-0vl0 [276kB] 取得:2 http://updates.vinelinux.org 3.1/i386/plus MySQL-client 4.0.23-0vl0 [2666kB] 2942kB を 6s 秒で取得しました (462kB/s) 変更を適用しています... Preparing... ########################################### [100%] 1:MySQL-server ########################################### [ 33%] Preparing db table Preparing host table Preparing user table Preparing func table Preparing tables_priv table Preparing columns_priv table Installing all prepared tables 050729 16:43:54 /usr/sbin/mysqld: Shutdown Complete 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 agartha password 'new-password' See the manual for more instructions. 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 https://order.mysql.com 2:MySQL-shared ########################################### [ 66%] 3:MySQL-client ########################################### [ 100%]
以上でMySQLのインストールは終了です。
2.3 管理用ユーザーを設定する
インストール時の状態だとDBはユーザrootかつパスワードなしで入れます。
これだとセキュリティに問題があるので、次のようにしてユーザーとパスワードを設定します。
- ユーザー"root"でmysqlにログインする
root@agartha atsushifx]# mysql -u root mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 to server version: 4.0.23a-standard Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
- 現在のユーザーを全て削除する
mysql> truncate user; - 管理用ユーザー"mysql"を追加する。
mysql> grant all on *.* to mysql identified by '***' with grant option; - mysqlからログアウトする
mysql> \q
Bye - 設定をシステムに反映させる
[root@agartha atsushifx]# mysqladmin -u root reload
これで、管理用ユーザとしてmysqlが設定されます。また、上記で指定したパスワードを使わないとmysqlにログインできなくなります。
3. MySQLを設定する
3.1 MySQLで日本語を使えるようにする
MySQLの動作の設定をします。あわせて、デフォルトのキャラクターセット"latin1"では、日本語を正しく扱えません。
そのため、キャラクターセットを設定して日本語を扱えるようにします。
設定ファイルは次のようになります。
- /etc/my.cnf
[client] port = 3306 socket = /var/lib/mysql/mysql.sock default-character-set = ujis [mysqld] user = mysql socket = /var/lib/mysql/mysql.sock port = 3306 default-character-set = ujis skip-locking key_buffer = 16K max_allowed_packet = 1M thread_stack = 64K table_cache = 4 sort_buffer_size = 64K net_buffer_length = 2K . . [mysql] default-character-set = ujis
mysqlデーモンを再起動させて設定を反映させます。
- mysqlを再起動します
[root@agartha etc]# /etc/init.d/mysql restart
Killing mysqld with pid 12114
Wait for mysqld to exit done
以上で設定は終了です。mysqlのステータスを見て、charsetがujisになっていたら日本語が扱えるようになっています。
3.2 メッセージを日本語化する
MySQL 4.0.23ではメッセージを日本語化することができます。
次の手順で、メッセージを日本語化します。
- /etc/my.cnfに言語設定を追加する
- /etc/my.cnf
. . # The MySQL server [mysqld] language = /usr/share/mysql/japanese
- mysqlを再起動します
[root@agartha etc]# /etc/init.d/mysql restart
Killing mysqld with pid 13225
Wait for mysqld to exit done
以上で、日本語化は完了です。
mysqlでエラーがあるコマンドを実行すると、次のようになります。
mysql> select * from user;
ERROR 1046: データベースが選択されていません.