Ubuntu16.04 failed to modify mysql data storage directory datadir startup

Ubuntu16.04 modified mysql data storage directory datadir failed to start, all kinds of methods on the Internet have been tried, but still failed to start successfully to consult the gods!

modify steps

< H2 > create a migration folder < / H2 >
> cd /mnt 
> mkdir lib 
> cd lib && mkdir mysqldata
>  /mnt/lib/mysqldata

modify the user and user group to msyql

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

modify permissions

> sudo chmod -vR  700 /mnt/lib/mysqldata

migrate files

stop the service

>  sudo /etc/init.d/mysql stop  

migrate data

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

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

change datadir under [mysqld] group to:

datadir = /mnt/lib/mysqldata

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

find one of them

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

two lines of permission declaration, which can be commented out by adding-sharp in front of it. Then, against the format, add the permission declaration of the new path:

  /mnt/lib/mysqldata/ r,
  /mnt/lib/mysqldata/** rwk

restart the service

you can restart the database after the configuration file has been modified successfully. Before restarting the database, you need to reload the apparmor configuration file. Use the following command to reload:

> sudo /etc/init.d/apparmor restart 
>sudo /etc/init.d/mysql start  

when restarting

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-sharp 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
           
 . 

contents of configuration file

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

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

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

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

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