2012年8月7日火曜日

3.2 MySQLの基本操作(2)

3.2.2 MySQLクライアント

MySQLサーバに接続してコマンドラインで操作をするには、MySQLクライ
アントコマンドを使います。MySQLクライアントコマンドはmysqlコマン
ドによって起動します。
# mysql -u root -p
Enter password:
-uオプションの引数には接続するユーザー名を指定します。-pオプションを
指定すると、対話的にパスワードを尋ねられます。パスワードを正しく入
力すると、プロンプトが「mysql>」に変わります。
※-pオプションの直後にパスワードを指定することもできますが、
コマンド履歴に残ってしまうので、対話的に入力した方がよいでしょう。

# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or ¥g.
Your MySQL connection id is 2
Server version: 5.0.45 Source distribution
Type 'help;' or '¥h' for help. Type '¥c' to clear the buffer.
mysql>
プロンプト「mysql>」に続けて、mysqlコマンド用のコマンドを実行でき
ます。また、任意のSQLコマンドを実行したり、MySQL独自のSQLコマン
ドを実行することもできます。主なコマンドを表3-2にまとめます。

主なmysqlコマンド


  • \?、\h MySQLクライアントコマンドのコマンドを一覧を表示する
  • \q MySQLクライアントコマンドを終了する
  • \s MySQLサーバのステータスを表示する
  • \C 文字コード文字コードを変更する
  • USE データベース名; 指定したデータベースに接続する
  • SHOW DATABASES; データベース一覧を表示する
  • SHOW TABLES; テーブル一覧を表示する
  • DESCRIBE テーブル名; テーブルの定義内容を表示する


MySQLクライアント用のコマンドは、「\」+1文字のアルファベットで指
定されます。たとえば、MySQLモニタを終了するには「\q」を入力します。


mysql> \q
Bye

実は、「\q」は「quit」コマンドの省略形です。次のように、quitコマンド
を指定することもできます。その場合は、行末に「;」を付けてください。

mysql> quit;
Bye

すべてのMySQLクライアントコマンドは「\h」もしくは「\?」を実行する
と表示されます。また「help;」と入力しても同じです。

mysql> \h
For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual,
visit:
http://dev.mysql.com/
To buy MySQL Network Support, training, or other products, visit:
https://shop.mysql.com/
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear command.
connect (\r) Reconnect to the server. Optional arguments are db
and host.
delimiter (\d) Set statement delimiter. NOTE: Takes the rest of
the line as new delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h)Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to _ pager]. Print the query results via
PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an
argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to _ outfile]. Append everything into
given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for
processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
For server side help, type 'help contents'

3.2.3 データベースへの接続


MySQLサーバは、複数のデータベースを同時に扱うことができます。どの
ようなデータベースが存在するのかは、MySQLモニタ上で次のようにして
確認することができます。

mysql> SHOW DATABASES;
+------------------------+
| Database |
+------------------------+
| information _ schema |
| mysql |
| test |
+------------------------+
3 rows in set (0.01 sec)

「SHOW DATABASESコマンド」というMySQLコマンドを実行すると、結
果が表示されます。「information_schema」と「mysql」は、MySQLが内
部的に利用するデータベースです。「test」はデフォルトで用意されている
テスト用のデータベースです。

MySQLクライアントを起動した状態では、どのデータベースにも接続して
いません。データベースへ接続するには、USEコマンドを使います。次の
例では、testデータベースに接続しています。




mysql> USE test;
Database changed

現在使用中のデータベースを知るには、SELECT DATABASEコマンドを実
行します。

mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test |
+------------+
1 row in set (0.01 sec)




もし、どのデータベースにも接続していないのであれば、次のように表示
されます。

mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)


「NULL」というデータベースに接続しているわけではないので注意してく
ださい。







0 件のコメント:

コメントを投稿