目錄
先決條件
首頁 資料庫 mysql教程 如何在 Ubuntu 16.04 上將 MySQL 資料目錄變更為另一個位置

如何在 Ubuntu 16.04 上將 MySQL 資料目錄變更為另一個位置

Aug 31, 2023 pm 09:33 PM

如何在 Ubuntu 16.04 上将 MySQL 数据目录更改为另一个位置

在本文中,我們將學習如何更改MySQL資料目錄或將MySQL資料庫資料重新定位到新位置,這種情況可能會在資料庫成長非常快時使用,或者出於某些安全性原因我們想要移動將資料目錄複製到新位置。

先決條件

  • 一台 Ubuntu 計算機,且非 root 使用者俱有 Sudo 權限。
  • 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 資料目錄重新定位到新位置,這將有助於我們保護或將更多資料儲存到不同的位置。

以上是如何在 Ubuntu 16.04 上將 MySQL 資料目錄變更為另一個位置的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1666
14
CakePHP 教程
1425
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1253
24
MySQL的角色:Web應用程序中的數據庫 MySQL的角色:Web應用程序中的數據庫 Apr 17, 2025 am 12:23 AM

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

說明InnoDB重做日誌和撤消日誌的作用。 說明InnoDB重做日誌和撤消日誌的作用。 Apr 15, 2025 am 12:16 AM

InnoDB使用redologs和undologs確保數據一致性和可靠性。 1.redologs記錄數據頁修改,確保崩潰恢復和事務持久性。 2.undologs記錄數據原始值,支持事務回滾和MVCC。

MySQL的位置:數據庫和編程 MySQL的位置:數據庫和編程 Apr 13, 2025 am 12:18 AM

MySQL在數據庫和編程中的地位非常重要,它是一個開源的關係型數據庫管理系統,廣泛應用於各種應用場景。 1)MySQL提供高效的數據存儲、組織和檢索功能,支持Web、移動和企業級系統。 2)它使用客戶端-服務器架構,支持多種存儲引擎和索引優化。 3)基本用法包括創建表和插入數據,高級用法涉及多表JOIN和復雜查詢。 4)常見問題如SQL語法錯誤和性能問題可以通過EXPLAIN命令和慢查詢日誌調試。 5)性能優化方法包括合理使用索引、優化查詢和使用緩存,最佳實踐包括使用事務和PreparedStatemen

MySQL與其他編程語言:一種比較 MySQL與其他編程語言:一種比較 Apr 19, 2025 am 12:22 AM

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。MySQL以其高性能、可扩展性和跨平台支持著称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

MySQL:從小型企業到大型企業 MySQL:從小型企業到大型企業 Apr 13, 2025 am 12:17 AM

MySQL適合小型和大型企業。 1)小型企業可使用MySQL進行基本數據管理,如存儲客戶信息。 2)大型企業可利用MySQL處理海量數據和復雜業務邏輯,優化查詢性能和事務處理。

MySQL索引基數如何影響查詢性能? MySQL索引基數如何影響查詢性能? Apr 14, 2025 am 12:18 AM

MySQL索引基数对查询性能有显著影响:1.高基数索引能更有效地缩小数据范围,提高查询效率;2.低基数索引可能导致全表扫描,降低查询性能;3.在联合索引中,应将高基数列放在前面以优化查询。

初學者的MySQL:開始數據庫管理 初學者的MySQL:開始數據庫管理 Apr 18, 2025 am 12:10 AM

MySQL的基本操作包括創建數據庫、表格,及使用SQL進行數據的CRUD操作。 1.創建數據庫:CREATEDATABASEmy_first_db;2.創建表格:CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY,titleVARCHAR(100)NOTNULL,authorVARCHAR(100)NOTNULL,published_yearINT);3.插入數據:INSERTINTObooks(title,author,published_year)VA

MySQL與其他數據庫:比較選項 MySQL與其他數據庫:比較選項 Apr 15, 2025 am 12:08 AM

MySQL適合Web應用和內容管理系統,因其開源、高性能和易用性而受歡迎。 1)與PostgreSQL相比,MySQL在簡單查詢和高並發讀操作上表現更好。 2)相較Oracle,MySQL因開源和低成本更受中小企業青睞。 3)對比MicrosoftSQLServer,MySQL更適合跨平台應用。 4)與MongoDB不同,MySQL更適用於結構化數據和事務處理。

See all articles