Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 July MySQL: Move Data Directory to a New Location on Ubuntu 16.04

MySQL: Move Data Directory to a New Location on Ubuntu 16.04

MySQL Move Data Dir

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing required steps to move MySQL Data Directory from old location to new location.

Check the current data directory of MySQL:

1
2
3
4
5
6
mysql> select @@datadir;
+-----------------+
| @@datadir |
+-----------------+
| /var/lib/mysql/ |
+-----------------+

Stop the MySQL Service:

1
sudo systemctl stop mysql

Confirm the inactive status of MySQL Service:

1
sudo systemctl status mysql
Output:

1
2
3
4
5
6
7
8
9
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Wed 2017-07-12 19:45:48 UTC; 11s ago
Main PID: 8841 (code=exited, status=0/SUCCESS)
 
Jul 12 19:11:30 ip-172-31-27-19 systemd[1]: Starting MySQL Community Server...
Jul 12 19:11:31 ip-172-31-27-19 systemd[1]: Started MySQL Community Server.
Jul 12 19:45:46 ip-172-31-27-19 systemd[1]: Stopping MySQL Community Server...
Jul 12 19:45:48 ip-172-31-27-19 systemd[1]: Stopped MySQL Community Server.

Use rsync with -av flag to copy the existing database directory to the new location:

1
sudo rsync -av /var/lib/mysql /mnt/dbrnd_mysql

Rename old MySQL Data Directory:

1
sudo mv /var/lib/mysql /var/lib/mysql.backup

Change (datadir=) parameter in MySQL Config file (mysqld.cnf) for pointing to the New Data Location:

1
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
My Sample mysqld.cnf:

1
2
3
4
5
6
7
8
9
10
11
12
13
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /mnt/dbrnd_mysql/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

Now, Check below sample error:

1
Job for mysql.service failed because the control process exited with error code. See "systemctl status mysql.service" and "journalctl -xe" for details.

To avoid above error, configure AppArmor Access Control Rules:

AppArmor: AppArmor (“Application”) is a Linux kernel security module that allows the system administrator to restrict programs’ capabilities with per-program profiles. Profiles can allow capabilities like network access, raw socket access, and the permission to read, write, or execute files on matching paths.

Open AppArmor for edit:

1
sudo nano /etc/apparmor.d/tunables/alias

Add new entry of new data location path:

1
alias /var/lib/mysql/ -> /mnt/volume-nyc1-01/mysql/,
Please check below sample output of my AppArmor:
Because don’t add # in new entry and also make sure that old path is there with #.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ------------------------------------------------------------------
#
# Copyright (C) 2010 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License published by the Free Software Foundation.
#
# ------------------------------------------------------------------
 
# Alias rules can be used to rewrite paths and are done after variable
# resolution. For example, if '/usr' is on removable media:
# alias /usr/ -> /mnt/usr/,
#
# Or if mysql databases are stored in /home:
# alias /var/lib/mysql/ -> /var/lib/mysql/,
alias /var/lib/mysql/ -> /mnt/dbrnd_mysql/mysql/,

After change, restart the AppArmor:

1
sudo systemctl restart apparmor

Create the minimal directory structure:

1
sudo mkdir /var/lib/mysql/mysql -p

Now, Start the MySQL Service:

1
sudo systemctl start mysql

Check the status of MySQL Service:

1
sudo systemctl status mysql

Now, connect MySQL and check the new datadir location:

1
2
3
4
5
6
mysql> select @@datadir;
+-------------------------+
| @@datadir |
+-------------------------+
| /mnt/dbrnd_mysql/mysql/ |
+-------------------------+

Jul 24, 2017Anvesh Patel
MySQL: How to Install MySQL 5.7 on Ubuntu 16.04?MySQL: How to change the default name of root User?
Comments: 2
  1. saeed
    January 5, 2018 at 5:48 pm

    I tried all posiiblesolution but same error with error log,please help me am stuck,your an expert and iam newbie to servers and
    thanks in adavance:
    Job for mysql.service failed because the control process exited with error code. See “systemctl status mysql.service” and “journalctl -xe” for details.

    root@CampuseraDroplet:~# tail -30 /var/log/mysql/error.log
    2018-01-05T11:56:22.047595Z 0 [Note] Shutting down plugin ‘INNODB_FT_DELETED’
    2018-01-05T11:56:22.047599Z 0 [Note] Shutting down plugin ‘INNODB_FT_DEFAULT_STO PWORD’
    2018-01-05T11:56:22.047602Z 0 [Note] Shutting down plugin ‘INNODB_METRICS’
    2018-01-05T11:56:22.047605Z 0 [Note] Shutting down plugin ‘INNODB_TEMP_TABLE_INF O’
    2018-01-05T11:56:22.047608Z 0 [Note] Shutting down plugin ‘INNODB_BUFFER_POOL_ST ATS’
    2018-01-05T11:56:22.047611Z 0 [Note] Shutting down plugin ‘INNODB_BUFFER_PAGE_LR U’
    2018-01-05T11:56:22.047615Z 0 [Note] Shutting down plugin ‘INNODB_BUFFER_PAGE’
    2018-01-05T11:56:22.047618Z 0 [Note] Shutting down plugin ‘INNODB_CMP_PER_INDEX_ RESET’
    2018-01-05T11:56:22.047621Z 0 [Note] Shutting down plugin ‘INNODB_CMP_PER_INDEX’
    2018-01-05T11:56:22.047625Z 0 [Note] Shutting down plugin ‘INNODB_CMPMEM_RESET’
    2018-01-05T11:56:22.047628Z 0 [Note] Shutting down plugin ‘INNODB_CMPMEM’
    2018-01-05T11:56:22.047649Z 0 [Note] Shutting down plugin ‘INNODB_CMP_RESET’
    2018-01-05T11:56:22.047653Z 0 [Note] Shutting down plugin ‘INNODB_CMP’
    2018-01-05T11:56:22.047656Z 0 [Note] Shutting down plugin ‘INNODB_LOCK_WAITS’
    2018-01-05T11:56:22.047659Z 0 [Note] Shutting down plugin ‘INNODB_LOCKS’
    2018-01-05T11:56:22.047661Z 0 [Note] Shutting down plugin ‘INNODB_TRX’
    2018-01-05T11:56:22.047664Z 0 [Note] Shutting down plugin ‘InnoDB’
    2018-01-05T11:56:22.047855Z 0 [Note] InnoDB: FTS optimize thread exiting.
    2018-01-05T11:56:22.048380Z 0 [Note] InnoDB: Starting shutdown…
    2018-01-05T11:56:22.149021Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/ mysql/ib_buffer_pool
    2018-01-05T11:56:22.149466Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 18 0105 11:56:22
    2018-01-05T11:56:23.370580Z 0 [Note] InnoDB: Shutdown completed; log sequence nu mber 2674079
    2018-01-05T11:56:23.372460Z 0 [Note] InnoDB: Removed temporary tablespace data f ile: “ibtmp1”
    2018-01-05T11:56:23.372481Z 0 [Note] Shutting down plugin ‘MEMORY’
    2018-01-05T11:56:23.372494Z 0 [Note] Shutting down plugin ‘CSV’
    2018-01-05T11:56:23.372500Z 0 [Note] Shutting down plugin ‘sha256_password’
    2018-01-05T11:56:23.372503Z 0 [Note] Shutting down plugin ‘mysql_native_password ‘
    2018-01-05T11:56:23.372914Z 0 [Note] Shutting down plugin ‘binlog’
    2018-01-05T11:56:23.373409Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      January 7, 2018 at 6:11 pm

      Are you doing with SUDO user?

      ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

ImageJuly 24, 2017 2 Comments MySQLAnvesh Patel, AppArmor, database, database research and development, datadir, dbrnd, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tuning, MySQL Query, MySQL Tips and Tricks, mysqld
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....