SELinux:让Apache在CentOS上与MySQL交谈

所以我把MySQL连接到/ opt / chroot / mysql,并且一切正常。 Apacheconfiguration正常,也运行良好。

我创build了一个连接到MySQL Daemon的小PHP脚本,并configuration了PHP,以便使用的默认套接字是chroot。 当SELinux被禁用时,脚本能够正常连接。 但是,启用SELinux时,脚本无法连接(错误号为13),审计日志告诉我SELinux拒绝了请求。

我发现每个教程都解决了这个问题,告诉我禁用SELinux。 这不是我想要做的,所以请不要提示! 我想要一个解决scheme,同时启用SELinux …我假设一个必须存在…

我想我可能不得不改变mysql文件的SELinux上下文,但我不知道如何改变它们,使其工作。

更新2

type = AVC msg = audit(1318863312.959:435):avc:denied {connectto} for pid = 12472 comm =“httpd”path =“/ opt / chroot / mysql / var / lib / mysql / mysql.sock”scontext = unconfined_u :system_r:httpd_t:s0 tcontext = unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 tclass = unix_stream_socket

您可以通过以下步骤构build自定义SELinux策略模块:

# grep httpd_t audit.log | audit2allow -m httpd > httpd.te # checkmodule -M -m -o httpd.mod httpd.te # semodule_package -m httpd.mod -o httpd.pp # semodule -i httpd.pp 

有关更多详细信息,请参阅此主题。


UPDATE

  1. 运行semanage命令为/opt/chroot/mysql/var/lib/mysql/添加一个上下文映射:

     # semanage fcontext -a -t mysqld_db_t "/opt/chroot/mysql/var/lib/mysql(/.*)?" 
  2. 并使用restorecon命令来应用此上下文映射:

     # restorecon -Rv /opt/chroot/mysql/var/lib/mysql 

如果你通过TCP / IP连接,试试这个:

 # setsebool -P httpd_can_network_connect 1