데이터베이스

<MariaDB> Mariadb 접속 포트 변경하기

xxvigrufv 2022. 10. 13. 09:16
반응형


1. 개발환경 구성. 
CentOS 7 
MariaDB 10.2.12

2. MaraiDB 서버의 서비스 포트를 변경하려면, 먼저 SELinux 활성화 여부를 확인한다.

[root@localhost bin]  sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

 

 

※위와 같이 "sestatus" 명령을 실행하였을 때, "SELinux status"가 "enabled"된 상태라면, 
MariaDB의 변경 대상 포트에 대한 설정을 추가해야만 한다.다음과 같이 SELinux에 신규 변경할 MariaDB 포트를 추가한다.

[root@localhost bin] semanage port -l | grep mysqld_port_t
//출력: mysqld_port_t                  tcp      1186, 3306, 63132-63164


[root@localhost bin] semanage port -a -t mysqld_port_t -p tcp 9306
[root@localhost bin] semanage port -l | grep mysqld_port_t


사실 mysql과 mariadb의 설정 방법은 동일하다. 먼저 root 계정으로 "vi /etc/my.cnf"라고 입력한다.

[root@localhost bin]# vi /etc/my.cnf
# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 9306
socket          = /tmp/mysql.sock
default-character-set=utf8

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
default-time-zone='+9:00'
port            = 9306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 384M
#max_allowed_packet = 1M
max_allowed_packet = 10M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
lower_case_table_names = 1
init_connect="SET collation_connection=utf8_general_ci"
init_connect="SET NAMES utf8"
character-set-server=utf8
collation-server=utf8_general_ci
skip-log-bin
log_bin_trust_function_creators = 1

 


기본 방화벽을 사용하고 있으면, 외부에서 MariaDB의 변경된 포트로 접근을 할수 있게 방화벽설정을 추가한다. 

[root@localhost bin] firewall-cmd --permanent --zone=public --add-port=9306/tcp
success
[root@localhost bin] firewall-cmd --reload
success
[root@localhost bin] firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens32
  sources:
  services: dhcpv6-client ssh
  ports: 9090/tcp 9091/tcp 6222/tcp 9080/tcp 3306/tcp 8080/tcp 8443/tcp 443/tcp 5701/tcp 25900/tcp 80/tcp 5222/tcp 9306/tcp
  protocols:
  masquerade: no
  forward-ports: port=443:proto=tcp:toport=8080:toaddr=
  source-ports:
  icmp-blocks:
  rich rules:



맨 윗 줄에 [mysqld]라고 되어 있는게 보일 것인데, mysqld이 [ ]로 감싸져 있는 이유는 구역(섹션)을 나누기 위함이라고 생각하면 편할 것이다. 즉 다른 [구역]을 만나기 전까지는 mysqld의 구역이며, 이 구역에 "port=원하는포트번호" 형식으로 적어주면 된다. 아래와 같이 말이다. :wq로 저장하고 나온뒤 재부팅 시켜주면 된다. 필자는 처음에 서비스만 재가동 시켰더니 안되서 재부팅을 하였다.

다음과 같이 MariaDB 설정 파일에서 서비스 포트 번호를 변경 후, "mariadb" 서비스를 재시작(systemctl restart mariadb)한다.


[root@localhost bin] systemctl restart mariadb

[root@localhost bin] /home/app/mariadb/bin/mysql  -uroot -p

MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 9306  |          => 설정한 포트로 변경이 되어 있어야 성공.
+---------------+-------+
1 row in set (0.00 sec)

 

끝.

반응형