Raspberry Pi_Kor_24.2.4 MySQL Shell 사용 – “mysql” 명령


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


24.2.4  <MySQL Shell> 사용 – "mysql" 명령

 

24.2.4.1    <MySQL Shell> 기본 체계

 

MySQL Database를 사용하기 위해서는 <MySQL Shell>을 사용한다. <MySQL Shell>은 간단한 SQL Shell 프로그램으로 자체적으로 database 명령을 입력하고, 편집하는 기능을 가지고 있다.

 

이 명령은 interactive 방식과 noninteractive 방식으로 사용할 있다. interactive 방식으로 사용될 때는 결과자료가 ASCII-table format으로 표시되고, noninteractive 방식으로 사용되면 처리결과가 tab-separated format으로 표현된다.

 

이 프로그램은 "mysql" 명령으로 시작하며, 해당 명령의 기본형식은 다음과 같다.

 

[명령 형식]

mysql  --u<user-id>  -p[password]    [option]   <database_name>

 

[주요 option]

--help, -?

Display a help message and exit.  

--host=host_name,

 -h host_name

Connect to the MySQL server on the given host.

--user=user_name,

-u user_name

The MySQL user name to use when connecting to the server.

--password[=password], -p[password]

The password to use when connecting to the server. If you use the short

option form (-p), you cannot have a space between the option and the password. If you omit the password value following the --password or p option on the Shell, mysql prompts for one.

Specifying a password on the Shell should be considered insecure. See Section 6.1.2.1, "End-User Guidelines for Password Security". You can

use an option file to avoid giving the password on the Shell.

 


 

이 명령에서 지정하는 database-name은 향후 SQL 명령에서 사용할 database이다. 여기서 특정 database를 지정하지 않으면 나중에 실제 SQL 명령을 실행하기 전에 반드시 사용할 database를 별도로 지정해야 한다.

 

이 명령에서는 "--user" 또는 "u" option을 사용하여 MySQL user id를 반드시 지정해야 한다. 특별히 지정하지 않으면, 시스템의 현재 logon 사용자 계정을 사용하려고 시도한다.

 

사용자 계정에 대한 암호는 "--password" 또는 "p" option을 사용하여 지정할 수 있는데, 단순히 option만 지정하고 실제 암호를 입력하지 않으면, 암호를 입력하는 별도의 prompt가 나타나서 암호를 입력받는다

 


 

24.2.4.2    <MySQL Shell> 프로그램 시작하기

 

MySQL database에서 작업하기 위해서는 먼저 <MySQL Shell> 프로그램을 시작해야 한다.  <MySQL Shell> 프로그램을 시작하기 위해서는 최소한 database user id와 암호를 지정해야 하는데, 다음과 같이 명령을 처리한다.

 

[명령 형식]

mysql  -u <user-id>   -p<password>  <database_name>

 

다음과 같이 명령을 입력하여 <MySQL Shell>을 시작해 본다.

 

pi@raspberrypi ~ $ mysql -u root p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 62

Server version: 5.5.43-0+deb7u1 (Debian)

 

Copyright (c) 2000, 2015, 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>

 

그러면 database user에 대한 암호를 입력하는 prompt가 나타나고, 필요한 암호를 입력하면, MySQL database에 대한 접속이 이루어 지고, MySQL database에 대한 기본적인 정보를 보여준다.

 

모든 접속이 완료되면  "mysql>" prompt가 나타나는데, 여기서 MySQL Shell 명령과 표준 database SQL 명령을 입력하여 작업을 수행할 수 있다

 

 


 

24.2.4.3    MySQL Shell 명령 사용하기

 

   MySQL Shell 명령  

 

MySQL Shell 명령은 <MySQL Shell>의 현재 상태를 확인하거나 Shell의 향후 동작과 표현 방식에 영향을 미치기 위한 명령이다.

 

모든 명령은 ";" 이나 "g"로 끝나야 한다.

 

사용할 수 있는 MySQL Shell 명령들은 다음과 같다. 각 명령에서 ""로 표현된 것은 해당 명령에 대한 축약 명령이다.

          

명령

축약

description

?        

?

Synonym for `help'.

help     

h

Display this help.

clear    

c

Clear command.

connect  

r

Reconnect to the server. Optional arguments are db and host.

edit     

e

Edit command with $EDITOR.

exit     

q

Exit mysql. Same as quit.

quit     

q

Quit mysql.

use      

u

Use another database. Takes database name as argument.

source  

.

Execute an SQL script file. Takes a file name as an argument

status   

s

Get status information from the server.

system   

!

Execute a system shell command.

warnings 

W

Show warnings after every statement.

nowarning

w

Don't show warnings after every statement.

delimiter

d

Set statement delimiter.

delimiter

d

Set statement delimiter.

ego

G

Send command to mysql server, display result vertically.

go

g

Send command to mysql server.

pager

P

Set PAGER [to_pager]. Print the query results via PAGER.

nopager

n

Disable pager, print to stdout.     

tee

T

Set outfile [to_outfile]. Append everything into given outfile.

notee

t

Don't write into outfile.

print

p

Print current command.

prompt

R

Change your mysql prompt.

rehash

#

Rebuild completion hash.

charset

C

Switch to another charset. Might be needed for processing binlog with multi-byte charsets

 

 

   MySQL Shell 명령에 대한 도움말 

 

MySQL Shell에서 처리할 수 있는 MySQL Shell 명령이나 표준 database SQL 명령에 대한 도움말이 필요하면 "help" 명령을 사용할 수 있다.

 

다음은 "use" 명령에 대한 도움말을 확인해 본 것이다. 이 결과를 보면 "use" 명령에 대한 설명, 사용하는 문법과 처리 방법에 대해서 도움말을 보여준다

 

mysql> help use ;

mysql> help use

Name: 'USE'

Description:

Syntax:

USE db_name

 

The USE db_name statement tells MySQL to use the db_name database as

the default (current) database for subsequent statements. The database

remains the default until the end of the session or another USE

statement is issued:

 

USE db1;

SELECT COUNT(*) FROM mytable;   # selects from db1.mytable

USE db2;

SELECT COUNT(*) FROM mytable;   # selects from db2.mytable

 

URL: http://dev.mysql.com/doc/refman/5.5/en/use.html

 

mysql>

 


 

   <MySQL Shell> 종료

 

MySQL Shell에서 처리를 종료하고 BASH Shell 상태로 돌아가려면 "exit" 명령이나 "quit" 명령을 실행한다. 아래와 같이 처리하면 MySQL Shell이 종료하고, BASH Shell로 돌아 온다.

 

mysql> exit;

Bye

pi@raspberrypi ~ $

 

특정 명령이 실행되는 도중에 강제로 처리를 중단하고 싶으면 [CTRL + C]를 눌러서 해당 명령을 중지한다. 만약 중단되지 않을 때는 다시 한번 [CTRL + C]를 눌러서 MySQL Shell 자체를 강제 중단(abort)할 수 있다.

 

 

   <MySQL Shell>에 대한 현재의 상태 확인

 

<MySQL Shell>이 현재 어떤 상태에 있는지를 확인하기 위해서는 "status" 명령을 사용한다. 그러면 현재 상태에 대한 정보를 표시해 준다.

 

mysql> status;

--------------

mysql  Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (armv7l) using readline 6.2

 

Connection id:          65

Current database:

Current user:           root@localhost

SSL:                    Not in use

Current pager:          stdout

Using outfile:          ''

Using delimiter:        ;

Server version:         5.5.43-0+deb7u1 (Debian)

Protocol version:       10

Connection:             Localhost via UNIX socket

Server characterset:    latin1

Db     characterset:    latin1

Client characterset:    utf8

Conn.  characterset:    utf8

UNIX socket:            /var/run/mysqld/mysqld.sock

Uptime:                 4 hours 32 min 9 sec

 

Threads: 1  Questions: 773  Slow queries: 0  Opens: 213  Flush tables: 2  Open tables: 24  Queries per second avg: 0.047

--------------

 

위의 내용을 보면, MySQL version과 관련된 정보를 확인할 수 있다. 여기서, 현재 사용되는 database에 대한 정보를 알 수 있는데, 아직까지 특별한 database가 지정되지 않은 상태이다. 또한 현재 사용된 database user에 대한 정보를 확인할 수 있다.

 

 

   사용할 database의 지정 

 

향후 표준 database SQL 명령에서 사용할 database를 지정할 때는 "use" 명령을 사용한다. "mysql" 명령을 실행할 때 처음부터 database 이름을 지정한 경우는 별도의 지정을 하지 않아도 무방하다. 또한 현재 특정 database를 사용하고 있는 상태에서 다른 database로 변경하고자 할 때도 "use" 명령을 사용한다.

 

아래는 현재 database가 지정되어 있지 않은 상태에서 use 명령으로 "mysql" 이란 database를 지정하고, 다시 status 명령으로 현재 상태를 확인한 사례이다. current database "mysql" 이란 database가 지정되어 있는 것을 확인할 수 있다.

 

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> status;

--------------

mysql  Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (armv7l) using readline 6.2

 

Connection id:          67

Current database:       mysql

Current user:           root@localhost

SSL:                    Not in use

Current pager:          stdout

Using outfile:          ''

Using delimiter:        ;

Server version:         5.5.43-0+deb7u1 (Debian)

Protocol version:       10

Connection:             Localhost via UNIX socket

Server characterset:    latin1

Db     characterset:    latin1

Client characterset:    utf8

Conn.  characterset:    utf8

UNIX socket:            /var/run/mysqld/mysqld.sock

Uptime:                 4 hours 39 min 59 sec

 

Threads: 1  Questions: 836  Slow queries: 0  Opens: 213  Flush tables: 2  Open tables: 24  Queries per second avg: 0.049

--------------

 

 

   명령의 편집 

 

실행해야 하는 명령이 길어서 Shell에서 입력하기 어려운 경우 edit 명령을 사용하면 [Vim] 편집기 화면에서 명령을 입력하거나 편집할 수 있다. [Vim]을 사용하는 방법에 대해서는 [16.4.3 vim 텍스트 편집기]를 참고하기 바란다.

 

mysql> use edit;


 

모든 내용을 입력하고 저장한 다음 해당 프로그램을 종료하고 나오면, 추가 입력을 의미하는 "->" prompt가 나타나는데, 여기서 Shell 명령 실행을 의미하는 "g"을 입력하고, [Enter] 키를 눌러서 명령을 실행하면 된다.

 

mysql> edit

    -> g

 

 


 

Leave a Reply