본문 바로가기

DEVELOPER/Database

[MySQL] MySQL CLI로 쉽게 다루기(+ MySQL Shell 명령어 정리)


안녕하세요😎 백엔드 개발자 제임스입니다 :)

SQL 기반의 관계형 데이터베이스 중 대표적으로 MySQL이 있습니다. 이러한 MySQL 데이터베이스를 다루기 위해선 다양한 방법이 있는데요. 그 중 CLI 환경과 GUI 환경을 기준으로 나눌 수 있습니다. 오늘 포스팅은 CLI 환경에서 MySQL을 다루는 방법에 대해서 정리할 것입니다. 

CLI는 Command-line interface의 약자입니다. 즉, 명령어 기반 환경인데요. 주로 터미널, shell 을 통해서 다룰 수 있습니다. 오늘 포스팅에서는 MySQL Shell에서 사용되는 명령어를 정리하려고 합니다. 추가로 GUI로는 보통 MySQL Workbench를 사용합니다.

MySQL Shell 설치는 아래 링크에서 할 수 있습니다.

https://www.mysql.com/downloads/

 

MySQL :: MySQL Downloads

MySQL Cluster CGE MySQL Cluster is a real-time open source transactional database designed for fast, always-on access to data under high throughput conditions. MySQL Cluster MySQL Cluster Manager Plus, everything in MySQL Enterprise Edition Learn More » C

www.mysql.com

설치 도중에 MySQL user, 패스워드를 설정하는 부분이 있습니다.
작성한 패스워드는 절대절대 잊으면 안됩니다!! (나중에 자주 사용되요.)

MySQL 초기화면

MySQL Shell을 실행하면 위와 같은 창이 열립니다.

1) MySql 접속

\connect --mysql [user]@URL:포트
ex) \connect --mysql root@localhost:3306 or \c --mysql root@localhost:3306

위와 같이 명령어를 입력하면 연결이 되는 것을 볼 수 있습니다.

만약 첫 접속이라면, MySQL 패스워드를 입력해야합니다.


2) sql 모드 변경

JS 모드에서 sql 명령어 실행

 이미지를 보면, ssl 글자 뒤에 JS가 적혀 있는데요. 현재 JS(JavaScript) 모드가 적용 중이라는 의미입니다. 그렇기 때문에 Sql 명령어가 적용되지 않고, 오류가 발생하게 됩니다. Sql 모드로 변경하려면 아래 명령어를 입력합니다. (슬래시(/)역슬래시(\) 를 잘 구분합니다.)

\sql

sql 모드로 변경된 모습


3) 데이터 베이스 확인

* 두 명령어 모두 동일한 결과를 출력합니다.
(1) \show query show databases;
(2) show databases;

show database 결과


4) 데이터 베이스 삭제

drop database [db이름];


5) 데이터 베이스 생성

create database [db이름];


6) 데이터 베이스 선택

use [db이름];


7) 테이블 생성

create table member (
-> student_id int primary key auto_increment
-> student_name varchar(50)
-> gender char(1),
->);
세미클론(;)을 작성할 때까지 하나의 명령어 입니다.


8) 테이블 목록 보기

show tables;


* MySQL documentation


아래 링크를 참고하면, 더 많은 명령어를 알 수 있습니다.

https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-commands.html

 

MySQL :: MySQL Shell 8.0 :: 3.1 MySQL Shell Commands

3.1 MySQL Shell Commands MySQL Shell provides commands which enable you to modify the execution environment of the code editor, for example to configure the active programming language or a MySQL Server connection. The following table lists the commands t

dev.mysql.com

 


 

반응형