简体   繁体   English

导出和导入mysql数据库的最佳实践是什么?

[英]What is the best practice to export and import mysql database?

I have been working in a project which involves Mysql and PHP. 我一直在一个涉及Mysql和PHP的项目中工作。 While creating package for testing i have exported mysql database as sql file from MySql Workbench and imported into linux machine's mysql server with 在创建测试包时,我已经从MySql Workbench中将mysql数据库作为sql文件导出,并使用以下命令导入到Linux机器的mysql服务器中:

mysql>source mydatabase.sql;

is this the correct way to export and import mysql database?. 这是导出和导入mysql数据库的正确方法吗? And also the database file contains creation of schema, inserting data, and creating indexes scripts. 数据库文件还包含架构的创建,数据的插入以及索引脚本的创建。 Importing takes long time with this file. 此文件的导入需要很长时间。 My immediate manager suggests me to export without indices and import database, then execute index creation scripts. 我的直接经理建议我不带索引导出并导入数据库,然后执行索引创建脚本。 Is this is the correct way? 这是正确的方法吗? Does indices takes long time while importing database? 导入数据库时​​索引会花费很长时间吗?

Thanks in advance! 提前致谢!

Export depends on the MySQL version/vendor you are running ! 导出取决于您正在运行的MySQL版本/供应商!

  • MySQL(older versions) - uses mysqldump MySQL(旧版本)-使用mysqldump
  • MySQL(percona) - uses XtraBackup MySQL(percona)-使用XtraBackup
  • MySQL(EE - 5.6) - uses MySQL Enterprise Backup which is more complex and much faster. MySQL(EE-5.6)-使用MySQL Enterprise Backup,它更加复杂且速度更快。

Fastest way is to avoid mysqldump. 最快的方法是避免mysqldump。

I like Percona more then the others ! 我比其他人更喜欢Percona!
Another way but is more advanced i think is to create the DB on the destination server, and copy the database files directly. 我认为另一种但更高级的方法是在目标服务器上创建数据库,然后直接复制数据库文件。 Assuming user has access to the destination server's mysql directory: 假设用户有权访问目标服务器的mysql目录:
As root user: 以root用户身份:

[root@server-A]# /etc/init.d/mysqld stop
[root@server-A]# cd /var/lib/mysql/[databasename]
[root@server-A]# scp * user@otherhost:/var/lib/mysql/[databasename]
[root@server-A]# /etc/init.d/mysqld start

Important things here are: Stop mysqld on both servers before copying DB files, make sure the file ownership and permissions are correct on the destination before starting mysqld on the destination server. 这里重要的事情是:在复制数据库文件之前,在两台服务器上都停止mysqld,在目标服务器上启动mysqld之前,请确保目标服务器上的文件所有权和权限正确。

[root@server-B]# chown mysql:mysql /var/lib/mysql/[databasename]/*
[root@server-B]# chmod 660 /var/lib/mysql/[databasename]/*
[root@server-B]# /etc/init.d/mysqld start

With time being your priority here, the use of compression will depend on whether the time lost waiting for compression/decompression (with something like gzip) will be greater than the time wasted transmitting uncompressed data; 在这里将时间作为优先事项,压缩的使用将取决于等待压缩/解压缩(使用gzip之类)所浪费的时间是否大于传输未压缩数据所浪费的时间。 that is, the speed of your connection. 即您的连接速度。

Percona Server comes with a modified mysqldump tool so you can choose the --innodb-optimize-keys option. Percona Server附带了一个经过修改的mysqldump工具,因此您可以选择--innodb-optimize-keys选项。 This exports the tables, data, and index definitions like stock mysqldump, but during import it defers creation of secondary indexes until after the data has been loaded into the table. 这将导出表,数据,以及像股票指数的mysqldump的定义,但在导入过程中它推迟二级索引的创建的数据被加载到表,直到。 This takes advantage of InnoDB's fast index creation feature. 这利用了InnoDB的快速索引创建功能。

Another option is to use a physical backup tool, like Percona XtraBackup (as user @Up_One) mentioned. 另一个选择是使用物理备份工具,例如提到的Percona XtraBackup (作为用户@Up_One)。 This means that the full data files are backed up, including the indexes. 这意味着将备份完整的数据文件,包括索引。 On restore, nothing needs to be rebuilt, because the indexes are already populated. 还原时,无需重建任何内容,因为索引已被填充。 There are pros and cons to using physical backup tools too, but restore time is a big advantage. 使用物理备份工具也有优点和缺点,但是恢复时间是一个很大的优势。

I prefer to use mysqldump! 我更喜欢使用mysqldump! (Check this: http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html ) (检查此: http : //dev.mysql.com/doc/refman/5.0/en/mysqldump.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM