mysqlコマンド
- mysqlコマンドの使い方〔コマンドプロンプトの起動・終了 / mysqlコマンドの開始・終了 / 入力途中でキャンセル〕
- mysqlコマンドのヘルプ表示〔\h または \n;?〕
- コマンド プロンプトでのキー操作
- コマンド プロンプトでのディレクトリ移動
- コマンドラインからSQL文を実行
mysqlコマンドの使い方
コマンドプロンプトの起動・終了 / mysqlコマンドの開始・終了 / 入力途中でキャンセル
unknown
- コマンド プロンプトの起動
コマンドプロントは、[スタート]ー[アクセサリ]-[コマンド プロンプト]から起動します。
- ディレクトリ移動
MySQLをインストールしたフォルダ内の「binフォルダ」に移動します。
デフォルトのままなら、C:\Documents and Settings\Owner> cd c:\mysql\bin
と入力し、Enterをクリック。 - mysqlコマンドの開始
MySQLのユーザー名とパスワードを入力し、MySQLサーバーへ接続します。
接続が成功すると mysql> と表示され、mysqlコマンドに入ります。 - コマンド入力途中でキャンセルする
\c
と入力し、Enterをクリックすると、mysql> に戻ります。 - mysqlコマンドの終了
\q
またはquit
と入力し、Enterをクリックします。 - コマンド プロンプトの終了
exit
と入力し、Enterをクリックすると、コマンド プロンプトが閉じます。
#MySQLのインストールフォルダ内の「bin」フォルダへ移動 C:\Documents and Settings\Owner> cd c:\mysql\bin Enter #MySQLサーバーに接続 C:\mysql\bin> mysql -u root -p Enter Enter password: ***** Enter Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 25 to server version: 4.0.26-nt Type 'help;' or '\h' for help. Type '\c' to clear the buffer. #mysqlコマンド開始 mysql> # 入力途中でキャンセル mysql> SELECT * FROM \c Enter mysql> # mysqlコマンド終了 mysql> \q Enter ← quitでもOK! Bye C:\mysql\bin> exit Enter
MYSQL管理者(root)のパスワードを設定していない場合は、MySQL管理者(root)のパスワードの設定を参考に設定してから、行ってください。
でないと、MySQL管理者(root)のパスワードは初期状態で空白のため、shell> mysql -u root
と入力すれば、誰でもパスワードなしで、全権限を持つMySQL管理者としてMySQLサーバーに接続できてしまい、セキュリティ上危険です。
mysqlコマンドのヘルプ表示
\h または \n;?
unknown
mysql> \h Enter
For the complete MySQL Manual online visit:
http://www.mysql.com/documentation
For info on technical support from MySQL developers visit:
http://www.mysql.com/support
For info on MySQL books, utilities, consultants, etc. visit:
http://www.mysql.com/portal
List of all MySQL commands:
(Commands must appear first on line and end with ';')
help (\h) Display this help.
? (\?) Synonym for `help'.
clear (\c) Clear command.
connect (\r) Reconnect to the server. Optional arguments are db and host.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
notee (\t) Don't write into outfile.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute a SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
Connection id: 11 (Can be used with mysqladmin kill)
コマンド プロンプトでのキー操作
unknown
キー操作 | 説明 |
---|---|
Ctrl+B または ← | カーソルを左へ移動 |
Ctrl+F または → | カーソルを右へ移動 |
Ctrl+A | カーソルを先頭へ移動 |
Ctrl+E | カーソルを行末へ移動 |
Ctrl+P または ↑ | 前に入力したコマンド履歴へ |
Ctrl+N または ↓ | 後に入力したコマンド履歴へ |
Ctrl+D または DEL | カーソルの後の1文字を削除 |
Ctrl+H または BS | カーソルの前の1文字を削除 |
Tab | 途中まで入力したSQL文などの入力を補完 |
コマンド プロンプトでのディレクトリ移動
unknown
# 任意のディレクトリへ移動する場合(絶対パス) C:\Documents and Settings\owner> cd C:\ Enter C:\> # 1つ上のディレクトリへ移動する場合(相対パス) C:\Documents and Settings\owner> cd .. Enter C:\Documents and Settings\> # 2つ上のディレクトリへ移動する場合(相対パス) C:\Documents and Settings\owner> cd ..\.. Enter C:\>
コマンドラインからSQL文を実行
unknown
mysql -u root -p -e "SQL文; SQL文;" データベース名
コマンドラインからSQL文を実行する事ができます。
複数のSQL文を入力する場合は、文の末尾にセミコロンを付けて指定します。
SQL文全体は、ダブルクウォートで括る必要があります。
シングルクウォートで括るとエラーになりますので注意して下さい。
# コマンドラインからSQL文を実行 # 例: データベース(db_User)にあるテーブル(tbl_Customer)の内容を表示 C:\mysql\bin> mysql -u root -p -e \"SELECT * FROM tbl_Customer\" db_User Enter Enter password: ******* Enter +--------+-----------+----------+------+------+ | UserID | FirstName | LastName | Sex | Age | +--------+-----------+----------+------+------+ | U001 | 花子 | 鈴木 | f | 21 | | U002 | 太郎 | 田中 | m | 30 | | U003 | 一郎 | 鈴木 | m | 45 | | U004 | 萌子 | 山口 | f | 18 | | U005 | 愛 | 森永 | f | 21 | | U006 | 太郎 | 鈴木 | m | 22 | | U007 | 花子 | 佐藤 | f | 35 | +--------+-----------+----------+------+------+ # 複数のSQL文を指定する場合 # 例: データベース(db_User)にあるテーブル(tbl_Customer)の内容を表示し、 新しいレコードを1行挿入します C:\mysql\bin> mysql -u root -p -e \"INSERT INTO tbl_Customer VALUES ('U008','あやか','立花','f',18); SELECT * FROM tbl_Customer;\" db_User Enter Enter password: ******* Enter +--------+-----------+----------+------+------+ | UserID | FirstName | LastName | Sex | Age | +--------+-----------+----------+------+------+ | U001 | 花子 | 鈴木 | f | 21 | | U002 | 太郎 | 田中 | m | 30 | | U003 | 一郎 | 鈴木 | m | 45 | | U004 | 萌子 | 山口 | f | 18 | | U005 | 愛 | 森永 | f | 21 | | U006 | 太郎 | 鈴木 | m | 22 | | U007 | 花子 | 佐藤 | f | 35 | | U008 | あやか | 立花 | f | 18 | +--------+-----------+----------+------+------+