DB/MySQL

[Ubuntu] MySQL 설치 그리고 MySQL, mariadb 사용자 생성 및 권한 부여

김개발^^ 2023. 1. 13. 11:49
반응형

 

설치 환경: Ubuntu 20.04.1 LTS

Database: MySQL , 5.5.68-MariaDB 

 

설치하는 부분 외에는 mysql이랑 mariadb랑 별 차이가 없다. 명령어 거의 다 똑같음... 

심지어 mariadb 접속조차도 mysql -u root -p로 mysql이랑 똑같음..

 

1. apt-get update

$ sudo apt-get update

 

2. mysql-server 설치

$ sudo apt-get install mysql-server

 

3. 기본 세팅

# 외부접속 포트 열기(default:3306)
$ sudo ufw allow mysql

# mysql 실행
$ sudo sytemctl start mysql

# 실행확인
$ ps -ef | grep mysql

# ubuntu 시작 시 자동 실행 설정
$ sudo systemctl enable mysql

 

4. 환경변수 설정

# 설정 파일 진입
$ vi ~/.profile

############### .profile ###############
# 해당 파일 마지막 부분에 하단 내용 추가
export PATH=$PATH:/usr/bin/mysql
########################################

# 환경변수 적용
$ source ~/.profile

 

5. 로그인

$ sudo mysql -u root -p
# 초기 비밀번호를 설정하지 않았으므로 password를 물으면 Enter치고 접속하면 됨


# mysql version 확인
mysql> SHOW VARIABLES LIKE "%version%";

 

6. root 계정 비밀번호 변경 

# 사용자 확인
mysql> SELECT User, Host, authentication_string FROM mysql.user;

# root 계정 비밀번호 변경
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('변경할 비밀번호');

 

7. 원격 접속 허용 계정 생성

'test'라는 사용자를 생성하는 여러가지 예시 

# 기본 생성
mysql> CREATE USER 'test' identified by '비밀번호';

# 모든 원격 환경에서 접속 가능하도록 생성
mysql> CREATE USER 'test'@'%' identified by '비밀번호';

# 특정 ip 대역에서만 접속 가능하도록 생성
mysql> CREATE USER 'test'@'xxx.xxx.%' identified by '비밀번호';

# 특정 ip만 접속 가능하도록 생성
mysql> CREATE USER 'test'@'xxx.xxx.xxx.xxx' identified by '비밀번호';

 

8. 사용자 권한 부여 GRANT

# 권한 부여 대상 사용자: test
# 대상 database: testdb

# select, insert, update 권한과, 모든 원격 환경에서 접속 가능하도록 권한 부여
mysql> GRANT select, insert, update PRIVILEGES ON testdb.* TO 'test'@'%';

# 모든 권한과 모든 원격 환경에서 접속 가능하도록 권한 부여 

mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'test'@'%';
# 모든 권한과 모든 원격 환경에서 접속 가능하며 사용자 권한 제어까지 가능하도록(==root) 권한 부여 
mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'test'@'%' with grant option;

# 권한 적용
mysql> FLUSH PRIVILEGES;

# 권한 확인
mysql> SHOW GRANTS FOR 'test'@'%'

 

9. 사용자 권한 제거 REVOKE

# test 사용자의 권한 확인
mysql> SHOW GRANTS FOR 'test'@'%';

# 기존에 부여된 select, insert, update 중 update 권한 제거
mysql> REVOKE update ON testdb.* to test@'%';

# 기존에 부여된 사용자 권한 제어가 가능한 권한(==root)을 제거할 경우
mysql> REVOKE grant option ON testdb.* to test@'%';

 

자세한 mysql 권한에 관한 내용은 하단 링크 참조(버전: 5.7, 8)

https://dev.mysql.com/doc/refman/5.7/en/grant.html

 

MySQL :: MySQL 5.7 Reference Manual :: 13.7.1.4 GRANT Statement

13.7.1.4 GRANT Statement GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level TO user [auth_option] [, user [auth_option]] ... [REQUIRE {NONE | tls_option [[AND] tls_option] ...}] [WITH {GRANT OPTION | resource_opt

dev.mysql.com

https://dev.mysql.com/doc/refman/8.0/en/grant.html

 

MySQL :: MySQL 8.0 Reference Manual :: 13.7.1.6 GRANT Statement

13.7.1.6 GRANT Statement GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level TO user_or_role [, user_or_role] ... [WITH GRANT OPTION] [AS user [WITH ROLE DEFAULT | NONE | ALL | ALL EXCEPT role [, role ] ... | role

dev.mysql.com

 

반응형

'DB > MySQL' 카테고리의 다른 글

[mysqldump] Mysql DB 백업과 복원  (0) 2023.01.13