简体   繁体   English

如何从phpmyadmin制作大型数据库的副本?

[英]How to make a copy of large database from phpmyadmin?

I want to create a dev environment of my website on the same server. 我想在同一台服务器上创建我的网站的开发环境。 But I have a 7Gb of database which contains 479 tables and I want to make a copy of that database to the new DB. 但我有一个7Gb的数据库,其中包含479个表,我想将该数据库的副本复制到新的数据库中。

I have tried this with the help of PHPmyadmin >> Operations >> copy database to functionality. 我在PHPmyadmin >> Operations >> copy database to的帮助下尝试了这个, PHPmyadmin >> Operations >> copy database to功能。 But every time it will fail and return the error 但每次它都会失败并返回错误

Error in processing request Error code: 500 Error text: Internal Error. 

Please let me know there is any other method/ solution to copy this database to a new database from cpanel please advise 请告诉我有任何其他方法/解决方案将此数据库从cpanel复制到新数据库请告知

NOTE: I have just read your comment, and as I can understand you don't have access to command line. 注意:我刚刚阅读了您的评论,并且我可以理解您无法访问命令行。 Please check Solution Two , this will definitely work. 请检查Solution Two ,这肯定会有效。

The only solution that will work for you (which work for me at 12GB database) is directly from the command line: 唯一适用于您的解决方案(适用于12GB数据库)直接来自命令行:

Solution One 解决方案一

mysql -u root -p

set global net_buffer_length=1000000; --Set network buffer length to a large byte number

set global max_allowed_packet=1000000000; --Set maximum allowed packet size to a large byte number

SET foreign_key_checks = 0; --Disable foreign key checking to avoid delays, errors and unwanted behavior

source file.sql --Import your sql dump file

SET foreign_key_checks = 1; --Remember to enable foreign key checks when the procedure is complete!

If you have root access you can create bash script: 如果您具有root访问权限,则可以创建bash脚本:

#!/bin/sh 

# store start date to a variable
imeron=`date`

echo "Import started: OK"
dumpfile="/home/bob/bobiras.sql"

ddl="set names utf8; "
ddl="$ddl set global net_buffer_length=1000000;"
ddl="$ddl set global max_allowed_packet=1000000000; "
ddl="$ddl SET foreign_key_checks = 0; "
ddl="$ddl SET UNIQUE_CHECKS = 0; "
ddl="$ddl SET AUTOCOMMIT = 0; "
# if your dump file does not create a database, select one
ddl="$ddl USE jetdb; "
ddl="$ddl source $dumpfile; "
ddl="$ddl SET foreign_key_checks = 1; "
ddl="$ddl SET UNIQUE_CHECKS = 1; "
ddl="$ddl SET AUTOCOMMIT = 1; "
ddl="$ddl COMMIT ; "

echo "Import started: OK"

time mysql -h 127.0.0.1 -u root -proot -e "$ddl"

# store end date to a variable
imeron2=`date`

echo "Start import:$imeron"
echo "End import:$imeron2"

Source 资源

Solution Two 解决方案二

Also, there is another option which is very good for those who are on shared hosting and don't have command line access. 此外,还有另一个选项,非常适合那些共享主机并且没有命令行访问权限的人。 This solution worked for me on 4-5GB files: 这个解决方案适用于4-5GB文件:

  1. MySQL Dumper: Download (You will able to backup/restore SQL file directly from "MySQL Dumper" you don't need phpmyadmin anymore). MySQL Dumper: 下载 (您将能够直接从“MySQL Dumper”备份/恢复SQL文件,您不再需要phpmyadmin)。
  2. Big Dump: Download (Just restore from Compress file and SQL file, need BIGDUMP PHP file editing for big import $linespersession = 3000; Change to $linespersession = 30000; ) 大转储: 下载 (只需从压缩文件和SQL文件恢复,需要BIGDUMP PHP文件编辑才能进行大导入$linespersession = 3000;更改为$linespersession = 30000;

Solution Three: 解决方案三:

This solution definitely works, it is slow but works. 这个解决方案绝对有效,它很慢但有效。

Download Trial version of (32 or 64 bit): Navicat MySQL Version 12 下载(32或64位)的试用版: Navicat MySQL Version 12

Install -> and RUN as Trial. 安装 - >和RUN作为试用版。

After that Add your Computer IP (Internet IP, not local IP), to the MySQL Remote in cPanel (new database/hosting). 之后将您的计算机IP(Internet IP,而不是本地IP)添加到cPanel(新数据库/主机)中的MySQL Remote。 You can use wildcard IP in cPanel to access MySQL from any IP. 您可以在cPanel中使用通配符IP从任何IP访问MySQL。

Goto Navicat MySQL: click on Connection put a connection name. Goto Navicat MySQL:点击Connection输入连接名称。

In next "Hostname/IP" add your "Hosting IP Address" (don't use localhost). 在下一个“主机名/ IP”中添加“主机IP地址”(不要使用localhost)。 Leave port as it is (if your hosting defined a different port put that one here). 保持端口不变(如果你的主机定义了一个不同的端口放在那里)。

add your Database Username and Password 添加您的数据库用户名密码

Click Test Connection , If it's successful, click on "OK" 单击“ 测试连接” ,如果成功,单击“确定”

Now on the Main Screen you will see all the database connected with the username on the left side column. 现在在主屏幕上,您将看到所有数据库都与左侧列中的用户名相关联。

Double click on your database where you want to import SQL file: 双击要导入SQL文件的数据库:

Icon color of the database will change and you will see "Tables/views/function etc..". 数据库的图标颜色将发生变化,您将看到“Tables / views / function etc.”。

Now right click on database and select " Execute SQL file " ( http://prntscr.com/gs6ef1 ). 现在右键单击 数据库并选择“ 执行SQL文件 ”( http://prntscr.com/gs6ef1 )。 choose the file, choose "continue on error" if you want to and finally run it. 选择文件,如果你愿意,选择“继续出错”并最终运行它。 Its take some time depending on your network connection speed and computer performance. 它需要一些时间,具体取决于您的网络连接速度和计算机性能。

Create an export of your database. 创建数据库的导出。 This should be easily done thru PhpMyAdmin interface. 这应该通过PhpMyAdmin接口轻松完成。 Once you downloaded the DB export, you need to create a new DB where you will put your exported data. 下载数据库导出后,您需要创建一个新数据库,您可以在其中放置导出的数据。 This, too, should be easily done thru PhpMyAdmin user interface. 这也应该通过PhpMyAdmin用户界面轻松完成。

To upload it, we cannot use Import -> Browse your computer because it has a limit of 2MB. 要上传它,我们无法使用Import -> Browse your computer因为它的限制为2MB。 One solution is to use Import -> Select from the web server upload directory /var/lib/phpMyAdmin/upload/ . 一种解决方案是使用Import -> Select from the web server upload directory /var/lib/phpMyAdmin/upload/ Upload your exported data in this directory. 将导出的数据上传到此目录中。 After that, your uploaded data should be listed in the dropdown next to it. 之后,您上传的数据应列在其旁边的下拉列表中。

在此输入图像描述

If this fails too, you can use the command line import. 如果此操作也失败,您可以使用命令行导入。

mysql -u user -p db_name < /path/to/file.sql

The easiest way is to try exporting the data from phpmyadmin. 最简单的方法是尝试从phpmyadmin 导出数据 It will create the backup of your data. 它将创建数据备份。

But Sometimes, transferring large amount of data via import/export does result into errors. 但有时,通过导入/导出传输大量数据会导致错误。

You can try mysqldump to backup the data as well. 您也可以尝试使用mysqldump备份数据。

I found a few links for you here and here . 我在这里这里找到了一些链接。

This is the mysqldump database backup documentation . 这是mysqldump数据库备份文档

Hope it helps. 希望能帮助到你。 :D :d

I suspect that PHPMyAdmin will handle databases of that size (PHP upload/download limits, memory constraints, script execution time). 我怀疑PHPMyAdmin将处理该大小的数据库(PHP上传/下载限制,内存限制,脚本执行时间)。 If you have acccess to the console, i would recommend doing export/import via the mysql command line: 如果您已经访问控制台,我建议通过mysql命令行进行导出/导入:

Export: 出口:

    $ mysqldump -u <user> -p<pass> <liveDatabase> | gzip > export.sql.gz

And Import: 和导入:

    $ gunzip < export.sql.gz | mysql -u <user> -p<pass> <devDatabase>

after you have created the new dev database in eg PHPMyAdmin or via command line. 在例如PHPMyAdmin或通过命令行创建新的dev数据库之后。

Otherwise, if you only have access to an Apache/PHP environment, I would look for an export utility that splits export in smaller chunks. 否则,如果您只能访问Apache / PHP环境,我会寻找一个导出实用程序,它以较小的块分割导出。 MySQLDumper comes to mind, but it's a few years old and AFAIK it is no longer actively maintained and is not compatible with PHP 7+. MySQLDumper浮现在脑海中,但它已经存在了几年,AFAIK它不再被主动维护,并且与PHP 7+不兼容。 But I think there is at least a pull request out there that makes it work with PHP7 (untested). 但我认为至少有一个pull请求可以使它与PHP7一起使用(未经测试)。

Edit based on your comment: 根据您的评论进行修改:

If the export already exists and the error occurs on import, you could try to increase the limits on your PHP environment, either via entries in .htaccess , changing php.ini or ini_set , whatever is available in your environment. 如果导出已经存在且导入时发生错误,您可以尝试通过.htaccess条目,更改php.iniini_set ,以及环境中可用的任何内容来增加PHP环境的限制。 The relevant settings are eg for setting via .htaccess (keep in mind, this will work only for apache environments with mod_php and also can be controlled by your hoster): 相关设置例如用于通过.htaccess进行设置(请记住,这仅适用于具有mod_php的apache环境,也可以由您的主机控制):

      php_value max_execution_time 3600
      php_value post_max_size 8000M
      php_value upload_max_filesize 8000M
      php_value max_input_time 3600

This may or may not work, depending on x32/x64 issues and/or your hosters restrictions. 这可能有效,也可能无效,具体取决于x32 / x64问题和/或您的托管服务商限制。 Additionally, you need to adjust the PHPmyadmin settings for ExecTimeLimit - usually found in the config.default.php for your PHPMyAdmin installation: Replace 此外,你需要调整phpMyAdmin的设置ExecTimeLimit -通常在发现config.default.php您的phpMyAdmin安装:更换

      $cfg['ExecTimeLimit'] = 300;

with

      $cfg['ExecTimeLimit'] = 0;

And finally, you probably need to adjust your MySQL config to allow larger packets and get rid of the 'lost connection' error: [mysqld] section in my.ini : 最后,您可能需要调整MySQL配置以允许更大的数据包并摆脱“丢失的连接”错误:my.ini中的[mysqld]部分:

      max_allowed_packet=256M

You can use mysqldump as follow 你可以使用mysqldump如下

mysqldump —user= —password= --default-character-set=utf8

You can also make use of my shell script, which actually wrote long back for creating back-up of MySQL database on regular basis using cron job. 您还可以使用我的shell脚本,该脚本实际上是使用cron作业定期编写的,以便定期备份MySQL数据库。

#!/bin/sh
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="db_backup_$now".gz
backupfolder=“"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt
echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
mysqldump —user= —password= --default-character-set=utf8  | gzip > "$fullpathbackupfile"
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
chown  "$fullpathbackupfile"
chown  "$logfile"
echo "file permission changed" >> "$logfile"
find "$backupfolder" -name db_backup_* -mtime +2 -exec rm {} \;
echo "old files deleted" >> "$logfile"
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*****************" >> "$logfile"
exit 0

I have already written an article on Schedule MySQL Database backup on CPanel or Linux . 我已经在CPanel或Linux上写了一篇关于安排MySQL数据库备份的文章。

Here's how I handled that problem when I faced it... Unfortunately this only works for Mac OS. 这是我在面对它时如何处理这个问题...不幸的是,这仅适用于Mac OS。

  • Download Sequel Pro - Completely free, and it has worked really well for me for over a year now. 下载Sequel Pro - 完全免费,它已经为我工作了一年多了。
  • Remotely connect to your server's database. 远程连接到服务器的数据库。 You will probably need to add your ip address to the "Remote MYSQL" section in CPANEL. 您可能需要将您的IP地址添加到CPANEL中的“Remote MYSQL”部分。 If you don't have the credentials, you can probably get them from your website's config file. 如果您没有凭据,则可以从网站的配置文件中获取凭据。
  • Once you're in the server, you can select all of your tables, secondary click, and select Export > As SQL Dump. 进入服务器后,可以选择所有表,辅助单击,然后选择“导出”>“作为SQL转储”。 You probably won't need to edit any of the settings. 您可能不需要编辑任何设置。 Click "Export". 单击“导出”。
  • Login to your local servers database, and select "Query" from the top menu. 登录到本地服务器数据库,然后从顶部菜单中选择“查询”。
  • Drag and drop the file that was downloaded from the export and it will automatically setup the database from the sql dump. 拖放从导出下载的文件,它将自动从sql转储设置数据库。

I hope this helps. 我希望这有帮助。 It's a little work around, but it's worked really well for me, especially when PMA has failed. 这是一个小小的工作,但它对我来说非常好,特别是当PMA失败时。

Since the requirements include PHPMyAdmin, my suggestion is to: 由于要求包括PHPMyAdmin,我的建议是:

  1. select the database you need 选择您需要的数据库
  2. go to the "Export" tab 转到“导出”选项卡
  3. click the "Custom - display all possible options" radio button 单击“自定义 - 显示所有可能的选项”单选按钮
  4. in the "Save output to a file" radio button options, select "gzipped" for "Compression:" 在“保存输出到文件”单选按钮选项中,为“压缩:”选择“gzipped”
  5. Remove the "Display comments" tick (to save some space) 删除“显示注释”勾选(以节省一些空间)
  6. Finish the export 完成导出

Then try to import the generated file in the new Database you have (if you have sufficient resources - this should be possible). 然后尝试在您拥有的新数据库中导入生成的文件(如果您有足够的资源 - 这应该是可能的)。

Note: My previous experience shows that using compression allows larger DB exports/import operations but have not tested what is the upper limit in shared hosting environments (assuming this by your comment for cPanel). 注意:我之前的经验表明,使用压缩允许更大的数据库导出/导入操作,但尚未测试共享托管环境中的上限(假设您对cPanel的评论)。

Edit: When your export file is created, select the new database (assuming it is already created), go to the "Import" tab, select the file created from the export and start the import process. 编辑:创建导出文件时,选择新数据库(假设它已创建),转到“导入”选项卡,选择从导出创建的文件并开始导入过程。

Limited to phpMyAdmin? 限于phpMyAdmin? Don't do it all at once 不要一次完成所有操作

Large data-sets shouldn't be dumped (unless it's for a backup), instead, export the database without data, then copy one table at a time (DB to DB directly). 不应转储大型数据集(除非是用于备份),而是在没有数据的情况下 导出数据库,然后一次复制一个表(直接DB到DB)。

Export/Import Schema 导出/导入架构

First, export only the database schema via phpMyAdmin (uncheck data in the export options). 首先,通过phpMyAdmin 导出数据库模式 (取消选中导出选项中的数据 )。 Then import that onto a new database name. 然后将其导入新的数据库名称。

Alternatively, you could use something like below to generate statements like below, once you've created the DB. 或者,一旦您创建了数据库,就可以使用类似下面的内容生成如下所示的语句。 The catch with this method is that you're likely to lose constraints, sprocs, and the like. 使用此方法的问题是您可能会丢失约束,sprocs等。

CREATE TABLE [devDB].[table] LIKE [prodDB].[table]

Copy data, one table at a time. 一次复制数据,一个表。

Use a good editor to create the 470 insert statements you need. 使用一个好的编辑器来创建所需的470个插入语句。 Start with a list of table names, and use the good old find-and-replace. 从表名列表开始,并使用旧的查找和替换。

INSERT INTO [devDB].[table] SELECT * FROM [prodDB].[table];

This may choke, depending on your environment. 根据您的环境,这可能会窒息。 If it does , drop and recreate the dev database (or empty all tables via phpMyAdmin). 如果 ,则删除并重新创建dev数据库(或通过phpMyAdmin清空所有表)。 Then, run the INSERT commands a few tables at a time. 然后,一次运行INSERT命令几个表。

Database Administration requires CLI 数据库管理需要CLI

The real problem you're facing here is that you're trying to do database administration without access to the Command Line Interface. 您在这里遇到的真正问题是您在不访问命令行界面的情况下尝试进行数据库管理。 There are significant complicated details to migrating large sets of data efficiently, most of which can only be solved using tools like mysqldump. 有效地迁移大量数据有很多复杂的细节,其中大部分只能使用像mysqldump这样的工具来解决。

如果您的本地服务器中有数据库,则可以将其导出并使用BigDump插入全局服务器BigDump上的新数据库

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

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