如何從MySQL遷移到MariaDB

如何從MySQL遷移到MariaDB

如果你正在考慮從MySQL遷移到MariaDB,有充分的理由進行轉換。作為最早的資料庫管理系統(DBMS)平臺之一,MySQL在行業中佔據了很長一段時間,並且仍然具有顯著的相關性。

然而,由於其處理速度較慢,處理複雜資料的效率低下,企業正在慢慢遠離它。其中許多組織轉向了MariaDB。

MariaDB是一個較新的、輕量級的DBMS,它提供了更好的效能、更前沿的功能,以及處理複雜資料集的更大能力。與MySQL相比,MariaDB還擁有一個更有活力的社羣,保持其公共資源庫的更新和創新變化。

本文將指導你將你的資料從MySQL遷移到MariaDB。

  1. 如何將資料庫從MySQL遷移到MariaDB
  2. 從MySQL匯出資料
  3. 將資料匯入MariaDB
  4. 如何更新你的WordPress網站

如何將資料庫從MySQL遷移到MariaDB

從MySQL遷移到MariaDB簡單得令人難以置信。你可以把整個過程分解成兩個重要的步驟:

  1. 通過轉儲到一個SQL檔案來備份你在MySQL中的資料庫
  2. 登入到你的MariaDB伺服器,載入備份檔案以建立資料庫。

MariaDB與MySQL是相容的,所以你的資料庫遷移不會有問題。

在將你的資料庫從MySQL遷移到MariaDB並交叉檢查表是否完整後,你可以更新你的網站,從MariaDB而不是MySQL中提取資料。

要求

既然你想從MySQL遷移到MariaDB,你應該已經有一個MySQL資料庫,其中包含一些你想遷移的資料。

當然,你也應該有一個MariaDB的副本。雖然不是必須的,但本教學使用phpMyAdmin,這是一個開源的視覺化工具,可以讓你使用網路瀏覽器管理MySQL和MariaDB資料庫。

phpMyAdmin

phpMyAdmin

要訪問phpMyAdmin,請安裝WAMPServerXAMPP。這些伺服器包預裝了phpMyAdmin、MySQL和MariaDB–你在本教學中需要的一切。(XAMPP可以在Windows、macOS和Linux上執行)。

如果你沒有phpMyAdmin,不要擔心。這篇文章包括了你可以在終端上執行的命令,以便在MySQL和MariaDB上覆制同樣的操作。

從MySQL匯出資料

首先在MySQL中為你的目標資料庫建立一個備份。啟動你的MySQL伺服器,使用phpMyAdmin登入到你的MySQL資料庫。從WAMP或XAMPP,你可以通過導航到http://localhost/phpMyAdmin/,訪問phpMyAdmin。

phpMyAdmin登入頁面

phpMyAdmin登入頁面

一旦進入MySQL環境,點選你要備份的資料庫。這個例子匯出一個WordPress資料庫,它包含典型的WordPress表。

phpMyAdmin上的WordPress資料庫

phpMyAdmin上的WordPress資料庫

點選表格上方的Export標籤,然後選擇SQL格式。以後你會把它匯入MariaDB。

以SQL格式匯出WordPress資料庫

以SQL格式匯出WordPress資料庫

點選 “Go“,將資料庫備份以SQL檔案的形式下載到你的本地計算機。或者,如果你沒有phpMyAdmin或者喜歡使用命令,使用下面的命令將你的資料庫轉儲為一個SQL檔案。用你的資料庫使用者名稱和密碼替換 your-name 和 your-pass 。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ mysqldump --user=your-name --password="your-pass" wordpress > wordpress.sql
$ mysqldump --user=your-name --password="your-pass" wordpress > wordpress.sql
$ mysqldump --user=your-name --password="your-pass" wordpress > wordpress.sql

在這種情況下,該命令建立了一個名為wordpress.sql的備份檔案,包含WordPress資料庫的SQL程式碼。

最後,解除安裝MySQL。

將資料匯入MariaDB

首先,確保你的本地機器上安裝了MariaDB。如果你使用WAMP、XAMPP或類似的預裝MariaDB的發行版,你就不需要手動做這個。

現在是時候將備份的MySQL檔案載入到MariaDB中了。從你的伺服器管理面板上停止MySQL伺服器。退出MySQL伺服器,進入你的MariaDB伺服器(在登入phpMyAdmin時從MySQL切換到MariaDB即可)。

phpMyAdmin在MariaDB伺服器上的登入頁面

phpMyAdmin在MariaDB伺服器上的登入頁面

在管理面板上,建立一個新的資料庫。你可以在phpMyAdmin中點選New,給你的資料庫一個名字,然後點選Create

使用MariaDB在phpMyAdmin上的WordPress資料庫

使用MariaDB在phpMyAdmin上的WordPress資料庫

點選你剛剛建立的新資料庫,然後點選頂部的Import標籤,通過點選Choose FileBrowse選項匯入備份檔案。

匯入WordPress資料庫

匯入WordPress資料庫

點選 “Go” 來載入檔案。這個過程可能需要一段時間,如果一切順利,phpMyAdmin會通知你查詢成功。

成功資訊

成功資訊

如果你想用命令列來代替,請按照以下步驟操作。

登入到你的MariaDB伺服器,按如下步驟建立新的資料庫:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ mysql --user=your-name --password="your-pass" -e "CREATE DATABASE wordpress";
$ mysql --user=your-name --password="your-pass" -e "CREATE DATABASE wordpress";
$ mysql --user=your-name --password="your-pass" -e  "CREATE DATABASE wordpress";

將備份檔案載入到MariaDB。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ mysql --user=your-name --password="your-pass" --database=wordpress < wordpress.sql
$ mysql --user=your-name --password="your-pass" --database=wordpress < wordpress.sql
$ mysql --user=your-name --password="your-pass" --database=wordpress < wordpress.sql

一旦你成功地從MySQL匯入檔案到MariaDB,遊標將再次變得活躍。

如何更新你的WordPress網站

從MySQL轉移到MariaDB之後,是時候讓你的WordPress網站開始使用新的資料庫了。要做到這一點,你只需要用新的資料庫細節更新你的網站的wp-config.php檔案:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// ** MariaDB settings** //
define('DB_NAME', 'database_name_here');
define('DB_USER', 'database_username_here');
define('DB_PASSWORD', 'database_password_here');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
// ** MariaDB settings** // define('DB_NAME', 'database_name_here'); define('DB_USER', 'database_username_here'); define('DB_PASSWORD', 'database_password_here'); define('DB_HOST', 'localhost'); define('DB_CHARSET', 'utf8'); /** The Database Collate type. Don't change this if in doubt. */ define('DB_COLLATE', '');
// ** MariaDB settings** //
define('DB_NAME', 'database_name_here');
define('DB_USER', 'database_username_here');
define('DB_PASSWORD', 'database_password_here');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

在你儲存該檔案後,WordPress網站將開始從你的新資料庫中提取資料。

更新WordPress網站

更新WordPress網站

小結

從MySQL遷移到MariaDB是一個簡單的過程。簡而言之,你需要備份你的資料庫並解除安裝MySQL,然後安裝MariaDB並匯入你的資料庫備份。

記住,在從MySQL遷移到MariaDB時,你可能會遇到一些問題。例如,如果MySQL的模式與MariaDB的模式不匹配,你可能會得到一個錯誤。另外,在從一個版本遷移到另一個版本時,確保你執行 mysql_upgrade 。在大多數情況下,解決遷移問題的方法是在再次嘗試之前將兩個資料庫升級到它們的最新版本。

隨著MariaDB的不斷創新,它與MySQL在基本層面的相容性可能會降低。因此,如果你正在使用MySQL,並考慮遷移到MariaDB以獲得額外的好處,我們建議儘早這樣做,而不是以後。

評論留言