MySQLのインストール後にデータベースにつなぐまで

やりたいことによって異なるが、下記流れを確認する。

1.MySQLの稼働確認
2.MySQLへの接続
3.データベースの確認
4.データベースへの接続
5.テーブルの確認
6.SQLの発行
7.接続の終了

1.MySQLの稼働確認
scコマンドで起動状態を確認する。

C:\>sc query MySQL

SERVICE_NAME: MySQL
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

状態だけを表示させたい場合は、次のようにすればよい。

C:\>sc query mysql | findstr -i state
        STATE              : 4  RUNNING

2.MySQLへの接続

C:\>mysql -uroot -p
Enter password: *******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.5.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

3.データベースの確認
インストールしたばかりでは、下記4つのデータベースが作成済みのようだ。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

4.データベースへの接続および接続確認
データベースへの接続

mysql> use test;
Database changed

データベースへの接続確認。

mysql> select database();
+------------+
| database() |
+------------+
| test       |
+------------+
1 row in set (0.00 sec)

5.テーブルの確認

mysql> show tables;
Empty set (0.00 sec)

testデータベースには、テーブルが無い。

6.SQLの発行
テーブルを作成する。

mysql> create table sampleTBL(id char(100));
Query OK, 0 rows affected (0.18 sec)

テーブルが作成されたことを確認をする。

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| sampletbl      |
+----------------+
1 row in set (0.00 sec)

作成したテーブルの構造を確認する(describeを使用する場合)。

mysql> describe sampletbl;
+-------+-----------+------+-----+---------+-------+
| Field | Type      | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-------+
| id    | char(100) | YES  |     | NULL    |       |
+-------+-----------+------+-----+---------+-------+
1 row in set (0.13 sec)

show fieldsを使用しても可能。

mysql> show fields from sampletbl;
+-------+-----------+------+-----+---------+-------+
| Field | Type      | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-------+
| id    | char(100) | YES  |     | NULL    |       |
+-------+-----------+------+-----+---------+-------+
1 row in set (0.13 sec)

show create tableを実行しても同様の内容を確認できる。

mysql> show create table sampletbl;
+-----------+-------------------------------------------------------------------
----------------------------+
| Table     | Create Table
                            |
+-----------+-------------------------------------------------------------------
----------------------------+
| sampletbl | CREATE TABLE `sampletbl` (
  `id` char(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------+-------------------------------------------------------------------
----------------------------+
1 row in set (0.00 sec)

データの投入。

mysql> insert into sampletbl(id) values('1');
Query OK, 1 row affected (0.03 sec)

データの確認。

mysql> select * from sampletbl;
+------+
| id   |
+------+
| 1    |
+------+
1 row in set (0.00 sec)

7.接続の終了

mysql> quit;
Bye