红联Linux门户
Linux帮助

ubuntu16.04 mysql修改datadir的折腾

发布时间:2016-12-18 10:54:59来源:linux网站作者:graphnj
回顾:
主要是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  
 
参考:
ubuntu16.04 mysql修改datadir的折腾
了解到是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
用此登录即可。
 
本文永久更新地址:http://www.linuxdiyf.com/linux/27010.html