제목 그대로 아래와 같은 환경에서 외부에서 MySQL DB에 접속하는 방법이다.
- OS : Ubuntu 22.04
- Apache : 2.4.52
- PHP : 8.1.2
- MySQL : 8.0.30
- ufw status : inactive (비활성)
- DBMS Client : DBeaver Community 22.1.5
적용 전에 아래 글들을 참고했다.
- 마리아DB(Maria DB) 외부접속 허용법 우분투(ubuntu) 18.0
- [Mysql] Public key retrieval is not allowed 에러 해결
- 우분투 mysql에 외부에서 접속하기
이어서 적용한 순서는 아래와 같다.
- 터미널에서
netstat -tnlp
입력 127.0.0.1:3306
⇒localhost(127.0.0.1)
만 접속 가능하고 외부 접속은 비허용 상태 확인
- 터미널에서
mysql -u root -p
입력 mysql> GRANT ALL PRIVILEGES ON <접속DB>.* TO '<사용자명>'@'%';
⇒ 외부 접속 가능하도록 권한 부여mysql> flush privileges;
mysql> commit;
mysql> quit
sudo vi /etc/mysql/mysql.cnf
입력하여 [Before]를 [After]로 수정
[Before]# # The MySQL database server configuration file. # # You can copy this to one of: # - "/etc/mysql/my.cnf" to set global options, # - "~/.my.cnf" to set user-specific options. # # One can use all long options that the program supports. # Run program with --help to get a list of available options and with # --print-defaults to see which it would actually understand and use. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html # # * IMPORTANT: Additional settings that can override those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/
[After]
# # The MySQL database server configuration file. # # You can copy this to one of: # - "/etc/mysql/my.cnf" to set global options, # - "~/.my.cnf" to set user-specific options. # # One can use all long options that the program supports. # Run program with --help to get a list of available options and with # --print-defaults to see which it would actually understand and use. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html # # * IMPORTANT: Additional settings that can override those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/ [mysqld] # 추가 port=3306 # or any other port # 추가 bind-address=0.0.0.0 # 추가
sudo systemctl restart mysql
입력하여mysql
재기동
아래는 상기 적용한 내용과 관련된 이미지들이다.