MySQLの基本的な操作を覚える。

MySQLに接続する。

オプション指定なし
bash-3.2$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.0.86 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit;
Bye
bash-3.2$ 

オプションをまったく指定しないとOSのログインユーザ名を使用して接続する。*1

rootユーザで接続
bash-3.2$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.0.86 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit;
Bye
bash-3.2$ 

rootユーザのパスワードは設定済みのため、-pオプションもつける。

MySQLサーバの状態を確認する。

MySQLが稼働中か確認
bash-3.2$ mysqladmin ping
mysqld is alive
bash-3.2$ 

稼働中ならこのように表示される。

MySQLのステータスを確認
bash-3.2$ mysqladmin status
Uptime: 56163  Threads: 1  Questions: 82  Slow queries: 0  Opens: 23  Flush tables: 1  Open tables: 17  Queries per second avg: 0.001
bash-3.2$ 

稼働中のサーバの状態が簡単に確認できる。Slow queriesなどは役に立ちそう。

データベースの一覧を表示する。

bash-3.2$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 5.0.86 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
                                          • +
Database
                                          • +
information_schema
mysql
test
                                          • +
3 rows in set (0.00 sec)

現在のカレントDB(接続中のDB)はどれなのだろうか?

mysql> select database();
                          • +
database()
                          • +
NULL
                          • +
1 row in set (0.00 sec)

接続先のDBを指定していないので"NULL"だそうです。

使用するDBを指定すると・・・

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> 
mysql> select database();
                          • +
database()
                          • +
mysql
                          • +
1 row in set (0.00 sec)

カレントDBが変わったことが確認できました。

ユーザ一覧を表示する。

ユーザ一覧を表示するには、データベースmysqlのuserテーブルの内容を表示する。

bash-3.2$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.0.86 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host, user, password from user;
                                                                                                                                          • +
| host | user | password |
                                                                                                                                          • +
| localhost | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | | hostname.local | root | | | 127.0.0.1 | root | | | localhost | | | | hostname.local | | |
                                                                                                                                          • +
5 rows in set (0.00 sec)

ユーザは、host毎に定義するのか・・・
user名が空白で表示されているのが匿名ユーザだな。*2

データベース内のテーブル一覧を表示する。

mysql> show tables;
                                                        • +
Tables_in_mysql
                                                        • +
columns_priv
db
func
help_category
help_keyword
help_relation
help_topic
host
proc
procs_priv
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user
                                                        • +
17 rows in set (0.00 sec)

テーブルのカラムを表示する。

mysql> desc host;
                                                                                                                                              • +
Field Type Null Key Default Extra
                                                                                                                                              • +
Host char(60) NO PRI
Db char(64) NO PRI
Select_priv enum('N','Y') NO N
Insert_priv enum('N','Y') NO N
Update_priv enum('N','Y') NO N
Delete_priv enum('N','Y') NO N
Create_priv enum('N','Y') NO N
Drop_priv enum('N','Y') NO N
Grant_priv enum('N','Y') NO N
References_priv enum('N','Y') NO N
Index_priv enum('N','Y') NO N
Alter_priv enum('N','Y') NO N
Create_tmp_table_priv enum('N','Y') NO N
Lock_tables_priv enum('N','Y') NO N
Create_view_priv enum('N','Y') NO N
Show_view_priv enum('N','Y') NO N
Create_routine_priv enum('N','Y') NO N
Alter_routine_priv enum('N','Y') NO N
Execute_priv enum('N','Y') NO N
                                                                                                                                              • +
19 rows in set (0.00 sec)

*1:DB上では匿名ユーザで接続したことになる

*2:user テーブルは、カレントデータベースに関係なく、ユーザに対してグローバルに権限を設定します。