Skip to content

Instantly share code, notes, and snippets.

@mogproject
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogproject/583482f35b31cb0f4883 to your computer and use it in GitHub Desktop.
Save mogproject/583482f35b31cb0f4883 to your computer and use it in GitHub Desktop.
MySQL Cheat Sheet

MySQL Cheat Sheet

確認

  • データベース一覧
show databases
  • テーブル一覧
use <db_name>
show tables
  • テーブル定義
desc <db_name>;
  • ユーザ一覧
select host, user, password from mysql.user;
  • ユーザ権限
show grants for <user>@<host>
  • レプリケーション状態

スレーブノード上で

show slave status \G
  • サーバパラメータ
show variables like '%host';

変更

  • カラム名変更
alter table <table_name> change <old_column_name> <new_column_name> <type> <option>;
  • カラム削除
alter table <table_name> drop column <column_name>;
  • サーバパラメータ
set global <variable_name>=<value>

or modify /etc/my.cnf

  • AUTO_INCREMENTの値をリセット
ALTER TABLE <table_name> AUTO_INCREMENT = 1;
  • TIMESTAMP型の値を変更
UPDATE <table_name> SET <column_name> = 'YYYY-MM-DD hh:mm:ss' WHERE <condition>
  • パスワード変更
set password for 'root'@'localhost' = password('root');

バックアップ

  • DB全体
mysqldump -u root -x --all-databases > 出力ファイル名
  • 特定DB
mysqldump -u root データベース名 > 出力ファイル名
  • 特定テーブル
mysqldump -h ホスト名 -u ユーザ名 -p -t データベース名 テーブル名 > 出力ファイル名
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment