回顾:
主要是MysqL-systemd-start脚本中的路径开始没修改,换这种启动方式后没注意到他,引出了初始化数据库的一堆折腾。
OS:Ubuntu16.04
MysqL:5.7.17
apt install之后默认datadir 为/var/lib/MysqL,希望修改下datadir的路径
修改/etc/MysqL/MysqL.conf.d/MysqLd.cnf中的datadir,并删除了原来的/var/lib/MysqL文件夹(也可将这个原来的内容mv过去,就不用初始化了),
修改/etc/apparmor.d/usr.sbin.MysqLd中的datadir路径为新的值, 并service apparmor reload重启服务。
在新路径下执行数据库初始化
在/webdata/db存在,但MysqL不存在情况下,执行报无权限。如果手动创建,又报路径已存在的错误。zhujinhua@ThinkPad-E455:/webdata$ MysqLd --initialize MysqLd: Can't create directory '/webdata/db/MysqL/' (Errcode: 13 - Permission denied) 2016-12-17T14:50:43.250752Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-12-17T14:50:43.261000Z 0 [ERROR] Aborting
参考了:
http://bugs.MysqL.com/bug.PHP?id=82281
https://blogs.oracle.com/jsmyth/entry/apparmor_and_MysqL
了解到是apparmor的问题。但路径确实修改过了。
这个通过暂时停止apparmor的服务执行数据库初始化成功
启动MysqL服务,
zhujinhua@ThinkPad-E455:/webdata$ systemctl restart MysqL.service Job for MysqL.service Failed because the control process exited with error code. See "systemctl status MysqL.service" and "journalctl -xe" for details.启动失败,按提示查看原因
zhujinhua@ThinkPad-E455:/webdata$ journalctl -xe 12月 17 23:22:26 ThinkPad-E455 systemd[1]: Stopped MysqL Community Server. -- Subject: Unit MysqL.service has finished shutting down -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit MysqL.service has finished shutting down. 12月 17 23:22:26 ThinkPad-E455 systemd[1]: Starting MysqL Community Server... -- Subject: Unit MysqL.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit MysqL.service has begun starting up. 12月 17 23:22:26 ThinkPad-E455 MysqL-systemd-start[31248]: MysqL system database not found. Please run MysqL_install_db tool. 12月 17 23:22:26 ThinkPad-E455 systemd[1]: MysqL.service: Control process exited,code=exited status=1 12月 17 23:22:26 ThinkPad-E455 systemd[1]: Failed to start MysqL Community Server. -- Subject: Unit MysqL.service has Failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit MysqL.service has Failed. -- -- The result is Failed. 12月 17 23:22:26 ThinkPad-E455 systemd[1]: MysqL.service: Unit entered Failed state. 12月 17 23:22:26 ThinkPad-E455 systemd[1]: MysqL.service: Failed with result 'exit-code'. 12月 17 23:22:27 ThinkPad-E455 systemd[1]: MysqL.service: Service hold-off time over,scheduling restart. 12月 17 23:22:27 ThinkPad-E455 systemd[1]: Stopped MysqL Community Server. -- Subject: Unit MysqL.service has finished shutting down -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit MysqL.service has finished shutting down. 12月 17 23:22:27 ThinkPad-E455 systemd[1]: MysqL.service: Start request repeated too quickly. 12月 17 23:22:27 ThinkPad-E455 systemd[1]: Failed to start MysqL Community Server. -- Subject: Unit MysqL.service has Failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit MysqL.service has Failed. -- -- The result is Failed.
查看/usr/share/MysqL/MysqL-systemd-start文件
sanity () { if [ ! -r /etc/MysqL/my.cnf ]; then echo "MysqL configuration not found at /etc/MysqL/my.cnf. Please create one." exit 1 fi if [ ! -d /var/lib/MysqL ] && [ ! -L /var/lib/MysqL ]; then echo "MysqL data dir not found at /var/lib/MysqL. Please create one." exit 1 fi if [ ! -d /var/lib/MysqL/MysqL ] && [ ! -L /var/lib/MysqL/MysqL ]; then echo "MysqL system database not found. Please run MysqL_install_db tool." exit 1 fi }
原来这里的路径还是以前的,于是定义个新变量,值为新路径,将设计原路径的统统修改
sanity () { if [ ! -r /etc/MysqL/my.cnf ]; then echo "MysqL configuration not found at /etc/MysqL/my.cnf. Please create one." exit 1 fi datadir=/webdata/db/MysqL sysdbdir=$datadir/MysqL if [ ! -d $datadir ] && [ ! -L $datadir ]; then echo "MysqL data dir not found at $datadir. Please create one." exit 1 fi if [ ! -d $sysdbdir ] && [ ! -L $sysdbdir ]; then echo "MysqL system database $sysdatadir not found. Please run MysqL_install_db tool." exit 1 fi }
启动成功后从error.log中找到初始化时生产的密码,
2016-12-17T14:56:46.781127Z 1 [Note] A temporary password is generated for root@localhost: xxxxxx
用此登录即可