少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          select FQQ,FScoreCount from Tbl_User into outfile "/tmp/terminatedtest.txt" fields terminated by ",";



          select * from test into outfile '/home/user/test.txt'

          在linux(centos)下 ,啟動了mysql 并給用戶文件讀寫的權利
          grant file on *.* to root@localhost;

          在linux系統上,目錄的權限全部是 rwxrwxrwx
          chmod 777 ...
          /home/user/test
          drwxrwxrwx  4 root root  4096 Sep  3 18:42 home
          drwxrwxrwx 10 mapuser mapuser 4096 Sep  4 03:41 user
          drwxrwxrwx 5 mapuser mapuser 4096 Sep  3 17:57 test


          在mysql下輸入
          select * from test into outfile '/home/user/test.txt'
          出現錯誤信息:
          ERROR 1 (HY000): Can't create/write to file '/home/user/test.txt' (Errcode: 13)
          當時如果是tmp目錄的話就不會有這個錯誤
          select * from test into outfile '/tmp/test.txt'
          Query OK, 0 rows test(0.00 sec)

          難道只能是tmp目錄嗎?
          有什么地方可以修改的嗎?
          后來吧home的所有者改成了mysql
          drwxrwxrwx  5 mysql mysql  4096 Sep  4 10:08 home
          select * from test into outfile '/home/test.txt'

          ERROR 1 (HY000): Can't create/write to file '/home/test.txt' (Errcode: 13)
          也是同樣出錯。

          這個有什么辦法可以寫入home目錄下面嗎?或者其他什么目錄,只要不是tmp目錄,有人說先寫入tmp目錄,再cp到想要的
          目錄,這樣做是可以,不過比較麻煩,文件比較大,2-3G呢,

          修改mysql的配置能實現嗎?還是修改文件的權限,這個是什么問題呢?


          select * from test into outfile '/tmp/test.txt'
          Query OK, 0 rows test(0.00 sec)

          看一下產生的這個文件的owner 是誰。


          [root@localhost /]# ls -l
          drwxrwxrwx    4 root     root         4096  9月  4 21:03 home
          drwxrwxrwt   10 root     root         4096  9月  4 21:03 tmp

          [root@localhost /]# mysql
          Welcome to the MySQL monitor.  Commands end with ; or \g.
          Your MySQL connection id is 27
          Server version: 5.1.14-beta MySQL Community Server (GPL)

          Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

          mysql> use mysql;
          Reading table information for completion of table and column names
          You can turn off this feature to get a quicker startup with -A

          Database changed

          mysql> select user from user;
          +--------+
          | user   |
          +--------+
          | system |
          | root   |
          +--------+
          2 rows in set (0.03 sec)

          mysql> select user from user into outfile '/home/test.txt';
          Query OK, 2 rows affected (0.02 sec)

          [root@localhost home]# ls -l
          -rw-rw-rw-    1 mysql    mysql          12  9月  4 21:12 test.txt

          [root@localhost home]# cat /home/test.txt
          system
          root


          select * from test into outfile '/home/test.txt'

          ERROR 1 (HY000): Can't create/write to file '/home/test.txt' (Errcode: 13)
          ------------------------
          從Errcode: 13來看是沒權限
          你執行上面語句時,是用什么用戶執行的呢?檢查下這個用戶是否有權限吧

          估計和權限沒關系,因為已經是777了。

          看看是不是selinux打開了,如果沒什么特別需要的話,關了為好。

          非root用戶,在mysql下執行的select * from test into outfile '/home/user/test.txt'


          select * from test into outfile '/home/user/test.txt'該語句產生的文件是
          -rw-rw-rw-    1 mysql    mysql          12  9月  4 21:12 test.txt
          mysql組的mysql用戶的。

          貌似和權限沒什么關系,我用root用戶登陸系統,執行mysql的語句,其結果還是一樣,寫入/home目錄時
          select * from test into outfile '/home/test.txt'
          ERROR 1 (HY000): Can't create/write to file '/home/test.txt' (Errcode: 13)
          還是有這個問題。
          selinux會阻止其他程序寫入操作??
          具體怎么改變一下selinx的配置呢

          我理清是什么問題了。
          在red hat系列的linux中selinux對哪些daemon可以進行怎么樣的操作是有限制的,mysql的select into outfile的命令是mysql的daemon來負責寫文件操作的。寫文件之前當然要具有寫文件的權限。而selinux對這個權限做了限制。如果 selinux是關閉的吧,這個命令執行是沒有問題的
          mysql> select user from user into outfile '/home/test.txt';
          Query OK, 2 rows affected (0.02 sec)
          當時selinux開啟時
          selinux對mysql的守護進程mysqld進行了限制。
          mysql> select user from user into outfile '/home/test.txt';
          ERROR 1 (HY000): Can't create/write to file '/home/test.txt' (Errcode: 13)
          出現了沒有權限寫的error。
          解決方法,可以關閉selinux。
          可以在/etc/selinux中找到config
          root用戶,
          shell>vi /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 - SELinux is fully disabled.
          SELINUX=enforcing

          修改SELINUX=disabled關閉selinux就可以了,這個問題就可以解決了。
          不過全部關閉SELINUX有帶來一些安全問題。
          當然也可以,單獨給mysql的守護進程權限,
          shell>getsebool -a可以查看當前的對系統一系列守護進程的權限情況。

          lpd_disable_trans --> off
          mail_read_content --> off
          mailman_mail_disable_trans --> off
          mdadm_disable_trans --> off
          mozilla_read_content --> off
          mysqld_disable_trans --> off
          nagios_disable_trans --> off
          named_disable_trans --> off
          named_write_master_zones --> off
          nfs_export_all_ro --> on
          nfs_export_all_rw --> on
          nfsd_disable_trans --> off
          nmbd_disable_trans --> off
          nrpe_disable_trans --> off

          shell>setsebool -P mysqld_disable_trans=1
          開啟對mysql守護進程的權限,這樣
          mysql> select user from user into outfile '/home/test.txt';
          寫入到自定義的目錄就沒有問題了。
          -P表示 是永久性設置,否則重啟之后又恢復預設值。
          getsebool setsebool命令在root用戶下有權限。

          除了對selinux的權限,當然首先要保證該目錄擁有讀寫權限。


          在ubuntu下 ,可以對AppArmor(/etc/apparmor.d/usr.sbin.mysqld) 修改,類似selinux。
          添加/etc/squid/lists/eighties.txt w,類似。

          posted on 2015-02-04 14:31 abin 閱讀(577) 評論(1)  編輯  收藏 所屬分類: mysql

          Feedback

          # re: Mysql解決select ... into outfile '..' mysql寫文件權限問題 2015-02-04 18:23 京山游俠
          這個背景色看著好累。  回復  更多評論
            

          主站蜘蛛池模板: 建平县| 沈阳市| 策勒县| 娱乐| 大余县| 巴里| 平塘县| 孝昌县| 岢岚县| 灵璧县| 博白县| 商南县| 寿宁县| 富民县| 新沂市| 句容市| 广水市| 武城县| 澄迈县| 梧州市| 无棣县| 漳州市| 宜兴市| 五家渠市| 双江| 侯马市| 浦北县| 商都县| 张家川| 宁陵县| 房山区| 苍溪县| 聂荣县| 伊金霍洛旗| 锦州市| 田东县| 江油市| 山东| 绥芬河市| 昆明市| 电白县|