在一台ubuntu测试机器上启动一个mysql实例,本来应该是一件很简单的事情,
启动的时候却报错了:
mysqld_safe --defaults-file=/etc/mysql/my3307.cnf
160218 15:45:13 mysqld_safe Can't log to error log and syslog at the same time. Remove all --log-error configuration options for --syslog to take effect.
160218 15:45:13 mysqld_safe Logging to '/var/log/mysql/error3307.log'.
160218 15:45:13 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql3307
160218 15:45:15 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld3307.pid ended
查看错误日志
/var/log/mysql/error3307.log
发现是由于权限问题无法创建ibdata文件:
160218 12:19:09 InnoDB: Completed initialization of buffer pool
160218 12:19:09 InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
InnoDB: File name ./ibdata1
InnoDB: File operation call: 'create'.
InnoDB: Cannot continue operation.
查看目录,理论上应该有读写权限的啊:
ls -ld /var/lib/mysql3307 drwx------ 4 mysql mysql 4096 Feb 18 15:09 /var/lib/mysql3307
难道还有其它什么权限限制?
用dmesg看一下,确实有一些报错,
[4489099.208660] type=1400 audit(1455768562.142:57): apparmor="DENIED" operation="mknod" profile="/usr/sbin/mysqld" name="/var/lib/mysql3307/ibdata1" pid=19775 comm="mysqld" requested_mask="c" denied_mask="c" fsuid=105 ouid=105
查看/usr/sbin/mysqld的配置:
增加mysql3307目录的权限,重启apparmor:
/var/lib/mysql3307/ r,
/var/lib/mysql3307/** rwk,
/etc/init.d/apparmor restart
再次重新启动mysqld,发现还是报错,不过这次报错的信息不一样了:
160218 12:44:03 [ERROR] Can't start server : Bind on unix socket: Permission denied
160218 12:44:03 [ERROR] Do you already have another mysqld server running on socket: /var/run/mysqld/mysqld3307.sock ?
160218 12:44:03 [ERROR] Aborting
dmesg也能看到错误消息:
[4491181.040673] type=1400 audit(1455770643.975:99): apparmor="DENIED" operation="mknod" profile="/usr/sbin/mysqld" name="/run/mysqld/mysqld3307.sock" pid=24093 comm="mysqld" requested_mask="c" denied_mask="c" fsuid=105 ouid=105
到/etc/apparmor.d/usr.sbin.mysqld增加配置,重启apparmor
/var/run/mysqld/mysqld3307.pid rw,
/var/run/mysqld/mysqld3307.sock w,
/run/mysqld/mysqld3307.pid rw,
/run/mysqld/mysqld3307.sock w,
再次重启mysqld,还是报错,不过这次是新的报错:
160218 12:57:27 [Note] Server socket created on IP: '0.0.0.0'.
160218 12:57:27 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
160218 12:57:27 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld3307.pid ended
跑一下mysql_install_db --defaults-file=/etc/mysql/my3307.cnf
这次mysqld终于可以正常启动了:
160218 14:44:59 [Note] Server socket created on IP: '0.0.0.0'.
160218 14:44:59 [Note] Event Scheduler: Loaded 0 events
160218 14:44:59 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.46-0ubuntu0.14.04.2' socket: '/var/run/mysqld/mysqld3307.sock' port: 3307 (Ubuntu)