How To Move a MySQL Data Directory to a New Location on Ubuntu 16.04

16

How To Move a MySQL Data Directory to a New Location on Ubuntu 16.04

PostedJuly 21, 2016 62.1kviews MYSQL BLOCK STORAGE STORAGE UBUNTU UBUNTU 16.04

Introduction

Databases grow over time, sometimes outgrowing the space on the file system. You can also run into I/O contention when they’re located on the same partition as the rest of the operating system. RAID, network block storage, and other devices can offer redundancy and other desirable features. Whether you’re adding more space, evaluating ways to optimize performance, or looking to take advantage of other storage features, this tutorial will guide you through relocating MySQL’s data directory.

Prerequisites

To complete this guide, you will need:

In this example, we’re moving the data to a block storage device mounted at /mnt/volume-nyc1-01. You can learn how to set one up in the How To Use Block Storage on DigitalOcean guide.

No matter what underlying storage you use, this guide can help you move the data directory to a new location.

Step 1 — Moving the MySQL Data Directory

To prepare for moving MySQL’s data directory, let’s verify the current location by starting an interactive MySQL session using the administrative credentials.

  • mysql -u root -p

When prompted, supply the MySQL root password. Then from the MySQL prompt, select the data directory:

  • select @@datadir;

Output

+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+
1 row in set (0.00 sec)

This output confirms that MySQL is configured to use the default data directory, /var/lib/mysql/, so that’s the directory we need to move. Once you‘ve confirmed this, type exit to leave the monitor.

To ensure the integrity of the data, we’ll shut down MySQL before we actually make changes to the data directory:

  • sudo systemctl stop mysql

systemctl doesn‘t display the outcome of all service management commands, so if you want to be sure you‘ve succeeded, use the following command:

  • sudo systemctl status mysql

You can be sure it’s shut down if the final line of the output tells you the server is stopped:

Output

. . .
Jul 18 11:24:20 ubuntu-512mb-nyc1-01 systemd[1]: Stopped MySQL Community Server.

Now that the server is shut down, we’ll copy the existing database directory to the new location withrsync. Using the -a flag preserves the permissions and other directory properties, while-v provides verbose output so you can follow the progress.

Note: Be sure there is no trailing slash on the directory, which may be added if you use tab completion. When there’s a trailing slash, rsync will dump the contents of the directory into the mount point instead of transferring it into a containing mysql directory:

  • sudo rsync -av /var/lib/mysql /mnt/volume-nyc1-01

Once the rsync is complete, rename the current folder with a .bak extension and keep it until we’ve confirmed the move was successful. By re-naming it, we’ll avoid confusion that could arise from files in both the new and the old location:

  • sudo mv /var/lib/mysql /var/lib/mysql.bak

Now we’re ready to turn our attention to configuration.

Step 2 — Pointing to the New Data Location

MySQL has several ways to override configuration values. By default, the datadir is set to/var/lib/mysql in the /etc/mysql/mysql.conf.d/mysqld.cnf file. Edit this file to reflect the new data directory:

  • sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Find the line that begins with datadir= and change the path which follows to reflect the new location.

In our case, the updated file looks like the output below:

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

. . .
datadir=/mnt/volume-nyc1-01/mysql
. . .

This seems like the right time to bring up MySQL again, but there’s one more thing to configure before we can do that successfully.

Step 3 — Configuring AppArmor Access Control Rules

We’ll need to tell AppArmor to let MySQL write to the new directory by creating an alias between the default directory and the new location. To do this, edit the AppArmor alias file:

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

At the bottom of the file, add the following alias rule:

/etc/apparmor.d/tunables/alias

. . .
alias /var/lib/mysql/ -> /mnt/volume-nyc1-01/mysql/,
. . .

For the changes to take effect, restart AppArmor:

  • sudo systemctl restart apparmor

Note: If you skipped the AppArmor configuration step, you would run into the following error message:

Output

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

The output from both systemctl and journalctl concludes with:

Output

Jul 18 11:03:24 ubuntu-512mb-nyc1-01 systemd[1]:
mysql.service: Main process exited, code=exited, status=1/FAILURE

Since the messages don’t make an explicit connection between AppArmor and the data directory, this error can take some time to figure out.

Step 4 — Restarting MySQL

The next step is to start MySQL, but if you do, you’ll run into another error. This time, instead of an AppArmor issue, the error happens because the script mysql-systemd-start checks for the existence of either a directory, -d, or a symbolic link, -L, that matches two default paths. It fails if they‘re not found:

/usr/share/mysql/mysql-systemd-start

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

. . .

Since we need these to start the server, we will create the minimal directory structure to pass the script‘s environment check.

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

Now we‘re ready to start MySQL.

  • sudo systemctl start mysql
  • sudo systemctl status mysql

To make sure that the new data directory is indeed in use, start the MySQL monitor.

  • mysql -u root -p

Look at the value for the data directory again:

Output

+----------------------------+
| @@datadir                  |
+----------------------------+
| /mnt/volume-nyc1-01/mysql/ |
+----------------------------+
1 row in set (0.01 sec)

Now that you’ve restarted MySQL and confirmed that it’s using the new location, take the opportunity to ensure that your database is fully functional. Once you’ve verified the integrity of any existing data, you can remove the backup data directory:

  • sudo rm -Rf /var/lib/mysql.bak

Restart MySQL one final time to be sure that it works as expected:

  • sudo systemctl restart mysql
  • sudo systemctl status mysql

Conclusion

In this tutorial, we’ve moved MySQL’s data directory to a new location and updated Ubuntu’s AppArmor ACLs to accommodate the adjustment. Although we were using a Block Storage device, the instructions here should be suitable for redefining the location of the data directory regardless of the underlying technology.

For more on managing MySQL’s data directories, see these sections in the official MySQL documentation:

时间: 2024-09-30 18:37:50

How To Move a MySQL Data Directory to a New Location on Ubuntu 16.04的相关文章

启动mysql 失败,“Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' ”

一.Mac OS X的升级或其他原因可能会导致MySQL启动或开机自动运行时 在MySQL操作面板上会提示“Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' ”, 这应该是某种情况下导致/usr/local/mysql/data的宿主发生了改变, 只需要运行“sudo chown -R mysql /usr/local/mysql/data”即可 mac 下面运行 “sudo c

启动 mysql 失败 Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'

Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' 这应该是某种情况下导致/usr/local/mysql/data的宿主发生了改变. 解决方法:打开终端运行 sudo chown -R mysql /usr/local/mysql/data 即可. mac 下运行  sudo chown -R  _mysql:wheel  /usr/local/mysql/data . -c 显

Ubuntu 16.04 LTS 安装 Nginx/PHP 5.6/MySQL 5.7 (LNMP) 与Laravel

1.MySQL安装[安装 MariaDB]MariaDB是MySQL的一个分支首先,更新升级系统$ sudo apt update$ sudo apt upgrade安装MariaDB:$ sudo apt install mariadb-server启动MariaDB服务:$ sudo systemctl start mysql$ sudo systemctl enable mysql查看状态:$ sudo systemctl status mysql 为例提高MariaDB的安全,我们可以执

Ubuntu 16.04 设置MySQL远程访问权限

本文记录一下在Ubuntu 16.04版本下设置MySQL数据库的远程访问. 第一步:修改配置文件的端口绑定 打开的目录可能会根据MySQL的版本稍有不同,可以先尝试打开/etc/mysql/my.cnf这个配置文件,若该文件不存在或文件内容为空,则尝试下面的文件路径. sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 在下面行的开头加上#,注释掉该行,然后保存退出vim: bind-address = 127.0.0.1 第二步:修改访问权限 进入mysq

Ubuntu 16.04 LAMP server 指南 - 配置 Apache2.4,PHP7,和MariaDB(而不是MySQL)

翻译自:https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ 昨天在虚拟机里面安装ubuntu server ,然后配置php开发环境,参考了这篇文章,一次性把所有的东西都安装配置好了,所以想把这篇文章记录下来.希望能够帮助到初学者一次性搞定这些配置(避免纠结),然后就可以愉快地编程了,嘿嘿. 以下是我翻译的内容,完全对照原文,没有自己改动的部分(因为原文已经很完美了

Ubuntu 16.04 安装Mysql 5.7 踩坑小记

title:Ubuntu 16.04 安装Mysql 5.7 踩坑小记 date: 2018.02.03 安装mysql sudo apt-get install mysql-server mysql-client 测试是否安装成功 sudo netstat -tap | grep mysql 相关操作 登录 mysql -uroot -p 检查MySQL服务器占用端口 netstat -nlt|grep 3306 检查MySQL服务器系统进程 ps -aux|grep mysql 查看数据库的

Ubuntu 16.04下安装MySQL详解

Ubuntu 16.04下安装MySQL详解分别依次输入以下3个命令: sudo apt-get install mysql-server sudo apt install mysql-client sudo apt install libmysqlclient-dev 安装成功后可以通过下面的命令测试是否安装成功: sudo netstat -tap | grep mysql 出现如下信息证明安装成功: >>> sudo netstat -tap | grep mysql tcp 0

ubuntu 16.04 和 windows 10系统安装mysql 允许远程访问 | mysql user guide on ubuntu 16.04 and windows 10

本文首发于个人博客https://kezunlin.me/post/36e618e7/,欢迎阅读! mysql user guide on ubuntu 16.04 and windows 10 Part-1: Ubuntu install sudo apt-get install mysql-server # root,123456 mysql -uroot -p123456 allow remote access change bind-address cd /etc/mysql grep

Ubuntu 16.04安装MySQL(5.7.18)

此篇为http://www.cnblogs.com/EasonJim/p/7139275.html的分支页. 安装MySQL前需要做如下了解: 1.MySQL各类型版本的区别,参考:http://www.cnblogs.com/EasonJim/p/6274344.html 2.官方的下载地址一般指向最新的版本下载,如果要下载以前的版本,比如5.5.x的版本,有特殊入口,参考:http://www.cnblogs.com/EasonJim/p/7147134.html 3.随着时间的推移,教程上