Help us understand the problem. What is going on with this article?

MySQLコマンドまとめ

ログイン

$ mysql -uroot # root でログイン
$ mysql -u ユーザ名 -p
Enter password: [パスワード]
$ mysql -h xxx.xxx.xxx.xxx -u username -p  # リモートアクセス

サーバ操作

セッション統計情報の表示

show status;

バージョンを確認

$ mysql --version
> show variables like 'version';

現在の同時接続数

show status like "Threads_connected";

ユーザ操作

ログイン中のユーザ

select user();

ユーザ一覧を表示

select host,user from mysql.user;

ユーザ作成

create user 'user_name'@'host_name' identified by 'password';

# 権限付与と同時に行う場合
GRANT ALL PRIVILEGES ON db_name.* TO 'user_name'@'host_name' IDENTIFIED BY 'local' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON db_name.* TO 'user_name'@'host_name' IDENTIFIED BY 'local' WITH GRANT OPTION;

ユーザ削除

drop user 'user_name'@'host_name;

権限の付与

grant all privileges on db_name.table_name to 'user_name'@'host_name';
flush privileges;

ユーザ権限確認

show grants; # 現在ログイン中のユーザ
show grants for 'user_name'@'host_name'; # ユーザを指定

パスワード変更

update mysql.user set password=password('password') where user='user';
flush privileges; # 権限変更を反映

データベース操作

データベース一覧を表示

show databases;

データベースの作成

create database db_name;

使用するデータベースを選択

use db_name

データベース削除

drop database db_name;

テーブル操作

テーブル一覧を表示

show tables;

カラム一覧を表示

desc table_name;

# Collation 付き
show full columns from table_name;

ステータス情報を表示

show table status (like "table_name");

起動・停止

$ mysql.server start # 起動
$ mysql.server start --skip-grant-tables # パスワード無しでログイン
$ mysql.server stop # 停止

インデックスを確認

# 特定のテーブル
show index from table_name;

# 特定のデータベース
> use information_schema;
> SELECT * FROM statistics where TABLE_SCHEMA = database_name;
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした