MySQL 시작하기

Homebrew 설치하기 [페이지 바로가기]

homebrew는 macOS용 패키지 관리자이다.

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

MySQL 설치하기

install 이나 upgrade 전에는 homebrew를 최신버전으로 유지해준다.

$ brew update

그리고 나서 mysql을 설치해준다.

$ brew install mysql

특정 버전을 설치할 수도 있다.

$ brew install mysql <version>

설치에 성공하면 다음과 같이 뜬다.

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

설치시에 root계정이 비밀번호 없이 생성되므로, 비밀번호를 설정을 해줘야한다.

비밀번호 설정하기

$ mysql_secure_installation
# : 복잡한 비밀번호를 사용하도록 제한해주는 플러그인을 사용하려면 yes, 보안은 무시하고 그냥 쓰던 비밀번호 제한받지 않고 쓰고 싶다면 no.
Would you like to setup VALIDATE PASSWORD plugin? (Press y|Y for Yes, any other key for No) : no

# 익명사용자 삭제할지/계속 사용할지 여부. no 하면 $ mysql -uroot가 아니라 $ mysql만으로도 접속 가능. yes하면 -u 옵션 필수.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

# localhost외에 다른 ip에서 root 아이디로 원격접속 가능하게 할지.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

# mysql에 기본적으로 설정된 test 디비 삭제 여부

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 
~$

설치된 버전 확인하기

$ mysql --version                                                 mysql  Ver 8.0.15 for osx10.14 on x86_64 (Homebrew)

MySQL 시작하기

$ brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
$ sudo mysql.server start                                      
Starting MySQL
. SUCCESS!

MySQL 접속하기

$ mysql -h호스트주소 -u아이디 -p비밀번호

여기서 호스트주소는 생략할 수 있다.

$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.15 Homebrew

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

접속 끊기

control + D를 하면 mysql 연결이 끊긴다.

상태확인하기

mysql> status
--------------
mysql  Ver 8.0.15 for osx10.14 on x86_64 (Homebrew)

Connection id:        18
Current database:    
Current user:        root@localhost
SSL:            Not in use
Current pager:        less
Using outfile:        ''
Using delimiter:    ;
Server version:        8.0.15 Homebrew
Protocol version:    10
Connection:        Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
UNIX socket:        /tmp/mysql.sock
Uptime:            9 min 7 sec

Threads: 2  Questions: 17  Slow queries: 0  Opens: 108  Flush tables: 2  Open tables: 84  Queries per second avg: 0.031
--------------

사용자 계정 다루기

root계정으로 접속한 후에 새로운 사용자 계정 생성 및 삭제할 수 있다.

사용자 계정 확인하기

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> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+

사용자 계정 생성하기

root 계정으로 하기보다는 사용자 계정을 생성해서 사용하는 것이 좋다.

mysql> create user 'user'@'host' identified by 'password';
  • 여기서 호스트 부분은 다른 컴퓨터에서도 접속하고 싶다면 %로 로컬환경에서 접속할 수 있게 하고 싶다면 localhost 로 설정하면 된다.

mysql>  create user 'exmaple'@'localhost' identified by 'example';
Query OK, 0 rows affected (0.01 sec)

mysql> select host,user from user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | exmaple          |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

사용자 계정 삭제하기

mysql> DROP USER [user명]@[server명];

사용자에게 권한 주기

mysql> GRANT ALL PRIVILEGES ON [database명].[테이블명] TO [user명]@[server명];

모든 권한 주기

mysql>  GRANT ALL PRIVILEGES ON *.* TO [user명]@[server명;
mysql>  grant all privileges on *.* to 'example'@'localhost';
grant all database.* to example@localhost;

특정 데이터베이스 권한주기

mysql> GRANT ALL PRIVILEGES ON [database명].* TO [user명]@[server명];
mysql> grant all privileges on test.* to 'example'@'localhost';
Query OK, 0 rows affected (0.00 sec)

사용자 권한 취소하기

모든 권한 삭제

mysql> REVOKE ALL ON [database명].[table명] FROM [user명]@[server명];

특정 권한 삭제

mysql> REVOKE DROP ON [database명].[table명] FROM [user명]@[server명];

데이터베이스

Database 조회

mysql>  show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

Database 생성

mysql> create database [데이터베이스명];
mysql> create database django;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| django             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Database 선택

mysql> USE [데이터베이스명];

참조

Last updated