提高mysql导入的速度

dha*_*bro 43 mysql linux database-administration

我有大型数据库22GB.我曾经用mysqldumpgzip格式的命令备份.

当我提取gz文件时,它生成的.sql文件16.2GB

当我尝试在本地服务器中导入数据库时​​,导入大约需要48小时.有没有办法提高导入过程的速度?

此外,我想知道是否需要进行任何硬件更改以提高性能.

当前系统配置

 Processor: 4th Gen i5
 RAM: 8GB
Run Code Online (Sandbox Code Playgroud)

#UPDATE

my.cnf如下

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions are currently parsed.
[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     = /var/lib/mysql
tmpdir      = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 127.0.0.1
#
# * Fine Tuning
#
key_buffer      = 16M
max_allowed_packet  = 512M
thread_stack        = 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 4M
query_cache_size        = 512M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries   = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id      = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size         = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem



[mysqldump]
quick
quote-names
max_allowed_packet  = 512M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer      = 512M

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
Run Code Online (Sandbox Code Playgroud)

它正在上传3天,现在已经导入了9.9 GB.数据库有两个表MyISAMInnoDB表.我该怎么做才能提高进口绩效?

我已经尝试以gz格式单独导出每个表,mysqldump并通过执行以下代码的PHP脚本导入每个表

$dir="./";
$files = scandir($dir, 1);
array_pop($files);
array_pop($files);
$tablecount=0;
foreach($files as $file){
    $tablecount++;
    echo $tablecount."     ";

    echo $file."\n";
    $command="gunzip < ".$file." | mysql -u root -pubuntu cms";

    echo exec($command);
}
Run Code Online (Sandbox Code Playgroud)

And*_*age 11

以所述方式执行转储和恢复意味着MySQL必须在导入数据时完全重建索​​引.它还必须每次解析数据.

如果你能以MySQL已经理解的格式复制数据文件会更有效率.这样做的好方法是使用Percona的innobackupex

(开源并作为XtraBackup的一部分分发,可从此处下载).

这将拍摄MyISAM表的快照,对于InnoDB表,它将复制底层文件,然后针对它们重放事务日志以确保一致的状态.它可以从没有停机时间的实时服务器执行此操作(我不知道这是否是您的要求?)

我建议你阅读文档,但要使用最简单的形式进行备份:

$ innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/
$ innobackupex --apply-log /path/to/BACKUP-DIR/
Run Code Online (Sandbox Code Playgroud)

如果数据在同一台机器上,那么innobackupex甚至还有一个简单的恢复命令:

$ innobackupex --copy-back /path/to/BACKUP-DIR
Run Code Online (Sandbox Code Playgroud)

有更多的选项和不同的实际备份方式,所以我真的鼓励你在开始之前仔细阅读文档.

对于速度的参考,我们的慢速测试服务器(大约600 IOPS)可以使用此方法在大约4小时内恢复500 GB的备份.

最后:您提到了如何加快导入速度.它主要取决于瓶颈是什么.通常,导入操作是受I/O限制的(您可以通过检查io等来测试它),加快速度的方法是更快的磁盘吞吐量 - 更快的磁盘本身,或者更多的一致.


Tat*_*ata 11

为了完全理解问题的原因,有很多参数缺失.如:

  1. MySQL版本
  2. 磁盘类型和速度
  3. 启动MySQL服务器之前,服务器上有空闲内存
  4. mysqldump之前和之时的iostat输出.
  5. 首先用于创建转储文件的参数是什么.

还有很多.

所以我会试着猜测你的问题是在磁盘中,因为我有150个MySQL实例,我用其中一个管理3TB的数据,通常磁盘就是问题

现在解决方案:

首先 - 您的MySQL未配置为获得最佳性能.

您可以在Percona博客文章中了解要配置的最重要设置:http://www.percona.com/blog/2014/01/28/10-mysql-settings-to-tune-after-installation/

特别检查参数:

innodb_buffer_pool_size 
innodb_flush_log_at_trx_commit
innodb_flush_method
Run Code Online (Sandbox Code Playgroud)

如果你的问题是磁盘 - 从同一个驱动器读取文件 - 会使问题变得更糟.

如果您的MySQL服务器开始交换,因为它没有足够的RAM可用 - 您的问题变得更大.

您需要在还原过程之前和还原过程中在计算机上运行诊断程序才能解决问题.

此外,我建议你使用另一种技术来执行重建任务,它比mysqldump工作得更快.

它是Percona Xtrabackup - http://www.percona.com/doc/percona-xtrabackup/2.2/

您需要使用它创建备份,然后从中进行恢复,或者直接使用流选项从正在运行的服务器重建.

此外,MySQL版本从5.5开始 - InnoDB比MyISAM执行速度更快.考虑将所有表格更改为它.


fak*_*ake 7

你能做的一件事是

SET AUTOCOMMIT = 0; SET FOREIGN_KEY_CHECKS=0
Run Code Online (Sandbox Code Playgroud)

你也可以玩这些价值观

innodb_buffer_pool_size
innodb_additional_mem_pool_size
innodb_flush_method
Run Code Online (Sandbox Code Playgroud)

my.cnf让你走,但总的来说,你应该看看在InnoDB的参数的其余部分,以及看看有什么最适合你.

这是我过去遇到的一个问题,我觉得我没有完全解决这个问题,但我希望自己从这个方向指出了自己.本来可以节省一些时间.

  • @DharanBro那是因为你错误地拼写了它. (6认同)

Aht*_*ham 7

我必须导入与您的大小几乎相同的转储 (15.8GB),并且使用以下设置花了我 2.2 小时才能完成:

我的.cnf:

innodb_buffer_pool_size = 12G
innodb_log_buffer_size = 256M
innodb_log_file_size = 2G
innodb_write_io_threads = 32
innodb_flush_log_at_trx_commit = 0
innodb_doublewrite = 0
Run Code Online (Sandbox Code Playgroud)

我的系统规格是:

CPU: core i5 7th gen
RAM: 16GB
HDD: 500GB
Run Code Online (Sandbox Code Playgroud)

我已经根据我的系统规格配置了这些设置,因为您的系统有 8GB RAM,因此您可以像这样配置它:

innodb_buffer_pool_size = 5G
innodb_log_buffer_size = 256M
innodb_log_file_size = 1G
innodb_write_io_threads = 16
innodb_flush_log_at_trx_commit = 0
innodb_doublewrite = 0
Run Code Online (Sandbox Code Playgroud)

您可以尝试看看哪种配置更适合您。另外不要忘记重新启动 mysql 以应用更改。


小智 5

确保将“ max_allowed_pa​​cket ”变量增加到足够大的大小。如果您有大量文本数据,这将真正有帮助。使用高性能硬件肯定会提高数据导入速度。

mysql --max_allowed_packet=256M -u root -p < "database-file.sql"
Run Code Online (Sandbox Code Playgroud)

  • max_allowed_pa​​cket = 512M 在配置中,因此将其设为 256M 实际上会减小其大小。 (4认同)