본문으로 바로가기

목차

    1. 문제

    1.1. 현상

    - Php가 설치된 서버에서 다른 DB서버로의 원격 접속할 경우 Permission denied가 발생하는 현상 발생

      mysql_connect(): Permission denied

    mysql_connect(): Permission denied

    1.2. 원인

    - 에러 원인은 SELinux(Security-Enhanced Linux)가 원격으로의 접속을 차단함으로써 발생되는 현상입니다.

     


    2. 문제 해결

    - 문제 해결 방법은 두가지가 있습니다. 

       1. SELinux 비활성화

       2. httpd(Apache) SELinux 설정 변경

    2.1. SELinux 비활성화

    - SELinux 비활성화를 해도 지장이 없을 경우 vi(vim) 툴을 이용해 /etc/sysconfig/selinux를 편집 후 재부팅합니다. 

    selinux가 활성화 되어있는지 확인 해봅니다.
    # 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
    
    # vim /etc/sysconfig/selinux
    
    ★다음과 같이 변경 저장 후 재부팅
    SELINUX=enforcing --> SELINUX=disabled
    
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=enforcing  
    # SELINUXTYPE= can take one of three two values:
    #     targeted - Targeted processes are protected,
    #     minimum - Modification of targeted policy. Only selected processes are protected. 
    #     mls - Multi Level Security protection.
    SELINUXTYPE=targeted
    
    # reboot

     

    - 재부팅후에 SELinux 비활성화 확인 후 DB 접속을 시도하면 잘 되는 것을 확인할 수 있습니다.

    # sestatus
    SELinux status:                 disabled

    2.2. httpd(Apache) SELinux 설정 변경

    - 다음 명령어를 이용해 httpd(Apache)에 걸려있는 SELinux 설정 값을 확인 해 봅니다.

    # getsebool -a | grep httpd
    
    httpd_anon_write --> off
    httpd_builtin_scripting --> on
    httpd_can_check_spam --> off
    httpd_can_connect_ftp --> off
    httpd_can_connect_ldap --> off
    httpd_can_connect_mythtv --> off
    httpd_can_connect_zabbix --> off
    httpd_can_network_connect --> off
    httpd_can_network_connect_cobbler --> off
    httpd_can_network_connect_db --> off
    httpd_can_network_memcache --> off
    httpd_can_network_relay --> off
    httpd_can_sendmail --> off
    httpd_dbus_avahi --> off
    httpd_dbus_sssd --> off
    httpd_dontaudit_search_dirs --> off
    httpd_enable_cgi --> on
    httpd_enable_ftp_server --> off
    httpd_enable_homedirs --> off
    httpd_execmem --> off
    httpd_graceful_shutdown --> on
    httpd_manage_ipa --> off
    httpd_mod_auth_ntlm_winbind --> off
    httpd_mod_auth_pam --> off
    httpd_read_user_content --> off
    httpd_run_ipa --> off
    httpd_run_preupgrade --> off
    httpd_run_stickshift --> off
    httpd_serve_cobbler_files --> off
    httpd_setrlimit --> off
    httpd_ssi_exec --> off
    httpd_sys_script_anon_write --> off
    httpd_tmp_exec --> off
    httpd_tty_comm --> off
    httpd_unified --> off
    httpd_use_cifs --> off
    httpd_use_fusefs --> off
    httpd_use_gpg --> off
    httpd_use_nfs --> off
    httpd_use_openstack --> off
    httpd_use_sasl --> off
    httpd_verify_dns --> off

     

    - httpd_can_network_connect가 off인 것을 확인 할 수 있습니다. 다음 명령어를 이용해 on으로 변경해 주면 됩니다.

    httpd_can_network_connect를 On으로 설정 변경
    # setsebool -P httpd_can_network_connect on
    
    적용이 되었는지 확인
    # getsebool -a | grep httpd_can_network_connect
    httpd_can_network_connect --> on
    httpd_can_network_connect_cobbler --> off
    httpd_can_network_connect_db --> off

     

    - 설정이 잘 완료되었습니다. php를 이용해 다른 서버에 있는 mysql에 접속이 잘 되는것을 확인할 수 있습니다.