在 Linux 中怎样将 MySQL 迁移到 MariaDB 上

自从甲骨文收购 MySQL 后,由于甲骨文对 MySQL 的开发和维护更多倾向于闭门的立场,很多 MySQL 的开发者和用户放弃了 MySQL。在社区驱动下,促使更多人移到 MySQL 的另一个叫 MariaDB 的分支。在原有 MySQL 开发人员的带领下,MariaDB 的开发遵循开源的理念,并确保它的二进制格式与 MySQL 兼容。Linux 发行版如 Red Hat 家族(Fedora,CentOS,RHEL),Ubuntu 和 Mint,openSUSE 和 Debian 已经开始使用,并支持 MariaDB 作为 MySQL 的直接替换品。

在 Linux 中怎样将 MySQL 迁移到 MariaDB 上

如果你想要将 MySQL 中的数据库迁移到 MariaDB 中,这篇文章就是你所期待的。幸运的是,由于他们的二进制兼容性,MySQL-to-MariaDB 迁移过程是非常简单的。如果你按照下面的步骤,将 MySQL 迁移到 MariaDB 会是无痛的。

 

准备 MySQL 数据库和表

出于演示的目的,我们在做迁移之前在数据库中创建一个测试的 MySQL 数据库和表。如果你在 MySQL 中已经有了要迁移到 MariaDB 的数据库,跳过此步骤。否则,按以下步骤操作。

在终端输入 root 密码登录到 MySQL 。

  1. <span class="pln">$ mysql </span><span class="pun">-</span><span class="pln">u root </span><span class="pun">-</span><span class="pln">p </span>

创建一个数据库和表。

  1. <span class="pln">mysql</span><span class="pun">></span><span class="pln"> create database test01</span><span class="pun">;</span>
  2. <span class="pln">mysql</span><span class="pun">></span><span class="kwd">use</span><span class="pln"> test01</span><span class="pun">;</span>
  3. <span class="pln">mysql</span><span class="pun">></span><span class="pln"> create table pet</span><span class="pun">(</span><span class="pln">name varchar</span><span class="pun">(</span><span class="lit">30</span><span class="pun">),</span><span class="pln"> owner varchar</span><span class="pun">(</span><span class="lit">30</span><span class="pun">),</span><span class="pln"> species varchar</span><span class="pun">(</span><span class="lit">20</span><span class="pun">),</span><span class="pln"> sex </span><span class="kwd">char</span><span class="pun">(</span><span class="lit">1</span><span class="pun">));</span>

在表中添加一些数据。

  1. <span class="pln">mysql</span><span class="pun">></span><span class="pln"> insert into pet values</span><span class="pun">(</span><span class="str">'brandon'</span><span class="pun">,</span><span class="str">'Jack'</span><span class="pun">,</span><span class="str">'puddle'</span><span class="pun">,</span><span class="str">'m'</span><span class="pun">),(</span><span class="str">'dixie'</span><span class="pun">,</span><span class="str">'Danny'</span><span class="pun">,</span><span class="str">'chihuahua'</span><span class="pun">,</span><span class="str">'f'</span><span class="pun">);</span>

退出 MySQL 数据库.

 

备份 MySQL 数据库

下一步是备份现有的 MySQL 数据库。使用下面的 mysqldump 命令导出现有的数据库到文件中。运行此命令之前,请确保你的 MySQL 服务器上启用了二进制日志。如果你不知道如何启用二进制日志,请参阅结尾的教程说明。

  1. <span class="pln">$ mysqldump </span><span class="pun">--</span><span class="pln">all</span><span class="pun">-</span><span class="pln">databases </span><span class="pun">--</span><span class="pln">user</span><span class="pun">=</span><span class="pln">root </span><span class="pun">--</span><span class="pln">password </span><span class="pun">--</span><span class="pln">master</span><span class="pun">-</span><span class="pln">data </span><span class="pun">></span><span class="pln"> backupdb</span><span class="pun">.</span><span class="pln">sql </span>

在 Linux 中怎样将 MySQL 迁移到 MariaDB 上

现在,在卸载 MySQL 之前先在系统上备份 my.cnf 文件。此步是可选的。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">cp</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">mysql</span><span class="pun">/</span><span class="kwd">my</span><span class="pun">.</span><span class="pln">cnf </span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="kwd">my</span><span class="pun">.</span><span class="pln">cnf</span><span class="pun">.</span><span class="pln">bak </span>

 

卸载 MySQL

首先,停止 MySQL 服务。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> service mysql stop</span>

或者:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> stop mysql</span>

或:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">mysql stop </span>

然后继续下一步,使用以下命令移除 MySQL 和配置文件。

在基于 RPM 的系统上 (例如, CentOS, Fedora 或 RHEL):

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">yum</span><span class="pln"> remove mysql</span><span class="pun">*</span><span class="pln"> mysql</span><span class="pun">-</span><span class="pln">server mysql</span><span class="pun">-</span><span class="pln">devel mysql</span><span class="pun">-</span><span class="pln">libs</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">rm</span><span class="pun">-</span><span class="pln">rf </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql </span>

在基于 Debian 的系统上(例如, Debian, Ubuntu 或 Mint):

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> remove mysql</span><span class="pun">-</span><span class="pln">server mysql</span><span class="pun">-</span><span class="pln">client mysql</span><span class="pun">-</span><span class="pln">common</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> autoremove</span>
  3. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> autoclean</span>
  4. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> deluser mysql</span>
  5. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">rm</span><span class="pun">-</span><span class="pln">rf </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">mysql </span>

 

安装 MariaDB

在 CentOS/RHEL 7和Ubuntu(14.04或更高版本)上,最新的 MariaDB 已经包含在其官方源。在 Fedora 上,自19 版本后 MariaDB 已经替代了 MySQL。如果你使用的是旧版本或 LTS 类型如 Ubuntu 13.10 或更早的,你仍然可以通过添加其官方仓库来安装 MariaDB。

MariaDB 网站 提供了一个在线工具帮助你依据你的 Linux 发行版中来添加 MariaDB 的官方仓库。此工具为 openSUSE, Arch Linux, Mageia, Fedora, CentOS, RedHat, Mint, Ubuntu, 和 Debian 提供了 MariaDB 的官方仓库.

在 Linux 中怎样将 MySQL 迁移到 MariaDB 上

下面例子中,我们使用 Ubuntu 14.04 发行版和 CentOS 7 配置 MariaDB 库。

Ubuntu 14.04

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install software</span><span class="pun">-</span><span class="pln">properties</span><span class="pun">-</span><span class="pln">common</span>
  2. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> apt</span><span class="pun">-</span><span class="pln">key adv </span><span class="pun">--</span><span class="pln">recv</span><span class="pun">-</span><span class="pln">keys </span><span class="pun">--</span><span class="pln">keyserver hkp</span><span class="pun">:</span><span class="com">//keyserver.ubuntu.com:80 0xcbcb082a1bb943db</span>
  3. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> add</span><span class="pun">-</span><span class="pln">apt</span><span class="pun">-</span><span class="pln">repository </span><span class="str">'deb http://mirror.mephi.ru/mariadb/repo/5.5/ubuntu trusty main'</span>
  4. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> update</span>
  5. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">apt-get</span><span class="pln"> install mariadb</span><span class="pun">-</span><span class="pln">server </span>

CentOS 7

以下为 MariaDB 创建一个自定义的 yum 仓库文件。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">vi</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">yum</span><span class="pun">.</span><span class="pln">repos</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="typ">MariaDB</span><span class="pun">.</span><span class="pln">repo </span>

  1. <span class="pun">[</span><span class="pln">mariadb</span><span class="pun">]</span>
  2. <span class="pln">name </span><span class="pun">=</span><span class="typ">MariaDB</span>
  3. <span class="pln">baseurl </span><span class="pun">=</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//yum.mariadb.org/5.5/centos7-amd64</span>
  4. <span class="pln">gpgkey</span><span class="pun">=</span><span class="pln">https</span><span class="pun">:</span><span class="com">//yum.mariadb.org/RPM-GPG-KEY-MariaDB</span>
  5. <span class="pln">gpgcheck</span><span class="pun">=</span><span class="lit">1</span>

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">yum</span><span class="pln"> install </span><span class="typ">MariaDB</span><span class="pun">-</span><span class="pln">server </span><span class="typ">MariaDB</span><span class="pun">-</span><span class="pln">client </span>

安装了所有必要的软件包后,你可能会被要求为 MariaDB 的 root 用户创建一个新密码。设置 root 的密码后,别忘了恢复备份的 my.cnf 文件。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">cp</span><span class="pun">/</span><span class="pln">opt</span><span class="pun">/</span><span class="kwd">my</span><span class="pun">.</span><span class="pln">cnf </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">mysql</span><span class="pun">/</span>

现在启动 MariaDB 服务。

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> service mariadb start</span>

或:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> start mariadb</span>

或:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">mariadb start </span>

 

导入 MySQL 的数据库

最后,我们将以前导出的数据库导入到 MariaDB 服务器中。

  1. <span class="pln">$ mysql </span><span class="pun">-</span><span class="pln">u root </span><span class="pun">-</span><span class="pln">p </span><span class="pun"><</span><span class="pln"> backupdb</span><span class="pun">.</span><span class="pln">sql </span>

输入你 MariaDB 的 root 密码,数据库导入过程将开始。导入过程完成后,将返回到命令提示符下。

要检查导入过程是否完全成功,请登录到 MariaDB 服务器,并查看一些样本来检查。

  1. <span class="pln">$ mysql </span><span class="pun">-</span><span class="pln">u root </span><span class="pun">-</span><span class="pln">p</span>

  1. <span class="typ">MariaDB</span><span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="pln"> show databases</span><span class="pun">;</span>
  2. <span class="typ">MariaDB</span><span class="pun">[(</span><span class="pln">none</span><span class="pun">)]></span><span class="kwd">use</span><span class="pln"> test01</span><span class="pun">;</span>
  3. <span class="typ">MariaDB</span><span class="pun">[</span><span class="pln">test01</span><span class="pun">]></span><span class="pln"> select </span><span class="pun">*</span><span class="kwd">from</span><span class="pln"> pet</span><span class="pun">;</span>

在 Linux 中怎样将 MySQL 迁移到 MariaDB 上

 

结论

如你在本教程中看到的,MySQL-to-MariaDB 的迁移并不难。你应该知道,MariaDB 相比 MySQL 有很多新的功能。至于配置方面,在我的测试情况下,我只是将我旧的 MySQL 配置文件(my.cnf)作为 MariaDB 的配置文件,导入过程完全没有出现任何问题。对于配置文件,我建议你在迁移之前请仔细阅读 MariaDB 配置选项的文件,特别是如果你正在使用 MySQL 的特定配置。

如果你正在运行有海量的表、包括群集或主从复制的数据库的复杂配置,看一看 Mozilla IT 和 Operations 团队的 更详细的指南 ,或者 官方的 MariaDB 文档

 

故障排除

1、 在运行 mysqldump 命令备份数据库时出现以下错误。

  1. <span class="pln">$ mysqldump </span><span class="pun">--</span><span class="pln">all</span><span class="pun">-</span><span class="pln">databases </span><span class="pun">--</span><span class="pln">user</span><span class="pun">=</span><span class="pln">root </span><span class="pun">--</span><span class="pln">password </span><span class="pun">--</span><span class="pln">master</span><span class="pun">-</span><span class="pln">data </span><span class="pun">></span><span class="pln"> backupdb</span><span class="pun">.</span><span class="pln">sql </span>

  1. <span class="pln">mysqldump</span><span class="pun">:</span><span class="typ">Error</span><span class="pun">:</span><span class="typ">Binlogging</span><span class="pln"> on server </span><span class="kwd">not</span><span class="pln"> active</span>

通过使用 "--master-data",你可以在导出的输出中包含二进制日志信息,这对于数据库的复制和恢复是有用的。但是,二进制日志未在 MySQL 服务器启用。要解决这个错误,修改 my.cnf 文件,并在 [mysqld] 部分添加下面的选项。(LCTT 译注:事实上,如果你并没有启用二进制日志,那取消"--master-data"即可。)

  1. <span class="pln">log</span><span class="pun">-</span><span class="pln">bin</span><span class="pun">=</span><span class="pln">mysql</span><span class="pun">-</span><span class="pln">bin</span>

保存 my.cnf 文件,并重新启动 MySQL 服务:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> service mysql restart</span>

或者:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="kwd">systemctl</span><span class="pln"> restart mysql</span>

或:

  1. <span class="pln">$ </span><span class="kwd">sudo</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">mysql restart </span>

MariaDB 的详细介绍:请点这里
MariaDB 的下载地址:请点这里


via: http://xmodulo.com/migrate-mysql-to-mariadb-linux.html

作者:Kristophorus Hadiono 译者:strugglingyouth 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出

相关推荐