(MySQL) MySQLコマンドライン利用メモ

戻る一つ前のメニューに戻る

MySQLをコマンドラインから利用する場合のメモ

目次

接続

mysql -h db.example.com -u user_name -p db_name
もしくは
mysql -h db.example.com -u user_name -p -D db_name

ポートがデフォルトの3306でない場合は、

mysql -h db.example.com -P 1234 -u user_name -p db_name

コマンドラインでパスワードも指定してしまう場合は、-pに続けて(スペースを空けずに)パスワードを指定することも出来る。

mysql -h db.example.com -u user_name -ppassword db_name

ヘルプ

mysql> help

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.
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.
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 a 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.

Connection id: 115468771  (Can be used with mysqladmin kill)

システム情報

システム情報一覧を表示する

status
show status;

MySQLのバージョンを表示する

select version();

サーバの現在時刻を表示する

select now();

定義済みのサーバ変数一覧

show variables;

ユーザ操作

user@%にはuser@localhostは含まれないので、ローカルからアクセスする場合はuser@localhostも定義すること。

ユーザ一覧表示 (hostが%と表示された場合は、アクセス元制限無し)

show user,host from mysql.user;

ユーザ作成(権限を与えるDBを指定して作成。@以下を省略した場合は@%を指定したことと同一となり、アクセス元制限が無くなる。パスワードを省略すると、パスワード無しのユーザとなる)

grant all on db_name.* to new_user_name identified by 'password';
grant all on db_name.* to new_user_name@localhost identified by 'password';
grant select,insert on db_name.* to new_user_name@localhost identified by 'password';

ユーザ作成(GRANT権限を与える)

grant all on db_name.* to new_user_name identified by 'password' with grant option;

ユーザ作成

create user new_user_name identified by 'password';

ユーザに権限を追加する

grant update on db_name.* to user_name;

ユーザの権限を削除する

remove all privileges on db_name.* from user_name;
remove update,insert on db_name.* from user_name;

ユーザの権限を表示する

show grants for user_name@hostname;

ユーザを削除する(全権限を削除後、ユーザを削除する)

revoke all privileges,grant option from user_name@hostname;
delete from mysql.user where user='user_name' and host='hostname';

ユーザ作成・権限変更後にシステムに反映する

flush privileges;

DB操作

データベース一覧表示

show databases;

利用するDBを変更する

use new_db_name;

新規データベースの作成

create database new_database_name;
create database new_database_name character set utf8;

テーブル操作

テーブル一覧表示

show tables;

基本的なデータ操作

データ抽出

select カラム名A, カラム名B from テーブル名 where 条件式 order by カラム名D;
w:SELECT (SQL)

データ追加

insert into テーブル名 (カラム名A, カラム名B) values ('値A', '値B');
w:INSERT_(SQL)

データ更新

update テーブル名 set カラム名A='値',カラム名B='値' where 条件式;
w:UPDATE_(SQL)

データ削除

delete from テーブル名 where 条件式;
w:DELETE_(SQL)

バックアップとリストア

バックアップ

mysqldump -Q -h db.example.com -u user_name -ppassword db_name > backupfile.dat

リストア
全てのテーブルをドロップした後に…

mysql -h db.example.com -u user_name -ppassword -d db_name < backupfile.dat
詳細は (MySQL) MySQLのバックアップとリストア を参照

テーブル構造のエクスポート(-dスイッチで、テーブル内容出力を抑制)

mysqldump -h db.example.com -u user_name -ppassword -d db_name table_name

参考文献

MySQLクイック・リファレンス

戻る一つ前のメニューに戻る