更改mysql的datadir

AppArmor and MySQL

转自MySQL 博客
By: Jeremy Smyth | Manager, MySQL Curriculum
MySQL accesses files in various places on the file system, and usually this isn't something to worry about. For example, in a standard MySQL 5.5 installation on Ubuntu, the data goes in

/var/lib/mysql

, and the socket is a file in

/var/run/mysqld

. It puts configuration files in/etc, logs and binaries in various locations, and it even needs to access some operating system files such as/etc/hosts.allow
.
This is all very well until you start trying to be clever and get MySQL to access other parts of the file system. After all, you can configure the location of data, log files, socket, and so on, so why shouldn't you use those settings to optimize your system? Unfortunately, on many modern Linux distributions, it's not that always easy.
Take Ubuntu, for example. Ubuntu comes with something called AppArmor, a kernel-integrated application security system that controls how applications can access the file system. This goes above and beyond normal permissions, and as a result can sometimes be a bit confusing.
TL;DR
First, here's the quick version of this post: If you want to relocate the data directory in MySQL (in this example, to the /data/
directory), and AppArmor is not letting you, add the following two lines to the bottom of/etc/apparmor.d/local/usr.sbin.mysqld
:

/data/ r,/data/** rwk, 

...and then reload the AppArmor profiles:

service apparmor reload

The Demonstration
To demonstrate this in a bit more detail, I've done the following:
Installed a stock MySQL 5.5 from the Ubuntu 12.04 repository
Created the/data
directory, owned by themysql
user and group
Copied my data directory to/data
withcp -a

Now, when I start MySQL with the new data directory, I get the following:
[root@boxy ~]# mysqld_safe --datadir=/data130130 21:31:51 mysqld_safe Logging to syslog.130130 21:31:51 mysqld_safe Starting mysqld daemon with databases from /data130130 21:31:51 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
...and it dies.
As it's logging tosyslog
, let's look there:
Jan 30 21:31:51 boxy mysqld: 130130 21:31:51 InnoDB: Completed initialization of buffer poolJan 30 21:31:51 boxy mysqld: 130130 21:31:51 InnoDB: Operating system error number 13 in a file operation.Jan 30 21:31:51 boxy kernel: [81703.213926] type=1400 audit(1359581511.909:36): apparmor="DENIED" operation="open" parent=16198 profile="/usr/sbin/mysqld" name="/data/ibdata1" pid=16538 comm="mysqld" requested_mask="rw" denied_mask="rw" fsuid=116 ouid=116Jan 30 21:31:51 boxy mysqld: InnoDB: The error means mysqld does not have the access rights toJan 30 21:31:51 boxy mysqld: InnoDB: the directory.
The final two lines saymysqld
doesn't have access to the directory, even though I've changed the ownership (both user and group) tomysql
. If you haven't come across AppArmor before, this is about where you start to get confused. However, that big "DENIED
" is a bit of a giveaway, and it's associated withapparmor
, so let's have a look at AppArmor's status:
[root@boxy ~]# aa-status apparmor module is loaded.18 profiles are loaded.18 profiles are in enforce mode. /sbin/dhclient... /usr/sbin/cupsd /usr/sbin/mysqld /usr/sbin/tcpdump0 profiles are in complain mode.2 processes have profiles defined....
There's a profile loaded for the mysqld
process, which could be what's blocking it from accessing/data
.
There are a couple of quick and dirty ways to get past this. You could, for example, disable AppArmor; it's a service, so you could uninstall it, or stop it with the specialteardown
command to unload all profiles. You could even delete the offending profile if you want rid of it. Another less extreme option is to use theapparmor-utils
package, which contains the utilitiesaa-complain
andaa-enforce
that allow you to work with existing profiles without removing them or stopping AppArmor entirely:
[root@boxy ~]# aa-complain /usr/sbin/mysqld Setting /usr/sbin/mysqld to complain mode.
As you can probably guess, complain mode simply whines when a process accesses something on the file system that it shouldn't, whereas enforce mode is what stops such access.
[root@boxy ~]# aa-status apparmor module is loaded.18 profiles are loaded.17 profiles are in enforce mode. /sbin/dhclient... /usr/sbin/tcpdump1 profiles are in complain mode. /usr/sbin/mysqld2 processes have profiles defined....
So now it's in complain mode, we can check to see if it starts:
[root@boxy ~]# mysqld_safe --datadir=/data130130 21:34:16 mysqld_safe Logging to syslog.130130 21:34:16 mysqld_safe Starting mysqld daemon with databases from /data
So now we know that AppArmor is the reason why MySQL is not starting, we can enforce again, before going through the proper configuration:
[root@boxy ~]# aa-enforce /usr/sbin/mysqld Setting /usr/sbin/mysqld to enforce mode.
Time to look under the covers.
Under the Covers: AppArmor's Policy Files
When you install MySQL on Ubuntu, it places an AppArmor policy file in/etc/apparmor.d/usr.sbin.mysqld
. Another policy file gets placed in/etc/apparmor.d/local/usr.sbin.mysqld
, which is initially empty (aside from comments) but exists to let you add non-standard policies such as those specific to this machine. In practice, you could add such policies to either file, but for now I'll put them in thelocal
file. There's also a cached policy file, which is a binary compiled version of the policy. We can happily ignore that; it's automatically generated from the policy text files.
Here are some of the contents of/etc/apparmor.d/usr.sbin.mysqld
:

vim:syntax=apparmor# Last Modified: Tue Jun 19 17:37:30 2007#include <tunables/global>/usr/sbin/mysqld { #include <abstractions/base>... /var/log/mysql.err rw, /var/lib/mysql/ r, /var/lib/mysql/** rwk, /var/log/mysql/ r, /var/log/mysql/* rw,... # Site-specific additions and overrides. See local/README for details. #include <local/usr.sbin.mysqld>}

In the middle are the file system policies. Looking at the settings for the existing data directory/var/lib/mysql
, you can see that the profile gives read (r
) access to the directory itself, and read, write, and lock access (rwk
) to its contents recursively (controlled by the**
). Conveniently, it also#include
s the contents of thelocal
file.
Editing the Policy
To give MySQL the necessary access to the/data
directory, I edit the includedlocal
file so it looks like the following:
[root@boxy ~]# cat /etc/apparmor.d/local/usr.sbin.mysqld# Site-specific additions and overrides for usr.sbin.mysqld.# For more details, please see /etc/apparmor.d/local/README./data/ r,/data/** rwk,
As you can see I haven't been particularly creative; I've just copied the policy that applies to the standard data directory/var/lib/mysql
, and copied it to this file, mapping the same settings to the new/data
directory. Also, although I've put this in thelocal
version of the policy file, I could just as easily have modified the main policy file.
Finally, reload the AppArmor profiles:
[root@boxy ~]# service apparmor reload * Reloading AppArmor profilesSkipping profile in /etc/apparmor.d/disable: usr.bin.firefoxSkipping profile in /etc/apparmor.d/disable: usr.sbin.rsyslogd
No errors about my new profile settings; the default configuration disables some AppArmor policies, but nothing I have to be concerned with. Finally, the moment of truth: Can we start MySQL?
[root@boxy ~]# mysqld_safe --datadir=/data130130 21:38:42 mysqld_safe Logging to syslog.130130 21:38:42 mysqld_safe Starting mysqld daemon with databases from /data
Success!
Conclusion
It's worth pointing out that this technique applies if you want to change where MySQL puts anything on the file system. Although the use case described here is a common first reason to bump up against AppArmor's security policies, the data directory is not the only thing that you might want to move. Logs, the UNIX socket, and even configuration files are subject to the controls placed in the AppArmor policy. This also includes any files you access with anything that uses theFILE
privilege, such asSELECT ... INTO OUTFILE
,LOAD DATA INFILE
, or theLOAD_FILE()
function. While you can secure this sort of access with MySQL's secure_file_priv
option, AppArmor provides a further layer of security that prevents even currently unknown exploits from accessing parts of the file system that they really, really shouldn't.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容