I have several sql files and I want to import all of them at once into a MySQL database.
I go to PHPMyAdmin, access the database, click import, select a file and import it. When I have more than a couple of files it takes a long time.
I would like to know if there is a better way to import multiple files, something like one file which will import the other files or similar.
I'm using WAMP and I would like a solution that does not require installing additional programs on my computer.
In Windows, open a terminal, go to the content folder and write:
copy /b *.sql all_files.sql
This concate all files in only one, making it really quick to import with PhpMyAdmin.
In Linux and macOS, as #BlackCharly pointed out, this will do the trick:
cat *.sql > .all_files.sql
Important Note: Doing it directly should go well, but it could end up with you stuck in a loop with a massive output file getting bigger and bigger due to the system adding the file to itself. To avoid it, two possible solutions.
A) Put the result in a separate directory to be safe (Thanks #mosh):
mkdir concatSql
cat *.sql > ./concatSql/all_files.sql
B) Concat them in a file with a different extension and then change it the name. (Thanks #William Turrell)
cat *.sql > all_files.sql1
mv all_files.sql1 all_files.sql
This is the easiest way that I have found.
In Windows (powershell):
cat *.sql | C:\wamp64\bin\mysql\mysql5.7.21\bin\mysql.exe -u user -p database
You will need to insert the path to your WAMP - MySQL above, I have used my systems path.
In Linux (Bash):
cat *.sql | mysql -u user -p database
Goto cmd
Type in command prompt
C:\users\Usersname>cd [.sql tables folder path ]
Press Enter
Ex: C:\users\Usersname>cd E:\project\database
Type command prompt
C:\users\Usersname>[.sql folder's drive (directory)name]
Press Enter
Ex: C:\users\Usersname>E:
Type command prompt for marge all .sql file(table) in a single file
copy /b *.sql newdatabase.sql
Press Enter
EX: E:\project\database>copy /b *.sql newdatabase.sql
You can see Merge Multiple .sql(file) tables Files Into A Single File in your directory folder
Ex: E:\project\database
I know it's been a little over two years... but I was looking for a way to do this, and wasn't overly happy with the solution posted (it works fine, but I wanted a little more information as the import happens). When combining all the SQL files in to one, you don't get any sort of progress updates.
So I kept digging for an answer and thought this might be a good place to post what I found for future people looking for the same answer. Here's a command line in Windows that will import multiple SQL files from a folder. You run this from the command line while in the directory where mysql.exe is located.
for /f %f in ('dir /b <dir>\<mask>') do mysql --user=<user> --password=<password> <dbname> < <dir>\%f
With some assumed values (as an example):
for /f %f in ('dir /b c:\sqlbackup\*.sql') do mysql --user=mylogin --password=mypass mydb < c:\sqlbackup\%f
If you had two sets of SQL backups in the folder, you could change the *.sql to something more specific (like mydb_*.sql).
just type:
cat *.sql |mysql -uroot -p
and mysql will import all the sql file in sequence
Enter the mysql shell like this.
mysql --host=localhost --user=username --password --database=db
Then use the source command and a semicolon to seperate the commands.
source file1.sql; source file2; source file3;
You could also a for loop to do so:
#!/bin/bash
for i in *.sql
do
echo "Importing: $i"
mysql your_db_name < $i
wait
done
Source
Save this file as .bat and run it , change variables inside parenthesis ...
#echo off
title Mysql Import Script
cd (Folder Name)
for %%a in (*) do (
echo Importing File : %%a
mysql -u(username) -p(password) %%~na < %%a
)
pause
if it's only one database modify (%%~na) with the database name .
The easiest solution is to copy/paste every sql files in one.
You can't add some sql markup for file importation (the imported files will be in your computer, not in the server, and I don't think MySQL manage some import markup for external sql files).
in windows open windows powershell and go to the folder where sql files are then run this command
cat *.sql | C:\xampp\mysql\bin\mysql.exe -u username -p databasename
Just type below command on your command prompt & it will bind all sql file into single sql file,
c:/xampp/mysql/bin/sql/ type *.sql > OneFile.sql;
Import From multiple SQL files into one Database.
Step 1: Goto to the folder and create file 'import-script.sh' with execute permission
(give Permission to file is chmod u+x import-script.sh )
#!/bin/bash
for i in *.sql
do
echo "Importing: $i"
mysql -u USERNAME -pPASSWORD DBNAME < $i
wait
done
The main thing is -p and PASSWORD didn't add any space.
Step 2: then in your terminal run this command ./import-script.sh
for windows users,
You can select the database in the phpMyadmin interface on the left, drag and drop all your files from your windows folder onto the web UI of phpMyadmin.
Related
I want to dump my database tables into a specific folder repeatedly in a file specified by real time date and time of dumping.
How can I do ?
1. Write this script inside any file for e.g. Sky [file extension doesn't matter] and make this file as executable file
current_date_time="`date "+%Y-%m-%d%H:%M:%S"`";
cd /home/akashgudadhe/FunZone/
mysqldump -u [user_name] -p[password] [dbname] > `pwd`/"$current_date_time.sql"
2. Run using terminal
./Sky [<----any_of your file name that you preferred above]
It will save [newly_created_file] into specified directory automatically.
I have a very large MySQL table on my local dev server: over 8 million rows of data. I loaded the table successfully using LOAD DATA INFILE.
I now wish to export this data and import it onto a remote host.
I tried LOAD DATA LOCAL INFILE to the remote host. However, after around 15 minutes the connection to the remote host fails. I think that the only solution is for me to export the data into a number of smaller files.
The tools at my disposal are PhpMyAdmin, HeidiSQL and MySQL Workbench.
I know how to export as a single file, but not multiple files. How can I do this?
I just did an import/export of a (partitioned) table with 50 millions record, it needed just 2 minutes to export it from a reasonably fast machine and 15 minutes to import it on my slower desktop. There was no need to split the file.
mysqldump is your friend, and knowing that you have a lot of data it's better to compress it
#host1:~ $ mysqldump -u <username> -p <database> <table> | gzip > output.sql.gz
#host1:~ $ scp output.sql.gz host2:~/
#host1:~ $ rm output.sql.gz
#host1:~ $ ssh host2
#host2:~ $ gunzip < output.sql.gz | mysql -u <username> -p <database>
#host2:~ $ rm output.sql.gz
Take a look at mysqldump
Your lines should be (from terminal):
export to backupfile.sql from db_name in your mysql:
mysqldump -u user -p db_name > backupfile.sql
import from backupfile to db_name in your mysql:
mysql -u user -p db_name < backupfile.sql
You have two options in order to split the information:
Split the output text file into smaller files (as many as you need, many tools to do this, e.g. split).
Export one table each time using the option to add a table name after the db_name, like so:
mysqldump -u user -p db_name table_name > backupfile_table_name.sql
Compressing the file(s) (a text file) is very efficient and can minimize it to about 20%-30% of it's original size.
Copying the files to remote servers should be done with scp (secure copy) and interaction should take place with ssh (usually).
Good luck.
I found that the advanced options in phpMyAdmin allow me to select how many rows to export, plus the start point. This allows me to create as many dump files as required to get the table onto the remote host.
I had to adjust my php.ini settings, plus the phpMyAdmin config 'ExecTimeLimit' setting
as generating the dump files takes some time (500,000 rows in each).
I use HeidiSQL to do the imports.
As an example of the mysqldump approach for a single table
mysqldump -u root -ppassword yourdb yourtable > table_name.sql
Importing is then as simple as
mysql -u username -ppassword yourotherdb < table_name.sql
Use mysqldump to dump the table into a file.
Then use tar with -z option to zip the file.
Transfer it to your remote server (with ftp, sftp or other file transfer utility).
Then untar the file on remote server
Use mysql to import the file.
There is no reason to split the original file or to export in multiple files.
If you are not comfortable with using the mysqldump command line tool, here are two GUI tools that can help you with that problem, although you have to be able to upload them to the server via FTP!
Adminer is a slim and very efficient DB Manager tool that is at least as powerful as PHPMyAdmin and has only ONE SINGLE FILE that has to be uploaded to the server which makes it extremely easy to install. It works way better with large tables / DB than PMA does.
MySQLDumper is a tool developed especially to export / import large tables / DBs so it will have no problem with the situation you describe. The only dowside is that it is a bit more tedious to install as there are more files and folders (~350 files in ~1.5MB), but it shouldn't be a problem to upload it via FTP either, and it will definately get the job done :)
So my advice would be to first try Adminer and if that one also fails go the MySQLDumper route.
How do I split a large MySQL backup file into multiple files?
You can use mysql_export_explode
https://github.com/barinascode/mysql-export-explode
<?php
#Including the class
include 'mysql_export_explode.php';
$export = new mysql_export_explode;
$export->db = 'dataBaseName'; # -- Set your database name
$export->connect('host','user','password'); # -- Connecting to database
$export->rows = array('Id','firstName','Telephone','Address'); # -- Set which fields you want to export
$export->exportTable('myTableName',15); # -- Table name and in few fractions you want to split the table
?>
At the end of the SQL files are created in the directory where the script is executed in the following format
---------------------------------------
myTableName_0.sql
myTableName_1.sql
myTableName_2.sql
...
I have a database backup with 400+ sql files. foreach table there is a separate sql file. Is it possible to import all this files together to a database? If so could you tell me how to do this?
Also the backup is a gzipped tar file. Is there a way to restore from a compressed file.?
If you are using linux Concatenate all the sql files using and
cat *.sql > fullBackup.sql
then you can restore the database using this backup file
I have found the answer for my question here. Import Multiple .sql dump files into mysql database from shell
find . -name '*.sql' | awk '{ print "source",$0 }' | mysql --batch works perfectly. Thanks for #Haim to pointing out the correct post.
Nowdays processors have many cores. To use all the cores:
for s in *.sql.gz ; do gunzip -c $s | mysql -u sql_user -p'password' database_name & done
This command opens background process for each sql-dump file.
Or, with pv installed, you can see also the progress by using:
pv -p *.sql | mysql database
I am trying to run a set of sql files with stored procedures and triggers in my windows XAMPP environment. Some suggested to me using a batch script but I do not know how to do this in windows.
Is it possible to run all these .sql files from within MySQL Workbench? How? If not, can anyone tell me how to run a batch file within windows?
Thank you.
It seems Workbench doesn't support the command "SOURCE" so the next best thing is is (at least in windows) is to run a batch job. Simply create a new .sql file and add the full path to each .sql file like so:
Create the batch file:
In windows, the batch file can be a .sql with the sql comman SOURCE which calls the other .sql files, like so:
create run.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_delete.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_insert.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_load.sql
Open Command Line and CD to MySQL Folder
Open the command line, and cd to MySQL. If you are using XAMPP, the command/location should be something like:
cd C:\xampp\mysql\bin\
Execute the Batch File by pressing ENTER
Last, simply load mysql and run the batch file using the following command:
mysql -u root -h 127.0.0.1 my_database_name -vvv < C:\xampp\htdocs\mysite\sql\procs\run.sql
The execution above means the following:
mysql -u <username> -h <host> <database> -vvv < <batch_path_file_name>
-vvv shows all the queries being executed and the rows affected for debugging.
That's it. All .sql files mentioned in the run.sql file will be executed.
Any idea how to do this restore ?
I looked into help of mysqldump but couldn't see it there .
If so can you give me some example.
With mysqldump you will generate a script you can use for restore on a different computer like this:
$ mysql -U user_name < your_backup.sql
Run on your favorite shell (windows command prompt, bash, csh...).
I think you can use CMD to navigate to the mysqldump location, then type this command,
mysqldump database_name -u username >location\to\save\dump.sql
change database_name to the database you want to backup, username to the username associated with the database, and location\to\save\dump.sql to the location where you want to save the output sql file, for me I wrote it D:\dump.sql
Then on the other machine you can import the SQL file using the PHPMyAdmin.
You can just execute the SQL using the mysql command-line command. There is a switch to specify which file to import, I think it is -I but I'm not sure.
It's just plain SQL. Pass the file to mysql (the mysql command line tool) and it will execute it:
mysql < backup.sql
From the shell prompt, using
parameters form the mysqldump
doc, mysqldump the database using a > redirect to a
human readable .sql file. E.g.
$ mysqldump --databases src_db > src_db.sql
Transfer the human readable file to
another machine.
After making sure the destination database exists has been created, redirect < the .sql file into the destination database.
$ mysql dest_db < src_db.sql