1. 程式人生 > >centos7數據庫連接使用127.0.0.1報permission denied,使用localhost報No such file or directory

centos7數據庫連接使用127.0.0.1報permission denied,使用localhost報No such file or directory

臨時 pro target lamp環境 processes sele mount sdn -i

安裝lamp環境後,測試數據庫連接。

當host使用127.0.0.1時,報錯:(HY000/2002): Permission denied。

把host換成localhost後,又報錯:SQLSTATE[HY000] [2002] No such file or directory。

技術分享圖片

技術分享圖片

最後發現是因為沒有關閉selinux,通常情況下載安裝完CentOS7後,默認情況下SElinux是啟用狀態。

[root@rdo ~]# 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: 28

如果要臨時關閉,可以執行:

setenforce 0

此時狀態如下:

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

如果要永久關閉,可以修改配置文件/etc/selinux/config,將SELINU置為disabled。

[root@rdo ~]# cat /etc/selinux/config   
  
# 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  
SELINUX=disabled  
# 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  

修改該配置文件也可以執行下面的命令來完成:

sed -i ‘/SELINUX/s/enforcing/disabled/‘ /etc/selinux/config 

修改完成後,保存重啟,重啟後狀態如下:

[root@rdo ~]# sestatus  
SELinux status:                 disabled  

文章本人親測有效。

文章轉載:https://blog.csdn.net/eclothy/article/details/48265445

centos7數據庫連接使用127.0.0.1報permission denied,使用localhost報No such file or directory