How to import and export large amounts of data from MySQL

Source: Internet
Author: User
Tags chop
You must have used the Database Import and Export function in phpmyadmin, which is very convenient. However, in actual application, I found the following problems: 1. The database size exceeds a certain size. For example, 6 MB is normal when export is used. It can be properly saved to the local hard disk, but the import will not work! The reason is: Set temporary files in common PHP. INI.

You must have used the Database Import and Export function in phpmyadmin, which is very convenient. However, in actual application, I found the following problems: 1. The database size exceeds a certain size, for example, 6 MB. In this case, it is normal to export data. You can properly Save the data to the local hard disk, but the import will not work! The reason is: Set temporary files in common PHP. INI.

You must have used the Database Import and Export function in phpmyadmin, which is very convenient. However, in practical application, I found the following problems:
1. The size of the database exceeds a certain size. For example, if the size of 6 MB is used for export, the database can be correctly saved to the hard disk of the local machine, but cannot be imported! The reason is: the size of temporary files/uploaded files is limited to 2 MB in PHP. INI, while phpmyadmin uses the upload method, resulting in a failure.
2. When importing the. SQL file exported to the hard disk back, the import may fail due to some single quotation marks, which can only be imported using applications such as mysql.
My database has exceeded 10 MB, so this problem must be solved. My ideas:
Export: Use phpmyadmin to save the database/table structure, use scripts to read the database content, and save it to a file!
Import: Use phpmyadmin to restore the database/table structure, use scripts to read files, and save them to the database!
The Export program is as follows: The call method is ***. php? Table = tablename
This simple program currently saves a table at a time !! Data of one field for each behavior !!

<?
If ($ table = \ "\") exit ();
Mysql_connect (\ "localhost \", \ "name \", \ "password \");
Mysql_select_db (\ "database \");
$ Result = mysql_query (\ "select * from $ table \");
If (mysql_num_rows ($ result) <= 0) exit ();
Echo \ "starts to convert data to text...
\";
$ Handle = fopen (\ "Export table.txt \", \ "w \");
$ Numfields = mysql_num_fields ($ result );
Fputs ($ handle, $ numfields. \ "\ r \ n \");
For ($ k = 0; $ k <mysql_num_rows ($ result); $ k ++)
{
$ Msg = mysql_fetch_row ($ result );
For ($ I = 0; $ I <$ numfields; $ I ++)
{
$ Msg [$ I] = str_replace (\ "\ r \ n \", \ "& php2000mysqlreturn & \", $ msg [$ I]);
$ Msg [$ I] = str_replace (\ "\ n \", \ "& php2000mysqlreturn & \", $ msg [$ I]);
Fputs ($ handle, $ msg [$ I]. \ "\ r \ n \");
}
Fputs ($ handle, \ "------- php2000 dump data program V1.0 for MySQL -------- \ r \ n \");
}
Fclose ($ handle );
Echo \ "OK \";
?>


The imported program is as follows: the usage is the same as above!

<?
If ($ table = \ "\") exit ();
Mysql_connect (\ "localhost \", \ "name \", \ "password \");
Mysql_select_db (\ "database \");
$ Message = file (\ "Your table.txt \");
Echo $ numfields = chop ($ message [0]);
For ($ k = 1; $ k <count ($ message); $ k + = ($ numfields + 1 ))
{
$ Value = \"\";
For ($ I = $ k; $ I <($ k + $ numfields-1); $ I ++)
{
$ Tmp = str_replace (\ "& php2000mysqlreturn & \", \ "\ r \ n \", chop ($ message [$ I]);
$ Value. = \ "\ '\". addslashes ($ tmp ).\"\',\";
}
$ Tmp = str_replace (\ "& php2000mysqlreturn & \", \ "\ r \ n \", chop ($ message [$ k + $ numfields-1]);
$ Value. = \ "\ '\". $ tmp .\"\'\";
$ Query = \ "insert into $ table values (\". $ value .\")\";
Echo mysql_error ();
Mysql_query ($ query );
Echo $ k .\"\";
}
Echo \ "OK \";
?>

Usage and possible problems!
1. The file () function may be faulty during import (my 10 m data is normal). You can change it to fopen () and then read a row once !!
2. ftp is required for both import and export. After export, ftp is used to transfer the data to the local machine. ftp is used to transfer the data to the server before import!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.