EC2 인스턴스를 먼저 생성하고 Utuntu 에 접속했다는 가정하게 진행
1. apt-get update
$ sudo apt-get update
☞ apt : advanced package tool 는 데비안의 패키징 시스템을 관리하는 도구 모음
☞ apt-get : 패키지 관리 명령어로써 패키지 설치, 업데이트, 제거가 가능
☞ apt-cache: : 새 패키지를 검색하는데 사용
☞ apt = apt-get + apt-catch 의 느낌이지만 apt-get 명령의 일부 기능을 포함하지 않고있다.
2. install mysql
$ sudo apt-get install mysql-server
3. 설치 확인
$ dpkg -l | grep mysql-server
☞ dpkg -l : 시스템에 설치된 모든 package 를 리스트로 출력
☞ 그 중에서 mysql-server 만 찾아서 출력
ubuntu@ip-172-31-39-66:/etc/mysql/mysql.conf.d$ dpkg -l | grep mysql-server
ii mysql-server 8.0.36-2ubuntu3 all MySQL database server (metapackage depending on the latest version)
ii mysql-server-8.0 8.0.36-2ubuntu3 amd64 MySQL database server binaries and system database setup
ii mysql-server-core-8.0 8.0.36-2ubuntu3 amd64 MySQL database server binaries
4. net-tools 설치
$ sudo apt install net-tools
- 설치하는 이유 - 참고링크
네트웍관리자가 흔히 사용하는 여러 가지 기능들(명령어, 유틸리티등)을 사용하기 쉽게 모아놓은 것
ifconfig, netstat 등과 같은 명령어를 사용가능
5. MySQL 실행 확인
$ sudo systemctl status mysql
- systemctl : system services 를 조작하고 운영할수 있게 해주는 명령어.
- systemctl status 서비스 : 특정 서비스의 현재 상태를 보여달라는 명령어
$ sudo netstat -tap | grep mysql
☞ netstat : network statistics 를 의미 - 라우팅 테이블, 연결된 네트워크 같은 것을 보여주는 유틸리티
☞ t : 연결된 tcp 만 보여줘
☞ a : 연결되고 listening 하고 있는 모든 포트를 보여줘
☞ p : PID 와 프로그램의 이름을 보여줘
더 많은 netstat 사용 방법 링크
6. MySQL 외부 접속허용
- 이동해서 mysqld.cnf 를 수정
$ cd /etc/mysql/mysql.conf.d
$ sudo vi mysqld.cnf
- 수정 내용
bind-address = 127.0.0.1 을
bind-address = 0.0.0.0 로 수정
7. MySQL 접속
$ sudo mysql
------ 다른 방식으로 접속할 시
$ mysql -u root -p
ERROR 1698 (28000): Access denied for user 'root'@'localhost' 발생
$ sudo mysql -u root
root 유저는 auth_socket을 이용하므로 sudo 로 접속하면 가능하다.
8. 사용할 데이터베이스 생성
mysql> create database temp_database;
- temp_database 에 사용할 db 명을 입력
- show databases; 를 입력하면 생성된 db 확인 가능
9. 사용자 생성
mysql> create user 'test'@'localhost' identified by 'DB패스워드';
- test 에 원하는 이름을 생성
- localhost 를 % 로 하여 어느 호스트에서든 접속이 가능하도록 설정 가능
- DB패스워드에 원하는 비번 설정
10. 모든 권한 부여
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost' with grant option;
mysql > flush privileges;
- *.* : 모둔 database 를 의미 -> temp_database.* 로 지정도 가능
- with grant option : test 라는 유저에게 준 권한을 test 유저가 다른 유저에게도 권한을 부여할 수 있음.
- flush privileges : 변경된 사항을 바로 적용하기 위해 사용하는 명령어
권한 확인
mysql > SHOW GRANTS FOR '사용자계정'@'호스트';
11. AWS EC2 인바운드 규칙에 MySQL 접속포트 추가
명령어
MySQL 시작 / 서버 재시작시 MySQL 자동 재시작
$ sudo systemctl start mysql
$ sudo systemctl enable mysql
사용자 조회
mysql> use mysql;
mysql> select user, host from user;
사용자 삭제
mysql> DROP USER [user명]@[server명];
비번변경 --> 버전 별로 다르다
# 5.6 이하
mysql > update user set password=password('새 비번') where user = '유저이름';
# 5.7 이상
mysql > update user set authentication_string=password('새 비번') where user = '유저이름';
# 8.x 이상
mysql > alter user '유저이름'@'localhost' identified with mysql_native_password by '새 비번';
참고자료
- https://velog.io/@ddeo99/AWS-EC2-ubuntu%EC%97%90-MySQL-%EC%84%A4%EC%B9%98%ED%95%98%EA%B8%B0
- https://technote.kr/32