본문 바로가기

카테고리 없음

MySQL ADMIN COMMAND v1.0

MySQL ADMIN COMMAND

 

from MySQL DBA 를 위한 | http://mysqldba.tistory.com/

 

1.    MySQL ADMIN COMMAND

MySQL 사용을 위해 기본적으로 필요한 접속 명령어와 그 후 사용에 있어 필요한 명령어들에 대해 설명하겠다.

 

2.    MySQL 에의 접속

MySQL 은 다음의 명령어로 접속한다.

 

-u Option User 를 입력하기 위한 옵션이다.

oracle32@/home/mysql/mysql>./bin/mysql -u root

 

 

아래와 같이 옵션 옆에 바로 입력값을 넣어도 된다.

추가로 –p 는 패스워드 옵션이다.

oracle32@/home/mysql/mysql>./bin/mysql -uhong -phong

 

3.    패스워드의 설정

패스워드의 변경 방법은 set password 를 사용하는 방법과 grant 를 사용하는 방법이 있다. 보통 후자의 방법을 많이 사용한다. grant 를 이용해 password 변경이 가능하며 사용자가 없는 경우엔 생성까지 해준다.

 

mysql> set password for 'hong'@'10.10.10.31'=password('hong');

Query OK, 0 rows affected (0.00 sec)

 

mysql> grant all on *.* to 'hong'@'10.10.1.31' identified by 'hong';

Query OK, 0 rows affected (0.00 sec)

 

4.    USER Name Rename

mysql> rename user 'hong'@'10.10.1.31' to 'kong'@'10.10.1.31';

Query OK, 0 rows affected (0.00 sec)

 

5.    권한부여

mysql> grant create routine on mysql.* to 'kong'@'10.10.1.31';

Query OK, 0 rows affected (0.00 sec)

 

mysql> grant create routine on mysql.* to 'kong'@'10.10.1.31' identified by 'hong';

Query OK, 0 rows affected (0.00 sec)

 

6.    권한제거

권한을 부분 제거할 때는 아래와 같이 권한명을 쓰고 쉼표를 쓰고 다른 권한명을 쓰면 된다.

 

mysql> revoke create routine on mysql.* from 'kong'@'10.10.1.31';

Query OK, 0 rows affected (0.01 sec)

 

아래와 같이 모든 데이터베이스(스키마)에 대해 권한을 모두 제거할 수도 있다.

mysql> revoke all on *.* from 'kong'@'10.10.1.31';

Query OK, 0 rows affected (0.01 sec)

 

7.    show 명령어의 사용

<show binary logs, show binlog events>

MySQL Binary Log 를 조회한 화면이다. Binary Log ORACLE Archive log 를 연상하면 된다. 다른 점은 OS 단에서 해당 파일을 수동으로 삭제하면 문제가 생길 수 있다. MySQL 프롬프트에서 명령어를 사용해 비워야만 한다.

 

mysql> show binary logs;

+------------------+-----------+

| Log_name         | File_size |

+------------------+-----------+

| mysql-bin.000001 |     27838 |

| mysql-bin.000002 |    993661 |

| mysql-bin.000003 |      1962 |

| mysql-bin.000004 |       126 |

| mysql-bin.000005 |      1651 |

+------------------+-----------+

5 rows in set (0.00 sec)

 

show binlog events binary log 내의 내용을 조회할 수 있는 명령이다.

자세한 사용법은 다른 게시물에서 다루겠다.

 

mysql> show binlog events in 'mysql-bin.000004'

    -> ;

+------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------+

| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                                                |

+------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------+

| mysql-bin.000004 |   4 | Format_desc |         1 |         107 | Server ver: 5.5.8-enterprise-commercial-advanced-log, Binlog ver: 4 |

| mysql-bin.000004 | 107 | Stop        |         1 |         126 |                                                                     |

+------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------+

2 rows in set (0.00 sec)

 

<show character set>

mysql> show character set;

+----------+-----------------------------+---------------------+--------+

| Charset  | Description                 | Default collation   | Maxlen |

+----------+-----------------------------+---------------------+--------+

| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |

| dec8     | DEC West European           | dec8_swedish_ci     |      1 |

~~(( 중략 )) ~~

 

아래와 같이 명령어 뒤에 like ‘pattern’ 을 사용해 결과를 한정지을 수 있다.

 

mysql> show character set like '%kr%';

+---------+---------------+-------------------+--------+

| Charset | Description   | Default collation | Maxlen |

+---------+---------------+-------------------+--------+

| euckr   | EUC-KR Korean | euckr_korean_ci   |      2 |

+---------+---------------+-------------------+--------+

1 row in set (0.01 sec)

 

<show collation>

아래의 샘플은 like 표현을 사용해 kr 이 들어간 collation 을 검색한 결과이다.

euckr_bin euckr_korean_ci 의 차이는 영문 대소문자의 구분여부이다.

 

mysql> show collation like '%kr%';

+---------------------+---------+----+---------+----------+---------+

| Collation           | Charset | Id | Default | Compiled | Sortlen |

+---------------------+---------+----+---------+----------+---------+

| euckr_korean_ci     | euckr   | 19 | Yes     | Yes      |       1 |

| euckr_bin           | euckr   | 85 |         | Yes      |       1 |

| cp1251_ukrainian_ci | cp1251  | 23 |         | Yes      |       1 |

+---------------------+---------+----+---------+----------+---------+

3 rows in set (0.00 sec)

 

<show columns>

show [full] columns {from|in} tbl_name [{from|in} db_name][like ‘pattern’|where expr]

 

아래의 명령어는 desc 사용한 것과 동일하다. (desc 와 비교해 collation, comment 컬럼이 추가되어 있다.)

 

mysql> show full columns in cond_instances;

+-----------------------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+

| Field                 | Type         | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |

+-----------------------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+

| NAME                  | varchar(128) | utf8_general_ci | NO   |     | NULL    |       | select,insert,update,references |         |

| OBJECT_INSTANCE_BEGIN | bigint(20)   | NULL            | NO   |     | NULL    |       | select,insert,update,references |         |

+-----------------------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+

2 rows in set (0.01 sec)

 

like 표현을 사용해 결과를 정제할 수 있다.

 

mysql> show columns in cond_instances like 'NA%';

+-------+--------------+------+-----+---------+-------+

| Field | Type         | Null | Key | Default | Extra |

+-------+--------------+------+-----+---------+-------+

| NAME  | varchar(128) | NO   |     | NULL    |       |

+-------+--------------+------+-----+---------+-------+

1 row in set (0.00 sec)

 

특정 데이터베이스에 대해 결과를 검색할 수도 있다. 이를 생략하는 경우는 use database_name 으로 지정된 데이터베이스를 default 검색 범주로 사용한다.

 

mysql> show columns in cond_instances in performance_schema like 'NA%';

+-------+--------------+------+-----+---------+-------+

| Field | Type         | Null | Key | Default | Extra |

+-------+--------------+------+-----+---------+-------+

| NAME  | varchar(128) | NO   |     | NULL    |       |

+-------+--------------+------+-----+---------+-------+

1 row in set (0.01 sec)

 

<show create>

show create 뒤에 올 수 있는 명령은 다음과 같다.

 

show create {database | schema} db_name

show create event event_name

show create function func_name /*+ show function code|status 와 비교할 것*/

show create procedure procedure_name

show create table table_name

show create trigger trigger_name

show create view view_name

 

위의 명령어들의 공통점으로는 해당 타겟의 구성정보를 조회한다는 것이다.

특히 자주 사용되는 것은 table로 다음과 같은 내용을 손쉽게 조회할 수 있다.

 

mysql> create table sample(id1 char(12),id2 char(24));

Query OK, 0 rows affected (0.06 sec)

 

mysql> show create table sample;

+--------+-----------------------------------------------------------------------------------------------------------------------------+

| Table  | Create Table                                                                                                                |

+--------+-----------------------------------------------------------------------------------------------------------------------------+

| sample | CREATE TABLE `sample` (

  `id1` char(12) DEFAULT NULL,

  `id2` char(24) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

+--------+-----------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

<show errors>

마지막으로 발생했던 에러 메시지를 보여준다.

 

mysql> create sample (id1 char(43));

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sample (id1 char(43))' at line 1

mysql> create sample2 (id1 number);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sample2 (id1 number)' at line 1

mysql> show errors

    -> ;

+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Level | Code | Message                                                                                                                                                                |

+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Error | 1064 | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sample2 (id1 number)' at line 1 |

+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

위 과정을 보면 동 테이블 생성을 시도했다 실패하고 다른 이름으로 존재하지 않는 데이터 타입을 이용해 에러를 다시 유발했다. show errors 로 조회한 결과를 보면 바로전 테이블 생성 시도에 대한 에러 메시지임을 확인할 수 있다.

 

<show grants>

타겟 유저가 소유한 권한을 조회할 수 있다.

 

mysql> show grants for 'hong'@'localhost';

+-------------------------------------------------------------------------------------------------------------+

| Grants for hong@localhost                                                                                   |

+-------------------------------------------------------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'hong'@'localhost' IDENTIFIED BY PASSWORD '*4CD8800DDC268BED3C7A31630CE362A2C63A2849' |

+-------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

 

참고로 현재 세션을 맺고 있는 USER 에 대한 조회는 다음과 같다.

 

mysql> show grants;

+---------------------------------------------------------------------+

| Grants for root@localhost                                           |

+---------------------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |

| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |

+---------------------------------------------------------------------+

2 rows in set (0.00 sec)

 

mysql> show grants current_user;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'current_user' at line 1

mysql> show grants for current_user;

+---------------------------------------------------------------------+

| Grants for root@localhost                                           |

+---------------------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |

| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |

+---------------------------------------------------------------------+

2 rows in set (0.00 sec)

 

mysql> show grants for current_user();

+---------------------------------------------------------------------+

| Grants for root@localhost                                           |

+---------------------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |

| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |

+---------------------------------------------------------------------+

2 rows in set (0.00 sec)

 

<show {index | indexes | keys}>

타겟 테이블에 생성되어 있는 인덱스를 조회한다. index, indexes, keys 모두 같은 내용을 조회한다.

 

mysql> show index in event;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| event |          0 | PRIMARY  |            1 | db          | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

| event |          0 | PRIMARY  |            2 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2 rows in set (0.00 sec)

 

mysql> show index in event in mysql;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| event |          0 | PRIMARY  |            1 | db          | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

| event |          0 | PRIMARY  |            2 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2 rows in set (0.00 sec)

 

mysql> show indexes in event;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| event |          0 | PRIMARY  |            1 | db          | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

| event |          0 | PRIMARY  |            2 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2 rows in set (0.00 sec)

 

mysql> show key in event;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key in event' at line 1

mysql> show keys in event;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| event |          0 | PRIMARY  |            1 | db          | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |

| event |          0 | PRIMARY  |            2 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2 rows in set (0.00 sec)

 

<show processlist>

User Session 에 대한 정보를 조회할 수 있다. 여기서 확인한 ID 를 가지고 해당 세션을 kill 하는 것이 가능하다.

 

mysql> show processlist;

+----+------+-----------+--------------------+---------+-------+-------+------------------+

| Id | User | Host      | db                 | Command | Time  | State | Info             |

+----+------+-----------+--------------------+---------+-------+-------+------------------+

| 21 | root | localhost | performance_schema | Sleep   | 14745 |       | NULL             |

| 22 | root | localhost | mysql              | Query   |     0 | NULL  | show processlist |

+----+------+-----------+--------------------+---------+-------+-------+------------------+

2 rows in set (0.00 sec)

 

mysql> kill 21;

Query OK, 0 rows affected (0.00 sec)

 

kill 명령어의 경우 아래와 같은 추가 옵션이 사용 가능하다.

 

kill [connection | query] thread_id

 

8.    기타

<show authors >

개발자 목록과 그에 대한 설명을 보여준다.

 

<show contributors>

기부자(?) 목록을 보여준다.

 

<show engine engine_name {status | mutex}>

storage engine 정보를 조회한다.

 

<show master status>

Master 상태를 조회한다.

 

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000005 |     1760 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

 

<show slave status>

Slave 상태를 조회한다.

 

mysql> show slave status;

Empty set (0.00 sec)

 

<show open tables in database_name>

타겟 데이터베이스가 OPEN 한 상태의 Table 을 조회한다.

mysql> show open tables in mysql;

+----------+---------------------------+--------+-------------+

| Database | Table                     | In_use | Name_locked |

+----------+---------------------------+--------+-------------+

| mysql    | time_zone_transition_type |      0 |           0 |

| mysql    | time_zone_name            |      0 |           0 |

| mysql    | time_zone                 |      0 |           0 |

| mysql    | db                        |      0 |           0 |

| mysql    | help_keyword              |      0 |           0 |

| mysql    | help_category             |      0 |           0 |

| mysql    | help_relation             |      0 |           0 |

| mysql    | user                      |      0 |           0 |

| mysql    | slow_log                  |      0 |           0 |

| mysql    | plugin                    |      0 |           0 |

| mysql    | proc                      |      0 |           0 |

| mysql    | proxies_priv              |      0 |           0 |

| mysql    | help_topic                |      0 |           0 |

| mysql    | time_zone_transition      |      0 |           0 |

| mysql    | event                     |      0 |           0 |

| mysql    | columns_priv              |      0 |           0 |

| mysql    | general_log               |      0 |           0 |

| mysql    | time_zone_leap_second     |      0 |           0 |

| mysql    | servers                   |      0 |           0 |

| mysql    | host                      |      0 |           0 |

| mysql    | func                      |      0 |           0 |

| mysql    | ndb_binlog_index          |      0 |           0 |

| mysql    | tables_priv               |      0 |           0 |

| mysql    | procs_priv                |      0 |           0 |

+----------+---------------------------+--------+-------------+

24 rows in set (0.00 sec)

 

<show plugins>

플러그인 정보를 조회한다.

 

mysql> show plugins;

+-----------------------+----------+--------------------+---------+-------------+

| Name                  | Status   | Type               | Library | License     |

+-----------------------+----------+--------------------+---------+-------------+

| binlog                | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| mysql_native_password | ACTIVE   | AUTHENTICATION     | NULL    | PROPRIETARY |

| mysql_old_password    | ACTIVE   | AUTHENTICATION     | NULL    | PROPRIETARY |

| MRG_MYISAM            | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| MyISAM                | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| CSV                   | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| MEMORY                | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| FEDERATED             | DISABLED | STORAGE ENGINE     | NULL    | PROPRIETARY |

| ARCHIVE               | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| BLACKHOLE             | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| InnoDB                | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| INNODB_TRX            | ACTIVE   | INFORMATION SCHEMA | NULL    | GPL         |

| INNODB_LOCKS          | ACTIVE   | INFORMATION SCHEMA | NULL    | GPL         |

| INNODB_LOCK_WAITS     | ACTIVE   | INFORMATION SCHEMA | NULL    | GPL         |

| INNODB_CMP            | ACTIVE   | INFORMATION SCHEMA | NULL    | GPL         |

| INNODB_CMP_RESET      | ACTIVE   | INFORMATION SCHEMA | NULL    | GPL         |

| INNODB_CMPMEM         | ACTIVE   | INFORMATION SCHEMA | NULL    | GPL         |

| INNODB_CMPMEM_RESET   | ACTIVE   | INFORMATION SCHEMA | NULL    | GPL         |

| PERFORMANCE_SCHEMA    | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

| partition             | ACTIVE   | STORAGE ENGINE     | NULL    | PROPRIETARY |

+-----------------------+----------+--------------------+---------+-------------+

20 rows in set (0.00 sec)

 

<show privileges>

모든 권한을 조회하고 그에대한 설명을 보여준다.

 

mysql> show privileges;

+-------------------------+---------------------------------------+-------------------------------------------------------+

| Privilege               | Context                               | Comment                                               |

+-------------------------+---------------------------------------+-------------------------------------------------------+

| Alter                   | Tables                                | To alter the table                                    |

| Alter routine           | Functions,Procedures                  | To alter or drop stored functions/procedures          |

| Create                  | Databases,Tables,Indexes              | To create new databases and tables                    |

~~((중략))~~