怎样修改MySQL数据库目录

admin 提交于 周二, 11/20/2018 - 09:58

默认情况下MySQL数据库放在/var/lib/mysql

因为磁盘空间问题,我们可能需要迁移这个目录。通常的想法是

 

service mysql stop

mv /var/lib/mysql /mnt/data/

ln -s /mnt/data/mysql /var/lib/mysql

service mysql start

然而,会发现mysql服务无法启动,/var/log/mysql/error.log会报以下错误:

 

2018-11-20T01:52:17.406707Z 0 [Warning] Can't create test file /mnt/data/mysql/uservdc.lower-test
2018-11-20T01:52:17.406751Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.24-0ubuntu0.18.04.1) starting as process 9538 ...
2018-11-20T01:52:17.409857Z 0 [Warning] Can't create test file /mnt/data/mysql/uservdc.lower-test
2018-11-20T01:52:17.409881Z 0 [Warning] Can't create test file /mnt/data/mysql/uservdc.lower-test
2018-11-20T01:52:17.412977Z 0 [Note] InnoDB: PUNCH HOLE support available
2018-11-20T01:52:17.413003Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-11-20T01:52:17.413008Z 0 [Note] InnoDB: Uses event mutexes
2018-11-20T01:52:17.413013Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2018-11-20T01:52:17.413017Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2018-11-20T01:52:17.413021Z 0 [Note] InnoDB: Using Linux native AIO
2018-11-20T01:52:17.413289Z 0 [Note] InnoDB: Number of pools: 1
2018-11-20T01:52:17.413397Z 0 [Note] InnoDB: Using CPU crc32 instructions
2018-11-20T01:52:17.415067Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2018-11-20T01:52:17.424103Z 0 [Note] InnoDB: Completed initialization of buffer pool
2018-11-20T01:52:17.426401Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2018-11-20T01:52:17.436459Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2018-11-20T01:52:17.436489Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2018-11-20T01:52:17.436513Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2018-11-20T01:52:18.037230Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2018-11-20T01:52:18.037273Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2018-11-20T01:52:18.037282Z 0 [ERROR] Failed to initialize builtin plugins.
2018-11-20T01:52:18.037288Z 0 [ERROR] Aborting

可是我们明明是有权限的,问题在于一个叫AppArmor的软件。我们需要在下面文件添加2行,/etc/apparmor.d/local/usr.sbin.mysqld:

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

然后重启apparmor服务,启动mysql:

service apparmor reload

service mysql start

参考:https://askubuntu.com/questions/758898/mysql-wont-start-after-changing-the-datadir-14-04-mysql-5-7

添加新评论

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
请输入"汉语"