在本文中,我们将学习如何更改Mysql数据目录或将mysql数据库数据重新定位到新位置,这种情况可能会在数据库增长非常快时使用,或者出于某些安全原因我们想要移动将数据目录复制到新位置。先决条件一台 ubuntu 计算机,且非 root 用户
在本文中,我们将学习如何更改Mysql数据目录或将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
此外,我们需要编辑 /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 数据目录重新定位到新位置,这将有助于我们保护或将更多数据存储到不同的位置。
--结束END--
本文标题: 如何在 Ubuntu 16.04 上将 MySQL 数据目录更改为另一个位置
本文链接: https://lsjlt.com/news/436375.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0