![为什么 MySQL 不能使用 /run/shm 作为 tmpdir?](https://linux22.com/image/858066/%E4%B8%BA%E4%BB%80%E4%B9%88%20MySQL%20%E4%B8%8D%E8%83%BD%E4%BD%BF%E7%94%A8%20%2Frun%2Fshm%20%E4%BD%9C%E4%B8%BA%20tmpdir%EF%BC%9F.png)
我读到使用 RAM 作为 MySQL 的临时表应该可以提高性能。我尝试创建 /etc/mysql/conf.d/local.cnf,内容如下:
[mysqld]
tmpdir = /run/shm
但是重新启动mysql失败,错误日志显示:
140711 11:14:49 [Warning] Using unique option prefix myisam-recover instead of
myisam-recover-options is deprecated and will be removed in a future release. Please
use the full name instead.
140711 11:14:49 [Note] Plugin 'FEDERATED' is disabled.
140711 11:14:49 InnoDB: The InnoDB memory heap is disabled
140711 11:14:49 InnoDB: Mutexes and rw_locks use GCC atomic builtins
140711 11:14:49 InnoDB: Compressed tables use zlib 1.2.8
140711 11:14:49 InnoDB: Using Linux native AIO
/usr/sbin/mysqld: Can't create/write to file '/run/shm/ibfkgz7Z' (Errcode: 13)
140711 11:14:49 InnoDB: Error: unable to create temporary file; errno: 13
140711 11:14:49 [ERROR] Plugin 'InnoDB' init function returned error.
140711 11:14:49 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
140711 11:14:49 [ERROR] Unknown/unsupported storage engine: InnoDB
140711 11:14:49 [ERROR] Aborting
140711 11:14:49 [Note] /usr/sbin/mysqld: Shutdown complete
错误代码 13 是“权限被拒绝”。但是考虑到以下输出,我看不出问题出在哪里。
$ ls -ld /run /run/shm
drwxr-xr-x 32 root root 1040 jul 11 09:42 /run
drwxrwxrwt 3 root root 280 jul 11 09:41 /run/shm
答案1
与 apparmor 安全设置存在依赖关系
你必须更改配置才能将 tmpdir 放入 RAM
文件:/etc/mysql/my.cnf
tmpdir = /run/shm/mysql
文件:/etc/apparmor.d/usr.sbin.mysqld 与其他文件系统规则一起添加一行
/run/shm/mysql/* rw,
然后 shell 进入 GTD
service apparmor restart
mkdir /run/shm/mysql
chown mysql:mysql /run/shm/mysql
chmod -R 777 /run/shm/mysql
service mysql restart
最终,Mysql 守护进程应该已启动并运行。
PS. 不要忘记在 /etc/rc.local 中添加一行,以便系统在重启后正常运行
mkdir /run/shm/mysql && chown mysql:mysql /run/shm/mysql && chmod -R 777 /run/shm/mysql