통합 검색
통합 검색
안녕하세요. 닷홈입니다.
이번 글에서는 MySQL 외부 접속이 가능하도록 설정해 보도록 하겠습니다.
MySQL 외부 접속이기 때문에 MySQL이 기본적으로 설치가 되어 있어야 합니다.
MySQL을 설치하지 않으신 분들은 아래의 링크로 가서 우선 MySQL을 설치하여 주시기 바랍니다.
http://blog.dothome.co.kr/221580482590
진행 버전은 위와 동일하게 MySQL 5.7.26 입니다.
MySQL 유저 설정
MySQL 5.7.26 이전 버전은 MySQL 설정 파일인 /etc/my.cnf 파일에 설정 후 MySQL 서비스 재시작을 해줘야하는 번거로움이 있었으나
MySQL 5.7.26의 경우 설정 파일에서 설정해야 하는 부분은 별도로 없습니다.
우선 시작 전에 MySQL 유저 테이블을 확인 합니다.
아래의 명령어로 MySQL 접속합니다.
[root@localhost ~]# mysql -u -p
Enter password:
접속을 완료하면 아래와 같은 화면이 나옵니다.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.27 MySQL Community Server (GPL)
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>
위에 창처럼 화면이 뜬다면 아래의 명령어를 입력합니다.
mysql> use mysql;
Database changed
mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| localhost | root | *151DD99F705E918E21CE0A971D4895D9A75C08DF |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
3 rows in set (0.00 sec)
MySQL 설치 후 유저 추가나 삭제를 하지 않았을 경우 위와 같이 확인이 됩니다.
각각 항목은 아래와 같습니다.
-host : 접속 가능한 범위 (localhost : localhost로만 접속 가능, % : 외부에서도 접속 가능)
-user : 계정 ID
- authentication_string : 암호화된 계정 비밀번호
root 계정을 외부에서도 접속할 수 있도록 하기 위해 host가 %인 root 계정을 생성하겠습니다.
계정 생성 방법은 아래와 같습니다.
mysql> insert into mysql.user (host, user, authentication_string, ssl_cipher, x509_issuer, x509_subject) values ('%','root', password('원하는비밀번호'),'','','');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%'; # 모든 데이터베이스 및 안에 테이블에 관하여 모든 권한 제공
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; # 권한 저장
Query OK, 0 rows affected (0.00 sec)
mysql> quit
이렇게 까지 하면 MySQL 데모에 설정해야 하는 부분은 설정이 완료되었습니다.
iptables 포트 열기
MySQL에서 설정을 진행한 후에 서버 iptables에서 포트를 열어주지 않으면 아래와 같은 에러가 발생합니다.
anode@anode-mint:~$ mysql -h 192.168.0.241 -u root -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.241' (113)
해당 에러를 피하기 위해 iptables에 MySQL 포트를 허용해 줍니다.
MySQL 포트는 TCP 3306 입니다.
[root@localhost ~]# iptables -I INPUT 1 -p tcp --dport 3306 -j ACCEPT
[root@localhost ~]# iptables -I OUTPUT 1 -p tcp --dport 3306 -j ACCEPT
이 후 접속 확인 해봅시다.
anode@anode-mint:~$ mysql -h 192.168.0.241 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.27 MySQL Community Server (GPL)
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>
현재 정상적으로 MySQL 외부 접속이 이루어 졌습니다.
추가 했던 iptables 정책을 저장합니다.
# CentOS 6
[root@localhost ~]# service iptables save
# CentOS 7
[root@localhost ~]# /usr/libexec/iptables/iptables.init save
MySQL 외부 접속 허용의 경우 MySQL을 편하게 이용할 수 있지만 보안상의 문제가 발생할 수 있습니다.
MySQL 개발 툴 대신 phpMyAdmin을 사용해도 관리가 편합니다.
감사합니다. 오늘도 즐거운 하루 되세요. ^^
닷홈 홈페이지 :
무료홈페이지 | 무료호스팅 | 닷홈
https://www.dothome.co.kr/
닷홈 무료호스팅 신청하기 :
국내 1위 무료호스팅 | 닷홈
https://www.dothome.co.kr/web/free/index.php
기능제한 없는 진짜 무료홈페이지 신청하기 :
닷홈 | 호스팅은 닷홈
https://www.dothome.co.kr/homepage/free_builder_index.php
댓글 0