<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post/2115/</link>
<title><![CDATA[解决select ... into outfile .. mysql写文件权限问题,selinuxile权限是不能授予的 必须root用户实行 这也是为了系统的安全性]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 27 Oct 2009 04:04:24 +0000</pubDate> 
<guid>http://jackxiang.com/post/2115/</guid> 
<description>
<![CDATA[ 
	 <br/><div class="code">select FQQ,FScoreCount from Tbl_User into outfile &quot;/tmp/terminatedtest.txt&quot; fields terminated by &quot;,&quot;;</div><br/><br/>select * from test into outfile &#039;/home/user/test.txt&#039; <br/><br/>在linux（centos）下 ，启动了mysql 并给用户文件读写的权利<br/>grant file on *.* to root@localhost;<br/><br/>在linux系统上，目录的权限全部是 rwxrwxrwx<br/>chmod 777 ...<br/>/home/user/test<br/>drwxrwxrwx&nbsp;&nbsp;4 root root&nbsp;&nbsp;4096 Sep&nbsp;&nbsp;3 18:42 home<br/>drwxrwxrwx 10 mapuser mapuser 4096 Sep&nbsp;&nbsp;4 03:41 user<br/>drwxrwxrwx 5 mapuser mapuser 4096 Sep&nbsp;&nbsp;3 17:57 test<br/><br/><br/>在mysql下输入<br/>select * from test into outfile &#039;/home/user/test.txt&#039;<br/>出现错误信息：<br/>ERROR 1 (HY000): Can&#039;t create/write to file &#039;/home/user/test.txt&#039; (Errcode: 13)<br/>当时如果是tmp目录的话就不会有这个错误<br/>select * from test into outfile &#039;/tmp/test.txt&#039;<br/>Query OK, 0 rows test(0.00 sec)<br/><br/>难道只能是tmp目录吗？<br/>有什么地方可以修改的吗？<br/>后来吧home的所有者改成了mysql<br/>drwxrwxrwx&nbsp;&nbsp;5 mysql mysql&nbsp;&nbsp;4096 Sep&nbsp;&nbsp;4 10:08 home<br/>select * from test into outfile &#039;/home/test.txt&#039;<br/><br/>ERROR 1 (HY000): Can&#039;t create/write to file &#039;/home/test.txt&#039; (Errcode: 13)<br/>也是同样出错。<br/><br/>这个有什么办法可以写入home目录下面吗？或者其他什么目录，只要不是tmp目录，有人说先写入tmp目录，再cp到想要的<br/>目录，这样做是可以，不过比较麻烦，文件比较大，2-3G呢，<br/><br/>修改mysql的配置能实现吗？还是修改文件的权限，这个是什么问题呢？ <br/><br/><br/>select * from test into outfile &#039;/tmp/test.txt&#039;<br/>Query OK, 0 rows test(0.00 sec)<br/><br/>看一下产生的这个文件的owner 是谁。<br/><div class="code"><br/>&#91;root@localhost /&#93;# ls -l<br/>drwxrwxrwx&nbsp;&nbsp;&nbsp;&nbsp;4 root&nbsp;&nbsp;&nbsp;&nbsp; root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4096&nbsp;&nbsp;9月&nbsp;&nbsp;4 21:03 home<br/>drwxrwxrwt&nbsp;&nbsp; 10 root&nbsp;&nbsp;&nbsp;&nbsp; root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4096&nbsp;&nbsp;9月&nbsp;&nbsp;4 21:03 tmp<br/><br/>&#91;root@localhost /&#93;# mysql<br/>Welcome to the MySQL monitor.&nbsp;&nbsp;Commands end with ; or &#92;g.<br/>Your MySQL connection id is 27<br/>Server version: 5.1.14-beta MySQL Community Server (GPL)<br/><br/>Type &#039;help;&#039; or &#039;&#92;h&#039; for help. Type &#039;&#92;c&#039; to clear the buffer.<br/><br/>mysql&gt; use mysql;<br/>Reading table information for completion of table and column names<br/>You can turn off this feature to get a quicker startup with -A<br/><br/>Database changed<br/><br/>mysql&gt; select user from user;<br/>+--------+<br/>&#124; user&nbsp;&nbsp; &#124;<br/>+--------+<br/>&#124; system &#124; <br/>&#124; root&nbsp;&nbsp; &#124; <br/>+--------+<br/>2 rows in set (0.03 sec)<br/><br/>mysql&gt; select user from user into outfile &#039;/home/test.txt&#039;;<br/>Query OK, 2 rows affected (0.02 sec)<br/><br/>&#91;root@localhost home&#93;# ls -l<br/>-rw-rw-rw-&nbsp;&nbsp;&nbsp;&nbsp;1 mysql&nbsp;&nbsp;&nbsp;&nbsp;mysql&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12&nbsp;&nbsp;9月&nbsp;&nbsp;4 21:12 test.txt<br/><br/>&#91;root@localhost home&#93;# cat /home/test.txt<br/>system<br/>root<br/></div><br/>select * from test into outfile &#039;/home/test.txt&#039;<br/><br/>ERROR 1 (HY000): Can&#039;t create/write to file &#039;/home/test.txt&#039; (Errcode: 13)<br/>------------------------<br/>从Errcode: 13来看是没权限<br/>你执行上面语句时，是用什么用户执行的呢？检查下这个用户是否有权限吧<br/><br/>估计和权限没关系，因为已经是777了。<br/><br/>看看是不是selinux打开了，如果没什么特别需要的话，关了为好。<br/><br/>非root用户，在mysql下执行的select * from test into outfile &#039;/home/user/test.txt&#039;<br/><br/><br/>select * from test into outfile &#039;/home/user/test.txt&#039;该语句产生的文件是<br/>-rw-rw-rw-&nbsp;&nbsp;&nbsp;&nbsp;1 mysql&nbsp;&nbsp;&nbsp;&nbsp;mysql&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12&nbsp;&nbsp;9月&nbsp;&nbsp;4 21:12 test.txt<br/>mysql组的mysql用户的。<br/><br/>貌似和权限没什么关系，我用root用户登陆系统，执行mysql的语句，其结果还是一样，写入/home目录时<br/>select * from test into outfile &#039;/home/test.txt&#039;<br/>ERROR 1 (HY000): Can&#039;t create/write to file &#039;/home/test.txt&#039; (Errcode: 13)<br/>还是有这个问题。<br/>selinux会阻止其他程序写入操作？？<br/>具体怎么改变一下selinx的配置呢<br/><br/>我理清是什么问题了。<br/>在red hat系列的linux中selinux对哪些daemon可以进行怎么样的操作是有限制的，mysql的select into outfile的命令是mysql的daemon来负责写文件操作的。写文件之前当然要具有写文件的权限。而selinux对这个权限做了限制。如果 selinux是关闭的吧，这个命令执行是没有问题的<br/>mysql&gt; select user from user into outfile &#039;/home/test.txt&#039;;<br/>Query OK, 2 rows affected (0.02 sec)<br/>当时selinux开启时<br/>selinux对mysql的守护进程mysqld进行了限制。<br/>mysql&gt; select user from user into outfile &#039;/home/test.txt&#039;;<br/>ERROR 1 (HY000): Can&#039;t create/write to file &#039;/home/test.txt&#039; (Errcode: 13)<br/>出现了没有权限写的error。<br/>解决方法，可以关闭selinux。<br/>可以在/etc/selinux中找到config<br/>root用户，<br/>shell&gt;vi /etc/selinux/config<br/><br/># This file controls the state of SELinux on the system.<br/># SELINUX= can take one of these three values:<br/>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enforcing - SELinux security policy is enforced.<br/>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;permissive - SELinux prints warnings instead of enforcing.<br/>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disabled - SELinux is fully disabled.<br/>SELINUX=enforcing<br/><br/>修改SELINUX=disabled关闭selinux就可以了，这个问题就可以解决了。<br/>不过全部关闭SELINUX有带来一些安全问题。<br/>当然也可以，单独给mysql的守护进程权限，<br/>shell&gt;getsebool -a可以查看当前的对系统一系列守护进程的权限情况。<br/><br/>lpd_disable_trans --&gt; off<br/>mail_read_content --&gt; off<br/>mailman_mail_disable_trans --&gt; off<br/>mdadm_disable_trans --&gt; off<br/>mozilla_read_content --&gt; off<br/>mysqld_disable_trans --&gt; off<br/>nagios_disable_trans --&gt; off<br/>named_disable_trans --&gt; off<br/>named_write_master_zones --&gt; off<br/>nfs_export_all_ro --&gt; on<br/>nfs_export_all_rw --&gt; on<br/>nfsd_disable_trans --&gt; off<br/>nmbd_disable_trans --&gt; off<br/>nrpe_disable_trans --&gt; off<br/><br/>shell&gt;setsebool -P mysqld_disable_trans=1<br/>开启对mysql守护进程的权限，这样<br/>mysql&gt; select user from user into outfile &#039;/home/test.txt&#039;;<br/>写入到自定义的目录就没有问题了。<br/>-P表示 是永久性设置，否则重启之后又恢复预设值。<br/>getsebool setsebool命令在root用户下有权限。<br/><br/>除了对selinux的权限，当然首先要保证该目录拥有读写权限。<br/><br/><br/>在ubuntu下 ，可以对AppArmor(/etc/apparmor.d/usr.sbin.mysqld) 修改，类似selinux。<br/>添加/etc/squid/lists/eighties.txt w,类似。 <br/><br/><br/><br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post/2115/#blogcomment63417</link>
<title><![CDATA[[评论] 解决select ... into outfile .. mysql写文件权限问题,selinuxile权限是不能授予的 必须root用户实行 这也是为了系统的安全性]]></title> 
<author>糊糊 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 09 Feb 2012 02:54:54 +0000</pubDate> 
<guid>http://jackxiang.com/post/2115/#blogcomment63417</guid> 
<description>
<![CDATA[ 
	把目錄所屬改為Mysql就可以了。
]]>
</description>
</item>
</channel>
</rss>