如何在Linux上将MySQL迁移到MariaDB?
本文将帮助您将数据库从MySQL迁移到MariaDB,因为在迁移过程中MySQL到MariaDB的二进制兼容性非常简单。
在甲骨文收购MySQL之后,社区推动了这一运动的结果,并开发了一个名为MariaDB的新数据库。MariaDB是开源的,并且与MySQL兼容。大多数Linux发行版(RH,CentOS,Fedora)已经开始使用MariaDB的支持来代替MySQL。
如果我们要将数据库从MySQL迁移到MariaDB,那么本文将有所帮助。
准备一个MySQL数据库和表
我们创建一个测试MySQL数据库和总计表以进行演示。如果环境中有数据库和表,则可以跳过此步骤。
使用MySQLroot用户和root用户密码从终端登录MySQL。
# mysql -uroot -p
创建一个数据库和表
Create database test1; use test1; create table tab1(name varchar(30), owner varchar(30), species varchar(20), sex char(1));
另外,我们将添加一些记录以测试数据
insert into tab1 values('Chandra','Prakash','Kadarla','m'),('Srinvas','Kadarla','Chary','m'); Records: 2 Duplicates: 0 Warnings: 0 Then quit from the MySQL.
MySQL数据库备份
我们需要使用以下命令对MySQL数据库进行备份–MySQLdump
# mysqldump --all-databases --user=root --password --master-data > backupDB.sql
出于安全原因,我们将MySQL的配置文件放在系统的其他位置
# cp /etc/mysql/my.cnf /opt/my.cnf.bak
卸载MySQL软件包
我们将使用以下命令卸载MySQL–
# service mysqld stop # yum remove mysql-server mysql-client mysql-common
安装MariaDB软件包
在所有主要的Linux发行版官方存储库中,MariaDB已被MySQL取代。即使您使用的是旧版本的Linux,也可以添加官方存储库。
在yumrepos目录中创建一个文件
# sudo vi /etc/yum.repos.d/MariaDB.repo
将以下内容添加到文件中并保存
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/5.5/centos7-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
要安装Maria-DBServer&Client,请运行以下命令。
# yum install MariaDB-server MariaDB-client Loaded plugins: fastestmirror, langpacks base | 3.6 kB 00:00 extras | 3.4 kB 00:00 mariadb | 2.9 kB 00:00 updates | 3.4 kB 00:00 --> Restarting Dependency Resolution with new changes. --> Running transaction check ---> Package MariaDB-shared.x86_64 0:5.5.49-1.el7.centos will be obsoleting ---> Package mariadb-libs.x86_64 1:5.5.35-3.el7 will be obsoleted --> Finished Dependency Resolution Dependencies Resolved ======================================================================================== Package Arch Version Repository Size ======================================================================================== Installing: MariaDB-client x86_64 5.5.49-1.el7.centos mariadb 8.6 M MariaDB-server x86_64 5.5.49-1.el7.centos mariadb 40 M MariaDB-shared x86_64 5.5.49-1.el7.centos mariadb 1.0 M replacing mariadb-libs.x86_64 1:5.5.35-3.el7 Installing for dependencies: MariaDB-common x86_64 5.5.49-1.el7.centos mariadb 23 k perl-Compress-Raw-Bzip2 x86_64 2.061-3.el7 base 32 k perl-Compress-Raw-Zlib x86_64 1:2.061-4.el7 base 57 k perl-DBI x86_64 1.627-4.el7 base 802 k perl-Data-Dumper x86_64 2.145-3.el7 base 47 k perl-IO-Compress noarch 2.061-2.el7 base 260 k perl-Net-Daemon noarch 0.48-5.el7 base 51 k perl-PlRPC noarch 0.2020-14.el7 base 36 k Transaction Summary ======================================================================================== Install 3 Packages (+8 Dependent packages) Total download size: 51 M Is this ok [y/d/N]: y Downloading packages: warning: /var/cache/yum/x86_64/7/mariadb/packages/MariaDB-5.5.49-centos7-x86_64-common.rpm: Header V4 DSA/SHA1 Signature, key ID 1bb943db: NOKEY Public key for MariaDB-5.5.49-centos7-x86_64-common.rpm is not installed (1/11): MariaDB-5.5.49-centos7-x86_64-common.rpm | 23 kB 00:00:01 (2/11): MariaDB-5.5.49-centos7-x86_64-client.rpm | 8.6 MB 00:00:12 warning: /var/cache/yum/x86_64/7/base/packages/perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Public key for perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm is not installed (3/11): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm | 32 kB 00:00:00 (4/11): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm | 57 kB 00:00:00 (5/11): perl-Data-Dumper-2.145-3.el7.x86_64.rpm | 47 kB 00:00:00 (6/11): perl-IO-Compress-2.061-2.el7.noarch.rpm | 260 kB 00:00:00 (7/11): perl-Net-Daemon-0.48-5.el7.noarch.rpm | 51 kB 00:00:00 (8/11): perl-DBI-1.627-4.el7.x86_64.rpm | 802 kB 00:00:00 (9/11): perl-PlRPC-0.2020-14.el7.noarch.rpm | 36 kB 00:00:00 (10/11): MariaDB-5.5.49-centos7-x86_64-shared.rpm | 1.0 MB 00:00:01 Total download size: 51 M Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : MariaDB-common-5.5.49-1.el7.centos.x86_64 1/12 Installing : perl-Data-Dumper-2.145-3.el7.x86_64 2/12 Installing : MariaDB-client-5.5.49-1.el7.centos.x86_64 3/12 Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 4/12 Installing : perl-Net-Daemon-0.48-5.el7.noarch 5/12 Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 6/12 Installing : perl-IO-Compress-2.061-2.el7.noarch 7/12 Installing : perl-PlRPC-0.2020-14.el7.noarch 8/12 Installing : perl-DBI-1.627-4.el7.x86_64 9/12 Installed: MariaDB-client.x86_64 0:5.5.49-1.el7.centos MariaDB-server.x86_64 0:5.5.49-1.el7.centos MariaDB-shared.x86_64 0:5.5.49-1.el7.centos Dependency Installed: MariaDB-common.x86_64 0:5.5.49-1.el7.centos perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 perl-DBI.x86_64 0:1.627-4.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7 perl-Net-Daemon.noarch 0:0.48-5.el7 perl-PlRPC.noarch 0:0.2020-14.el7 Replaced: mariadb-libs.x86_64 1:5.5.35-3.el7 Complete!
安装所有必需的软件包后,我们需要为root用户帐户设置密码。root密码恢复从MySQL备份的配置文件。
# cp /opt/my.cnf /etc/mysql/
现在,我们需要重新启动MariaDB服务,运行以下命令。
# service mariadb start
导入MySQL数据库
我们必须导入从MySQL备份到MariaDB的数据库
# mysql -u root -p < backupDB.sql
输入root用户的密码,然后将数据库导入到MariaDB中
我们需要检查数据库是否已正确还原。
请遵循以下命令–
# mysql -u root -p MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test1 | +--------------------+ 3 rows in set (0.00 sec) MariaDB [(none)]> use test1; MariaDB [test01]> select * from tab1; select * from tab1; +---------+---------+---------+------+ | name | owner | species | sex | +---------+---------+---------+------+ | Chandra | Prakash | Kadarla | m | | Srinvas | Kadarla | Chary | m | +---------+---------+---------+------+ 2 rows in set (0.00 sec)
现在,从MySQL到MariaDB的迁移已成功完成。
在运行命令并按照教程进行操作之后,我们可以通过简单的步骤将数据库从MySQL迁移到MariaDB,与MySQL相比,MariaDB具有许多新功能。在上面的教程中,我使用了一个简单的场景来测试迁移配置。