SSH Tunnel을 통한 mysql server에 접속하기


참고1: https://www.rosehosting.com/blog/access-your-database-remotely-through-an-ssh-tunnel/

참고2: https://bit.ly/3JUY46T

이것은 FTP Server에 접속할 때 SFTP로 접속하는 것과 동일하다. 즉, 원래 21번 포트로 접속하지 않고 22번 포트로 접속하여 FTP의 취약한 보안성을 해결할 있다.

그렇듯이 mysql DB server에 접속할 때도 3306 포트로 접속하지 않고 SSH Tunnel을 만들어서 접속하면 22번 포트로 접속하여 훨씬 보안성을 높일 수 있다

FTP Server나 mysql Server가 Linux에서 실행중이면 이 방법을 사용하여 접속하는 것이 유리하다

==============

-mysql Server가 설치된 ansible(172.30.1.60)-
yum -y install http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
rpm –import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server
systemctl start mysqld –now

netstat -nltup
netstat -an | grep ESTABLISHED

yum install mysql -y (##mysql client 설치)

cat /var/log/mysqld.log | grep ‘temporary password’ (##임시 암호를 복사한다)
mysql -u root -p
복사한 암호를 붙여 넣는다

root 계정에 대한 암호를 변경한다
alter user ‘root’@’localhost’ identified by ‘P@ssw0rd’;
commit;

mysql -u root -p (##바뀐 암호로 로그인 하여 본다)
exit

mysql Client(node1)가 접속한 후 다시 아래 명령어를 입력한다
netstat -an | grep ESTABLISHED

===============================

-mysql Client가 설치된 node1(172.30.1.22)-

yum install mysql -y
netstat -an | grep ESTABLISHED
ssh -fNg -L 3306:127.0.0.1:3306 root@172.30.1.60

포트 포워딩: -L local_port:localhost:remote_port

man ssh | grep \-f

man ssh | grep \-N

man ssh | grep \-g

netstat -an | grep ESTABLISHED ##22번 포트로 하나 더 연결되었다는 것을 확인
mysql -h 127.0.0.1 -P 3306 -u root -p ##원격(172.30.1.60) 서버에서 실행 중인 mysql server에 접속함
show databases;

다른 세션으로 node1에 접속하여 아래 작업을 하여 어떻게 세션이 연결되었는지 확인한다
netstat -an | grep ESTABLISHED

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s