putin1223
putin1223
采纳率0%
2018-06-23 03:10 浏览 1.8k

Ubuntu16.04 修改mysql 数据存储目录datadir 启动失败

5

Ubuntu16.04 修改mysql 数据存储目录datadir 启动失败 ,网上的各种方法都已经尝试,但是还是没有启动成功请教各位大神!!!!!

修改步骤

创建 迁移文件夹

cd /mnt
mkdir lib
cd lib && mkdir mysqldata
数据存放在 /mnt/lib/mysqldata

修改所属用户 和所属用户组为 msyql

sudo chown -vR mysql:mysql /mnt/lib/mysqldata

修改权限

sudo chmod -vR 700 /mnt/lib/mysqldata

迁移文件

停止服务

sudo /etc/init.d/mysql stop

迁移数据

cp -av /var/lib/mysql/* /mnt/lib/mysqldata

vim /etc/mysql/mysql.conf.d/mysqld.cnf

将 [mysqld] 组下的 datadir改为:
datadir = /mnt/lib/mysqldata

sudo vim /etc/apparmor.d/usr.sbin.mysqld

找到其中的

/var/lib/mysql/ r,
/var/lib/mysql/** rwk,

两行权限声明,可以在前面加上#好注释掉。然后对照格式,加入新路径的权限声明:
/mnt/lib/mysqldata/ r,
/mnt/lib/mysqldata/** rwk

重启服务
配置文件修改成功后就可以重启数据库,重启数据库之前需要先重新载入apparmor配置文件,使用下面命令重新载入:

sudo /etc/init.d/apparmor restart

sudo /etc/init.d/mysql start

重启的时候
Starting mysql (via systemctl): mysql.serviceJob for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.
failed!
root@iZm5e472vz1trxejt8m5akZ:/etc/mysql# systemctl status mysql.service
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-06-23 00:55:37 CST; 25s ago
Process: 13418 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 13408 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 13417 (mysqld)
CGroup: /system.slice/mysql.service
└─13417 /usr/sbin/mysqld

. 配置文件 内容

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
#
# * Basic Settings
#
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
# datadir         = /var/lib/mysql
datadir     = /mnt/lib/mysqldata
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 0.0.0.0
#
# * Fine Tuning
#
key_buffer_size     = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries   = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size   = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • qq_41486972 qq_41486972 2018-06-23 11:21

    主要是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重启服务。

    点赞 评论 复制链接分享

相关推荐