개발이야기/스프링 프레임워크

Install Mysql for spring project(in Macbook)

AndersonChoi 2017. 6. 24. 15:54

Spring 프로젝트를 하면서 DB연결은 필수적이다. DB는 무었을 쓸까 고민하던 도중 Oracle을 사용하려고 했지만 Mac에서 local로 돌리기 적합하지 않다는 생각에 MySQL을 깔기로 결정하였다.


MySQL(위키피디아)은 다음과 같은 특징을 가진다.

  • 오픈소스의 관계형 데이터베이스 관리 시스템(RDBMS)
  • 다중 스레드, 다중 사용자 형식의 구조 질의어 형식의 데이터베이스 관리 시스템
  • GPL 라이선스
  • 다수의 프로그래밍 언어로 된 API 사용가능

Install MySQL

MySQL을 쉽고 빠르게 개발하기 위해서 나는 HomeBrew(공식 웹사이트)를 사용하기로 결심했다.
HomeBrew는 MacOS 용 패키지 관리자로서 한번의 설치로 다양한패키지를 단 한줄로 설치 할 수 있다.


HomeBrew 설치하기

1
/usr/bin/ruby -"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
cs



한번 HomeBrew가 설치완료되면 MySQL과 같은 패키지를 사용자의 Mac에 설치하는건 식은죽 먹기다.

HomeBrew를 사용하여 MySQL 최신버전 설치

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$ brew install mysql
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
bitcoin        freedink       jose           pycodestyle
erlang@19      iamy           libopusenc
==> Updated Formulae
amazon-ecs-cli                 kibana
amqp-cpp                       kitchen-sync
angular-cli                    knot
ansifilter               
...
blabla
blabla
...
If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc
 
For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
 
==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2l: 1,709 files, 12.2MB
==> Installing mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5
###                                                         #### 100.0%
==> Pouring mysql-5.7.18_1.sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql/5.7.18_1/bin/mysqld --initialize
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
    mysql -uroot
To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/5.7.18_1: 321 files, 232.9MB
 
cs

brew install mysql 단 한줄로 mysql이 정상설치 되었다.


Run MySQL

homebrew를 통해 설치된 경로로 가면 mysql.server 실행파일을 실행할 수 있다. 옵션으로 start를 주게 되면 mysql서버가 local환경에서 실행된다. local환경에 실행된 mysql을 접속하려면 mysql -uroot를 치면 된다.


아래와 같이 testDB 라는 이름의 데이터베이스를 생성, 조회, 제거를 정상적으로 동작하는 모습도 볼 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> create database testDB;
Query OK, 1 row affected (0.00 sec)
 
mysql> show create database testDB;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| testDB   | CREATE DATABASE `testDB` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> drop database testDB;
Query OK, 0 rows affected (0.00 sec)
cs


Add database users

local환경에서 개발하기 위해 아래와 같이 plalabdb라는 db를 생성하고 dev라는 이름으로 접속이 가능토록 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> create database plalabdb;
Query OK, 1 row affected (0.00 sec)
 
mysql> show create database plalabdb;
+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| plalabdb | CREATE DATABASE `plalabdb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> grant all privileges on plalabdb.* to dev@localhost identified by 'pla1';
Query OK, 0 rows affected, 1 warning (0.03 sec)
cs


위와 같이 정상적으로 접속 설정이 완료되었으면 아래와 같이 root권한이 아닌 dev라는 이름의 id로 접속도 가능한것을 볼 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
╭─anderson@andersonui-MacBook ~  
╰─$ mysql -u dev -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18 Homebrew
 
Copyright (c) 20002017, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| plalabdb           |
+--------------------+
2 rows in set (0.00 sec)
 
mysql> 
cs



End of Documents