如何在 Ubuntu 16.04 上将 MySQL 数据目录更改到另一个位置
在本文中,我们将学习如何更改 MySQL 数据目录或将 MySQL 数据库数据迁移到新位置。当数据库增长速度非常快,或出于安全原因需要将数据目录移动到新位置时,可能会用到这种情况。
先决条件
- 一台具有 Sudo 权限的非 root 用户的 Ubuntu 机器。
- 已安装并运行的 MySQL。
- 一个新的卷或位置,我们希望将数据库数据位置移动到该位置,新的位置将是 /mnt/data_vol/MySQL,其中 data_vol 是连接到机器或服务器的新卷。
更改 MySQL 数据文件夹位置
在继续之前,我们将首先找到数据目录的当前位置。
$ mysql –u root –p Output: Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 472 Server version: 5.6.30-0ubuntu0.14.04.1 (Ubuntu) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
当提示输入 MySQL 的 root 密码时,请输入密码。运行以下命令以了解 MySQL 的当前工作数据目录。
Mysql> select @@datadir; Output: +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set (0.00 sec)
输出将显示 MySQL 数据库使用 /var/lib/mysql 作为默认文件夹作为数据目录。在修改任何内容之前,我们将检查数据的完整性,我们将停止 MySQL 并检查状态。
$ sudo systemctl stop mysql
由于 systemctl 不会为 services 命令显示任何内容
$ sudo systemctl status mysql Output: mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: inactive (dead) since Mon 2016-09-12 13:57:43 IST; 1s ago Process: 17669 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS) Process: 17668 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS) Process: 17664 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 17668 (code=exited, status=0/SUCCESS) Sep 12 13:55:14 ubuntu-16 systemd[1]: Starting MySQL Community Server... Sep 12 13:55:15 ubuntu-16 systemd[1]: Started MySQL Community Server. Sep 12 13:57:40 ubuntu-16 systemd[1]: Stopping MySQL Community Server... Sep 12 13:57:43 ubuntu-16 systemd[1]: Stopped MySQL Community Server.
一旦我们确认 MySQL 已停止,我们将数据移动到新位置。要移动数据,我们将使用带有 –a 选项的 Rsync,该选项保留数据文件的权限,并使用 –v 选项显示详细输出。
以下是将数据移动到新位置的完整命令:
$ rsync –av /var/lib/mysql /mnt/data_vol/ OutPut: sending incremental file list mysql/ mysql/auto.cnf mysql/debian-5.7.flag mysql/ib_buffer_pool mysql/ib_logfile0 mysql/ib_logfile1 mysql/ibdata1 mysql/mysql/ mysql/mysql/columns_priv.MYD mysql/mysql/columns_priv.MYI mysql/mysql/columns_priv.frm mysql/mysql/db.MYD mysql/mysql/db.MYI mysql/mysql/db.frm mysql/mysql/db.opt …. mysql/sys/x@0024user_summary.frmmysql/sys/x@0024user_summary_by_file_io.frm mysql/sys/x@0024user_summary_by_file_io_type.frm mysql/sys/x@0024user_summary_by_stages.frm mysql/sys/x@0024user_summary_by_statement_latency.frm mysql/sys/x@0024user_summary_by_statement_type.frm mysql/sys/x@0024wait_classes_global_by_avg_latency.frm mysql/sys/x@0024wait_classes_global_by_latency.frm mysql/sys/x@0024waits_by_host_by_latency.frm mysqlsys//x@0024waits_by_user_by_latency.frm mysql/sys/x@0024waits_global_by_latency.frm sent 199,384,083 bytes received 6,858 bytes 132,927,294.00 bytes/sec total size is 199,307,568 speedup is 1.00
Rsync 成功将数据文件夹移动到新位置后。出于安全原因,我们将保留数据文件夹,直到确认数据位于新位置,我们将当前数据目录从 /var/lib/mysql 重命名为 /var/lib/mysql_backup。以下是更改当前数据目录的命令。
以下是更改当前数据目录的命令:
$ sudo mv /var/lib/mysql /var/lib/mysql_backup
现在,我们将更改默认数据目录,更改的方法有很多,但我们将编辑位于 /etc/mysql/mysql.conf.d/mysqld.cnf 中的 mysqld.cnf 文件。
要编辑 mysqld.cnf,以下是命令:
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf Output: [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 = /mnt/data_vol/mysql/ tmpdir = /tmp lc-messages-dir = /usr/share/mysql skip-external-locking
更改 Apparmor 别名设置
此外,我们需要编辑 /etc/apparmor.d/tunables/alias。
在文件底部,我们需要在别名规则中添加以下几行。
$ sudo vi /etc/apparmor.d/tunables/alias Output: # ------------------------------------------------------------------ # # 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/ -> /home/mysql/, alias /var/lib/mysql/ -> /mnt/data_vol/mysql
编辑文件后,我们需要重新启动 apparmor。
以下是重新启动 apparmor 的命令:
由于我们已更改默认数据目录,因此我们需要运行以下命令,该命令将创建最小的目录文件夹结构以传递脚本环境。
$ sudo mkdir /var/lib/mysql/mysql –p
现在我们将重新启动 mysql 服务。
$ sudo systemctl start mysql
现在我们将使用以下命令检查 MySQL 服务的状态
$ sudo systemctl status mysql Output: mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2016-09-12 14:17:27 IST; 23s ago Process: 18481 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCC Process: 18477 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCES Main PID: 18480 (mysqld) Tasks: 28 (limit: 512) Memory: 137.3M CPU: 329ms CGroup: /system.slice/mysql.service └─18480 /usr/sbin/mysqld Sep 12 14:17:26 ubuntu-16 systemd[1]: Starting MySQL Community Server... Sep 12 14:17:27 ubuntu-16 systemd[1]: Started MySQL Community Server.
为了确保已更改新的数据目录,我们将运行以下命令
$ mysql -uroot -p Output: Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.13-0ubuntu0.16.04.2 (Ubuntu) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select @@datadir +----------------------+ | @@datadir | +----------------------+ | /mnt/data_vol/mysql/ | +-----------------+ 1 row in set (0.00 sec) mysql>
一旦我们确认数据目录已更改,我们将删除位于 /var/lib/mysql_backup 的默认数据目录,以下是删除旧数据库目录的命令。
$ sudo rm –rf /var/lib/mysql_backup
在上述配置和步骤中,我们学习了如何将 MySQL 数据目录迁移到新位置,这将帮助我们保护数据或将更多数据存储到不同的位置。