Import mysql dump using php

I have a mysql dump file. How can I import this file into mysql using php?

+4
source share
3 answers

Depending on how large the dump file is. A small one can simply be added using the sql command. A larger one can be imported using a script.

A quick search will show many ready-made scripts that break the dump into smaller pieces so as not to overload the server. I have used this in the past: http://www.ozerov.de/bigdump.php

+3
source

You dump is probably a file that just contains a lot of SQL queries.

The easiest way to import such a dump is to use the mysql command line client (especially if the file is large!)

 mysql --user=USERNAME --password=PASSWORD --host=HOST DB_NAME < /path/to/your/file.sql 


If you can connect to your server with some command line access (usually using ssh), this is the way to go.

Otherwise, I suppose, you could execute such a command using system()

+2
source

I want to use PHP, you can easily use the file() function, which reads a file line by line.

You can try something like this:

 mysql_connect(BD_HOST, DB_USER, BD_PASS) or die("Error connecting to MySQL server: ".mysql_error()); mysql_select_db(DB_NAME); $all_lines = file("mysql_dump.sql", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); foreach($all_lines as $query) { if(substr($query, 0, 2) == "--") { continue; } mysql_query($query) or die("Error executing query <br/>&nbsp;&nbsp;"$query\"<br/>".mysql_error()); } 
+2
source

Source: https://habr.com/ru/post/1345380/


All Articles