提高mysql导入速度
我有一个22GB的大型数据库。我曾经用gzip格式的提高mysql导入速度,mysql,linux,database-administration,Mysql,Linux,Database Administration,我有一个22GB的大型数据库。我曾经用gzip格式的mysqldump命令进行备份 当我提取gz文件时,它会生成16.2GB的.sql文件 当我尝试在本地服务器中导入数据库时,导入大约需要48小时。有没有办法提高导入过程的速度 另外,我想知道是否需要进行任何硬件更改以提高性能 当前系统配置 Processor: 4th Gen i5 RAM: 8GB #更新 我的简历如下 # # The MySQL database server configuration file. # # You c
mysqldump
命令进行备份
当我提取gz文件时,它会生成16.2GB的.sql
文件
当我尝试在本地服务器中导入数据库时,导入大约需要48小时。有没有办法提高导入过程的速度
另外,我想知道是否需要进行任何硬件更改以提高性能
当前系统配置
Processor: 4th Gen i5
RAM: 8GB
#更新
我的简历如下
#
# 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/
它正在上载3天,目前已导入9.9 GB。数据库既有MyISAM
表,也有InnoDB
表。我可以做些什么来提高导入性能
我已经尝试过使用mysqldump
以gz格式分别导出每个表,并通过执行以下代码的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);
}
$dir=“./”;
$files=scandir($dir,1);
数组_pop($文件);
数组_pop($文件);
$tablecount=0;
foreach($files作为$file){
$tablecount++;
echo$tablecount。”;
echo$文件。“\n”;
$command=“gunzip<”$file.| mysql-u root-pubuntu cms”;
echo exec($command);
}
您可以做的一件事是
SET AUTOCOMMIT = 0; SET FOREIGN_KEY_CHECKS=0
你也可以利用这些价值观
innodb_buffer_pool_size
innodb_additional_mem_pool_size
innodb_flush_method
在my.cnf
中启动,但一般来说,您也应该查看一下,看看什么最适合您
这是我过去遇到的一个问题,我觉得我没有完全解决,但我希望我从一开始就把自己指向了这个方向。这会为我节省很多时间。以所述方式进行转储和恢复将意味着MySQL必须在导入数据时完全重建索引。它还必须每次解析数据
如果您能够以MySQL已经理解的格式复制数据文件,那么效率会更高。这样做的一个好方法是使用Percona
(开源,作为的一部分分发,可从下载)
这将获取MyISAM表的快照,对于InnoDB表,它将复制底层文件,然后根据这些文件重放事务日志,以确保状态一致。它可以在一个实时服务器上实现这一点,而不需要停机(我不知道这是否是您的要求?)
我建议您阅读文档,但要以最简单的形式进行备份,请使用:
$ innobackupex --user=DBUSER --password=DBUSERPASS /path/to/BACKUP-DIR/
$ innobackupex --apply-log /path/to/BACKUP-DIR/
如果数据在同一台机器上,innobackupex甚至有一个简单的恢复命令:
$ innobackupex --copy-back /path/to/BACKUP-DIR
有更多的选项和不同的实际备份方法,因此我鼓励您在开始之前仔细阅读文档
就速度而言,我们的慢速测试服务器(IOPS约为600)可以使用此方法在大约4小时内恢复500 GB的备份
最后:你提到了可以做些什么来加速进口。这主要取决于瓶颈是什么。通常,导入操作受I/O限制(您可以通过检查io等待来测试这一点),提高速度的方法是使用更快的磁盘吞吐量—或者更快的磁盘本身,或者同时使用更多的磁盘。缺少许多参数,以便完全理解问题的原因。例如:
MySQL版本
磁盘类型和速度
在启动MySQL服务器之前释放服务器上的内存
mysqldump之前和时的iostat输出李>
您首先使用哪些参数来创建转储文件
还有更多
所以我试着猜测你的问题出在磁盘上,因为我有150个MySQL实例,其中一个上面有3TB的数据,通常磁盘就是问题所在
现在来看解决方案:
首先,您的MySQL没有配置为最佳性能
您可以在Percona博客文章中了解要配置的最重要设置:
特别检查参数:
innodb_buffer_pool_size
innodb_flush_log_at_trx_commit
innodb_flush_method
如果您的问题是磁盘—从同一驱动器读取文件—会使问题变得更糟
如果您的MySQL服务器因为没有足够的可用RAM而开始交换,您的问题会变得更大
您需要在执行恢复过程之前和执行恢复过程时在计算机上运行诊断程序来解决此问题
此外,我可以建议您使用另一种技术来执行重建任务,它比mysqldump工作得更快
这是Percona Xtrabackup-
您将需要使用它创建备份,并从中恢复,或者使用流式处理选项直接从运行的服务器重建
此外,从5.5开始的MySQL版本-InnoDB的性能比MyISAM更快。考虑将所有表更改为它。获取更多RAM,获得更快的处理器,获得SSD用于更快的写入。批处理插入,使其比一组单独插入运行得更快。这是一个巨大的文件,需要时间
方法1:按照fakedrake的建议禁用外键
设置自动提交=0;设置外键检查=0
方法2:使用BigDump,它会将mysqldump文件分块,然后导入该文件。
问题:你说你正在上传?如何导入转储文件?不是直接从服务器/命令行?确保将“max\u allowed\u packet”变量增加到足够大的大小。如果您有大量文本数据,这将非常有用。使用高性能硬件肯定会提高导入数据的速度
mysql --max_allowed_packet=256M -u root -p < "database-file.sql"
mysql--max\u allowed\u packet=256M-u root-p<“database file.sql”
我不得不处理同样的问题。我发现使用mysqldump
输出到CSV文件(如下所示):
然后使用loaddatainfle
查询从mysql客户端导入该数据(如下所示):
要比只执行包含数据的SQL查询快一个数量级。当然是艾尔
mysqldump -u [username] -p -t -T/path/to/db/directory [database] --fields-enclosed-by=\" --fields-terminated-by=,
LOAD DATA FROM INFILE /path/to/db/directory/table.csv INTO TABLE FIELDS TERMINATED BY ',';