Debianとmysql5.0でOSSDB標準教科書を試す

※実際には111/24のネタです。

Debianとmysql5.0でオープンソースデータベース標準教科書を試す

■Debian lenny amd64版とMySQL5.0編

 先のPostgresql8.3のmysql版
 http://ameblo.jp/labunix/entry-11087407247.html

$ apt-cache search mysql-server-5.0
mysql-server-5.0 - MySQL データベースサーババイナリ
mysql-server - MySQL データベースサーバ (最新版に依存するメタパッケージ)
mysql-server-5.1 - MySQL database server binaries and system database setup

$ apt-cache search mysql-client-5.0
mysql-client-5.0 - MySQL データベースクライアントバイナリ
mysql-client - MySQL データベースクライアント (最新版に依存するメタパッケージ)

■インストール
 ※rootパスワードを設定する

$ sudo apt-get install mysql-common mysql-server mysql-client

$ mysql --version
mysql Ver 14.12 Distrib 5.0.51a, for debian-linux-gnu (x86_64) using readline 5.2

■マニュアルのチェック
 ※メタコマンドも表示されます。

$ mysql --help | grep help
-?, --help Display this help and exit.
-I, --help Synonym for -?
internal commands; see mysql> help . When enabled, the
etc. See interactive help (\h) also. This option does not
--no-pager Disable pager and print to stdout. See interactive help
--tee=name Append everything into outfile. See interactive help (\h)
--no-tee Disable outfile. See interactive help (\h) also. WARNING:

$ mysql --help | grep "\-\-user"
-u, --user=name User for login if not current user.

$ mysql --help | grep -A 4 "\-\-password"
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty. WARNING: This is
insecure as the password is visible for anyone through
/proc for a short time.

■サービスの起動確認

$ sudo /etc/init.d/mysql status
/usr/bin/mysqladmin Ver 8.41 Distrib 5.0.51a, for debian-linux-gnu on x86_64
Copyright (C) 2000-2006 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version 5.0.51a-24+lenny5
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 6 min 44 sec

Threads: 1 Questions: 219 Slow queries: 0 Opens: 148 Flush tables: 2 Open tables: 17 Queries per second avg: 0.542.

■ログインしてみる

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.0.51a-24+lenny5 (Debian)

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

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
+--------------------+
2 rows in set (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> select User from user;
+------------------+
| User |
+------------------+
| root |
| debian-sys-maint |
| root |
| root |
+------------------+
4 rows in set (0.00 sec)

■ファイルから読み込むには。。。

$ man mysql 2> /dev/null| grep "shell>"
shell> mysql db_name
shell> mysql --user=user_name --password=your_password db_name
shell> mysql db_name < script.sql > output.tab
shell> mysql --xml -uroot -e "SHOW VARIABLES LIKE 'version%'"
shell> ln -s /dev/null $HOME/.mysql_history
shell> man less
shell> export MYSQL_PS1="(\u@\h) [\d]> "
shell> mysql --prompt="(\u@\h) [\d]> "
shell> mysql db_name
shell> mysql db_name < text_file
shell> mysql < text_file
shell> mysql --safe-updates --select_limit=500 --max_join_size=10000

■先にdbを作成する

mysql> create database dummydb;
Query OK, 1 row affected (0.00 sec)
mysql> use dummydb
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> \q
Bye

■char型をvarchar型に変更

$ sed s/"char"/"varchar"/g cdb_dummy.sql > mysql_dummy.sql

★dummyテーブル内で日本語カラム名を使う場合、バッククオート「`」を使う必要があります。

$ cat mysql_dummy.sql
create table dummy(
`名前` varchar(32),
`ふりがな` varchar(32),
`アドレス` varchar(32),
`性別` varchar(16),
`年齢` varchar(16),
`誕生日` varchar(16),
`婚姻` varchar(16),
`血液型` varchar(16),
`都道府県` varchar(16),
`電話番号` varchar(16),
`携帯` varchar(16),
`キャリア` varchar(32),
`カレーの食べ方` varchar(32)
);

■DBにsqlファイルを取り込みます

$ mysql -u root -p dummydb < dummy.sql > output.tab
Enter password:

$ du output.tab
0 output.tab

■csvを取り込みます

mysql> load data local infile "~/dummy2.csv"
into table dummy
fields
terminated by ','
enclosed by '"'
escaped by '\\'
lines
terminated by '\n'
;
Query OK, 51 rows affected (0.01 sec)
Records: 51 Deleted: 0 Skipped: 0 Warnings: 0

■名前の行を一覧

mysql> select `名前` from dummy;
+---------------------------+
| 名前 |
+---------------------------+
| 名前 |
| 会田 光臣 |
| 手島 智花 |
| 谷村 妃里 |

■不要な行の検索

mysql> select `名前` from dummy where `名前`='名前';
+--------+
| 名前 |
+--------+
| 名前 |
+--------+
1 row in set (0.00 sec)

■レコード()の削除

mysql> delete from dummy where `名前`='名前';
Query OK, 1 row affected (0.00 sec)

★確認する

mysql> select `名前` from dummy;
+---------------------------+
| 名前 |
+---------------------------+
| 会田 光臣 |
| 手島 智花 |