วิธีแก้ปัญหาการนำเข้าฐานข้อมูลขนาดใหญ่ ใน PHP และ MySQL
หน้าแรก PHP MySQL เกร็ดความรู้ วิธีแก้ปัญหาการนำเข้าฐานข้อมูลขนาดใหญ่ ใน PHP และ MySQL
หากใครพบปัญหาในการ import ข้อมูลขนาดใหญ่ผ่าน phpmyadmin แล้วละก็ ผมมีทางแก้ให้นั่นก็คือ การใช้โปรแกรมช่วยชื่อ bigdump โดยตัวโปรแกรมเขียนด้วยภาษา PHP การทำงานของมันคือทำการ import file sql เข้าไปยังฐานข้อมูล MySql ได้อย่างไม่ต้องกังวลถึงขนาดไฟล์ sql
รายละเอียดโปรแกรม
ชื่อโปรแกรม: BigDump
ผู้เขียน: Alexey Ozerov (alexey at ozerov dot de)
ลิขสิทธ์: GNU License
สิ่งที่ต้องการ
- Bigdump script bigdump.php from the download ได้จาก link ข้างบน
- Dump ข้อมูลจาก database ที่สร้างโดย phpMyAdmin. สามารถใช้ GZip compressed เช่น dump.gz. GZip support เฉพาะ PHP 4.3.0 ขึ้นไป
Note: หากใช้ gzip อาจใช้เวลาในการประมวลผลนานเนื้องจากต้อง extract file ก่อนตอนปประมวลผล - Access account สำหรับ mySQL database
- Access account สำหรับ web server ที่มี PHP 4.1.0 หรือสูงกว่านั้นติดตั้งอยู่และสามารถติดต่อกับ mySQL database
- โปรแกรม text editor จำพวก Notepad เพื่อแก้ configuration file
- โปรแกรม FTP client สำหรับ upload files ไปยัง web server
- ความรู้เบื่องต้นเกี่ยวกับ PHP, mySQL databases, phpMyAdmin, FTP และ HTTP
เริ่มกันเลยครับผม
Download Script มาก่อนครับ Download | เวปต้นฉบับ
- แตกไฟล์ zip ออกมาจะได้ไฟล์ bigdump.php นะครับ
- จัดการ config ด้วย text editer ที่ถนัดตามนี้ครับ// Database configuration
$db_server = localhost; // ตั้งค่า DB ตาม Server ของคุณ
$db_name = ;
$db_username = ;
$db_password = ; // Other Settings$filename = ; // ชื่อ file sql ที่ต้องการจะ dump - เสร็จแล้วก็ upload ขึ้น server ไปพร้อมกับไฟล์ SQL ได้เลยครับ แล้วก็เรียก http://www.yourdomain.com/bigdump.php กด Start Import เท่านี้ก็เรียบร้อยครับ
FAQ
Q: I get an error: MySQL: Bad syntax near DEFAULT CHARACTER SET . Why?
A: You are probably trying to restore a dump file from a newer mySQL database version into an older mySQL database. Sorry, this is obviously not a very good idea and this cant work. You have to recreate the dump file in compatibility mode or remove all incompatible stuff by hand.
Q: I get an error: MySQL: Table some_tbl_name already exists. Why?
A: Your dump file doesnt contain DROP queries. Use phpMyAdmin to drop all the tables on the target database which must be restored before you start the import.
Q: I get an error: MySQL: Variable xyz cant be set to the value of NULL. Why?
A: Your dump file probably contains a statement like this: SET character_set_client = @saved_cs_client;
Bigdump is not able to restore settings saved at the beginning of the import process. However you can skip this error message by adding a comment setting $comment[]=SET character_set_client = @saved_cs_client;;
to the BigDump configuration.
Q: I get an error at the line beginning with /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE ...
Why?
A: You can skip this error by adding a comment setting $comment[]=/*!;
to the BigDump configuration. This setting is already included in bigdump.php but its deactivated. Remove double slashes at the beginning of that line to activate the setting.
Q: I get an error: Fatal error: Allowed memory size of xxx bytes exhausted or MySQL server has gone away. Why?
A: Your dump file probably contains extended inserts. An extended insert contains all table entries within one SQL query. BigDump isnt able to split such SQL queries. Please turn off extended inserts when exporting database from phpMyAdmin.
Q: Why does BigDump fail putting strange SQL errors if I run it on MS Windows based server?
A: This is IMHO a bug in the PHP4 and PHP5 that is crashing BigDump on Win32 if using a dump file with DOS encoded line breaks (contact me for details). As workaround create your dump with only the UNIX line breaks or convert it into UNIX format using some text editor. Then you will be able to run BigDump also on Win32.
Q: I get an error: PHP version 4.1.0 is required for BigDump to proceed. You have PHP 4.3.x installed. Sorry!. Why?
A: This is strange but it happens. Something is wrong with you PHP installation. As workaround you can remove from bigdump.php five code lines following on the comment // Check PHP version
. This will skip the version check.
Q: Can I use dump files created by other software than phpMyAdmin?
A: You can use any text dump file at your own risk. Although its very important for BigDump to find a semicolon followed by a line break at the end of each query. It wont work else since it has no time to parse the queries to find their proper ends. Further BigDump behaviour can be unpredictable if your dump file is using double quotes to surround strings. BigDump wont work too if your dump file contains any proprietary comment lines (like in some dumps created by other tools). Although you can use the $comment setting in bigdump.php to drop non-standard comment lines by their first characters. Apart from that BigDump doesnt filter the dump file in any way.
Q: Why dont you provide any facility to input the database configuration from the browser form?
A: Input the database configuration from the form would be very insecure since the settings must be saved in the cookies or returned to your browser everytime the session ends.
ขึ้นไปด้านบน