MySQL Dump to Directory - mysql

I'm currently trying to make a copy of a site to run locally, and i'm having some difficulty in downloading a dump of the mySQL database using PhpMyAdmin. (In that it doesn't work). I was wondering (and I suspect) if there is an sql command I can execute that would dump out the database to a location that I can download it via FTP.
I've found the following:
mysqldump --opt --user=root --password password > /home/backup/db.sql
but the page says that this will only run via shell, and I don't have shel access to the server. Can anyone help?

mysqldump -u root -p -T/path/ database

Simple Three steps Export mysql DB
**step1 :** run windows command prompt
**step2 :** make where yours wamp mysql stored directory ex E:\wamp\bin\mysql\mysql5.5.24\bin
**step 3 :**
E:\wamp\bin\mysql\mysql5.5.24\bin> mysqldump -u UserName -p PassWord dbname >c:\dbname.sql

PERFORM MySQL DATABASE DUMP:
Having registered mysql.exe in the Paths of Environment Variables (On
Windows Os)
Open windows console
Type the following Query:
mysqldump [Your database Name] -u root -p > C:/[Your Destination Directory ]/[ new Backup Database Name.sql ]
You will be prompted for a database user password
Enter Password:*******
when the dump is over...the directory will go back to the current
user session in Windows console
For this to work well, replace all parameters in square braces "[ ]" with your own values without the square braces

Open the Command prompt from MySQL bin folder (Make sure you have Read/Write Access)
Use the below mysqldump command
mysqldump -h [hostname] -u [username] -p [your database] > [your destination path][your filename.extn]
Ex: mysqldump -h localhost -u root -p test_DB > D:\MySQL\Dumps\test_file.sql
***WARNING : In case, if your custom path has any spaces in between, the above query will throw an error.
Invalid Path Description
To avoid Invalid Path Description, Give your path in double-quotes.
Ex : mysqldump -h localhost -u root -p test_DB > D:\MySQL\"Dumps from ABC"\test_file.sql

What are you looking for is Sypex Dumper 2. It exports (backups) your database directly to disk, so you can download the backup via ftp. It is written on php, supports large databases and has a very nice interface.

You can go to the directory where you want store the file, then execute de mysqldump command.
~/Desktop$ sudo /Applications/XAMPP/xamppfiles/bin/mysqldump -u root -p publicaciones > publicaciones.sql
PD. Use sudo to grant full permissions.
PD. If you have your PATH setup, don`t need use the full route to your MySql commands.

Related

Where does mysqldump files go?

I've read a post saying that I would have to use this command below, to dump my sql files
$ mysqldump -u [uname] -p db_name > db_backup.sql
However, my question is, I don't quite get where the db_backup.sql comes from. Am I suppose to create a file or is it being created as I enter any name for the backup sql?
Also, where can I find the backup.sql at?
More information: I am doing this on Mariadb/phpmyadmin on the xampp shell
Whichever directory you are in when you run that command will have db_backup.sql.
The statement:
mysqldump -u user -p db_name generates an output on the terminal screen, assuming you are SSH'ed into the server. Instead of the output written on the screen, you can redirect the output to a file.
To do that, you use > db_backup.sql after the command.
If you are in directory /home/hyunjae/backups and run the command:
$ mysqldump -u [uname] -p db_name > db_backup.sql
You will see a new file created called db_backup.sql. If there's already a file with that name, it will be overwritten.
If you don't know what directory you are in, you can type pwd for present working directory.
db_backup.sql is automatically generated by the mysqldump command, and is located in the current directory (.) by default. If you need to specify a directory, you can directly specify /path/to/target/db_backup.sql.

mysqldump Error 1045 Access denied despite correct passwords etc

This is a tricky one, I have the following output:
mysqldump: Got error: 1045: Access denied for user 'root'#'localhost' (using password: YES) when trying to connect
When attempting to export my database with mysqldump on Windows XP. The username is root, the password is correct and contains only alphanumeric characters. I have tried different cases, with/without quotes, specifying using -u and -p, specifying using --user= and --password= and other methods of specifying user/passwords etc, specifying the host (it's all local) and even specifying the database using --databases instead of just blank. The error is always the same when using a password and always the same except the "NO" message when without. I have tried many fixes found through searches with no success. One fix suggested inspecting mysql.conf, but the Windows build doesn't seem to have one. The credentials (and indeed commandline parameters) work perfectly with mysql.exe - this problem only seems to be affecting mysqldump.exe.
This worked for me
mysqldump -u root -p mydbscheme > mydbscheme_dump.sql
after issuing the command it asks for a password:
Enter password:
entering the password will make the dump file.
If you're able to connect to the database using mysql, but you get an error for mysqldump, then the problem may be that you lack privileges to lock the table.
Try the --single-transaction option in that case.
mysqldump -h database.example.com -u mydbuser -p mydatabase --single-transaction > /home/mylinuxuser/mydatabase.sql
Try to remove the space when using the -p-option. This works for my OSX and Linux mysqldump:
mysqldump -u user -ppassword ...
The access being denied is probably to the Windows file system not to the MySQL database; try redirecting the output file to a location where your account is allowed to create files.
You need to put backslashes in your password that contain shell metacharacters, such as !#'"`&;
Don't enter the password with command. Just enter,
mysqldump -u <username> -p <db_name> > <backup_file>.sql
Then you will get a prompt to enter password.
Access dined problem solved when I run command prompt in Administrator mode.
Go to Start-> All Programs -> Accessories right click on Command Prompt clickc on Run as.. Select The Following User select administrator username from select option enter password if any click OK button.
Example 1: For entire database backup in mysql using command prompt.
In Windows 7 and 8
C:\Program Files <x86>>\MySQL\MySQL Server 5.5\bin>mysqldump test -u root -p >testDB.sql
Enter Password: *********
In Windows xp
C:\Program Files\MySQL\MySQL Server 5.5\bin>mysqldump test -u root -p >testDB.sql
Enter Password: *********
It asks password for credentials enter password and click on Enter button.
Example 2: For specific table backup / dump in mysql using command prompt.
In Windows 7 and 8
C:\Program Files <x86>>\MySQL\MySQL Server 5.5\bin>mysqldump test -u root -p images>testDB_Images.sql
Enter Password: *********
In Windows xp
C:\Program Files\MySQL\MySQL Server 5.5\bin>mysqldump test -u root -p images>testDB_Images.sql
Enter Password: *********
Dumpt file will be created under folder
In windows xp
C:\Program Files\MySQL\MySQL Server 5.5\bin
In windows 7 and 8
C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin
Note: Check MySQL installation folder in Windows 7, 8 while run in command prompt. If MySQLWorkbench is 32 bit version it is installed in Program Files (x86) folder other wise Program Files folder.
Put The GRANT privileges:
GRANT ALL PRIVILEGES ON mydb.* TO 'username'#'%' IDENTIFIED BY 'password';
mysqldump -h hostname -u username -P port -B database --no-create-info -p > output.sql
I think you should specify the args
Doing without the -u and -p worked for me (when I was logged in as root):
mysqldump --opt mydbname > mydbname.sql
I was having the same issue, for 30min! I found that I was using _p instead of -p, the terminal font confused me!
Putting -p as the first option worked for me on Windows Server 2012R2 (in cmd.exe as Admin).
mysqldump.exe –p --user=root --databases DBname --result-file=C:\DBname.sql
I just ran into this after a fresh install of MySQL 5.6.16.
Oddly, it works without the password specified or flagged:
mysqldump -u root myschema mytable > dump.sql
mysqldump -u (user) -p(passwd) -h (host_or_IP) database_to_backup > backup_file.sql
example:
mysqldump -u god -pheaven -h 10.0.10.10 accounting > accounting_20141209.sql
this would create sql backup file for the accounting database on server 10.0.10.10. Sometimes your error is seen when localhost is not in config. Designating ip of server may help.
I had to remove the single ticks after the password flag:
--password=mypassword
and NOT
--password='mypassword'
Mysql replies with Access Denied with correct credentials when the mysql account has REQUIRE SSL on
The ssl_ca file (at a minimum) had to be provided in the connection paramiters.
Additional ssl parameters might be required and are documented here: http://dev.mysql.com/doc/refman/5.7/en/secure-connection-options.html
Also posted here https://stackoverflow.com/a/39626932/1695680
For MAMP PRO users (or anyone who's mysql is in a weird location) be prepared to specify the mysql full path from the boonies and also specify full path to your user local folder where you want to dump the file or you'll get the "permission denied error"..
Following worked for me after 3 hours of research:
/Applications/MAMP/Library/bin/mysqldump -u root -proot YOUR_DB > /Users/YOUR_USER/yourdump2.sql
In my case, I could access correctly with mysql.exe but not with mysqldump.exe.
The problem was the port for my connection was not the default one (3306) and I had to put the mysqldump port work with (-P3307)
mysqldump -u root -p -P3307 my_database > /path/backup_database
This is the solution that worked for me
mysqldump -h hostname.com -u username -p'password' database > dump.sql
In Past same problem occurred to me after I copied the mysqldump statement from a MS Word file.
But When typing the statement directly, everything worked fine.
In hex editor the "-" of the not working statement was represented by the unicode char e2 80 93 (http://www.fileformat.info/info/unicode/char/2013/index.htm)
In sort, type password directly and check the copy paste code as the uni-code (or other encoding) strings might cause an issue..
I had the same error for last 2 days. Tried bunch of things. Nothing worked.
But this did work:
Create another user. Grant it everything.
mysqldump -u new_user db_name > db_name.sql //no error
I discovered a running apache process acessing the MYSQL causing this error. So I suggest to ensure that all processes which might interact with the DB are shutdown beforehand.
I had the problem that there were views that had a bad "DEFINER", which is the user that defined the view. The DEFINER used in the view had been removed some time ago as being "root from some random workstation".
Check whether there might be a problem by running:
USE information_schema;
SELECT DEFINER, SECURITY_TYPE FROM views;
I modified the DEFINER (actually, set the DEFINER to root#localhost and the SQL SECURITY value to INVOKER so the view is executed with the permissions of the invoking user instead of the defining user, which actually makes more sense) using ALTER VIEW.
This is tricky as you have to construct the appropriate ALTER VIEW statement from information_schema.views, so check:
Modify DEFINER on many
views
MySQL error 1449: The user specified as a definer does not exist
For me it worked when I omitted the password.
So mysqldump -u user dbname > dump.sql
Tried most of the above with no joy.
Looking at my password, it had characters that might confuse a parser. I wrapped the password in quotes and the error was resolved.
-p"a:##$%^&+6>&FAEH"
Using 8.0
If you want to create a mysql data dump, you can use mysqldump command. Following command will create a sql file called xxx.sql at the same location from where this command is run. xxx.sql will have all the necessary sqls to replicate exactly same db schema in any other mysql database.
Command is : mysqldump -u root -ppassword --databases database Name you want to import > xxx.sql
Here root is the mysql root user and password is THIS root user's password.
EXAMPLE: If root user password is hello, database name to export is regdb and xxx.sql is the file where you want to export this regdb, command would be like:
mysqldump -u root -phello --databases regdb > xxx.sql
Note: xxx.sql is the file name where this db will get dumped.
This solution might be one of the last to try/least likely to be the culprit, but this was my problem...
My problem was that the directory I was trying to dump to needed admin privileges to write to and that's what was causing the mysqldump command to return "Access Denied".
I set the dump file path to my desktop dir and then it worked.
This was on Windows.
I had the same error. Only occurred after moving from my normal work PC to a PC at a different location.
I had to add my public IP ho address to Remote MySQL in my CPanel at my host site
I got the same error when I ran the command in a directory that I didn't have write access to.
Test your access by creating an empty file in the directory, and see if you get an error.
Here was my error
mysqldump -u root librenms -p > librenms.sql
-bash: librenms.sql: Permission denied
I changed to my home directory and then it worked.
cd ~
mysqldump -u root librenms -p > librenms.sql
Enter password:
Do the equivalent on windows, and it may just fix your problem!
ENSURE YOU TRY REMOVING AND TYPING THE DASH OVER to make sure that you are actually fighting with the right problem.
Be very careful that you actually have a "-". I apparently had some other character that looks very similar. I had a – instead of a -. I had copied the command from somewhere online don't remember where but the point is I spend a lot of time trying to figure it out when I just needed to replace that character.

How do I import an SQL file using the command line in MySQL?

I have a .sql file with an export from phpMyAdmin. I want to import it into a different server using the command line.
I have a Windows Server 2008 R2 installation. I placed the .sql file on the C drive, and I tried this command
database_name < file.sql
It is not working. I get syntax errors.
How can I import this file without a problem?
Do I need to create a database first?
Try:
mysql -u username -p database_name < file.sql
Check MySQL Options.
Note 1: It is better to use the full path of the SQL file file.sql.
Note 2: Use -R and --triggers with mysqldump to keep the routines and triggers of the original database. They are not copied by default.
Note 3 You may have to create the (empty) database from MySQL if it doesn't exist already and the exported SQL doesn't contain CREATE DATABASE (exported with --no-create-db or -n option) before you can import it.
A common use of mysqldump is for making a backup of an entire database:
mysqldump db_name > backup-file.sql
You can load the dump file back into the server like this:
Unix
mysql db_name < backup-file.sql
The same in the Windows command prompt:
mysql -p -u [user] [database] < backup-file.sql
PowerShell
cmd.exe /c "mysql -u root -p db_name < backup-file.sql"
MySQL command line
mysql> use db_name;
mysql> source backup-file.sql;
Regarding the time taken for importing huge files: most importantly, it takes more time because the default setting of MySQL is autocommit = true. You must set that off before importing your file and then check how import works like a gem.
You just need to do the following thing:
mysql> use db_name;
mysql> SET autocommit=0 ; source the_sql_file.sql ; COMMIT ;
Among all the answers, for the problem above, this is the best one:
mysql> use db_name;
mysql> source file_name.sql;
Easiest way to import into your schema:
Login to mysql and issue below mention commands.
mysql> use your_db_name;
mysql> source /opt/file.sql;
We can use this command to import SQL from the command line:
mysql -u username -p password db_name < file.sql
For example, if the username is root and password is password. And you have a database name as bank and the SQL file is bank.sql. Then, simply do like this:
mysql -u root -p password bank < bank.sql
Remember where your SQL file is. If your SQL file is in the Desktop folder/directory then go the desktop directory and enter the command like this:
cd ~/Desktop
mysql -u root -p password bank < bank.sql
And if you are in the Project directory and your SQL file is in the Desktop directory. If you want to access it from the Project directory then you can do like this:
cd ~/Project
mysql -u root -p password bank < ~/Desktop/bank.sql
If you already have the database, use the following to import the dump or the sql file:
mysql -u username -p database_name < file.sql
if you don't you need to create the relevant database(empty) in MySQL, for that first log on to the MySQL console by running the following command in terminal or in cmd
mysql -u userName -p;
And when prompted provide the password.
Next, create a database and use it:
mysql>create database yourDatabaseName;
mysql>use yourDatabaseName;
Then import the sql or the dump file to the database from
mysql> source pathToYourSQLFile;
Note: if your terminal is not in the location where the dump or sql file exists, use the relative path in above.
Open the MySQL command line
Type the path of your mysql bin directory and press Enter
Paste your SQL file inside the bin folder of mysql server.
Create a database in MySQL.
Use that particular database where you want to import the SQL file.
Type source databasefilename.sql and Enter
Your SQL file upload successfully.
A solution that worked for me is below:
Use your_database_name;
SOURCE path_to_db_sql_file_on_your_local;
While most answers here just mention the simple command
mysql -u database_user -p [db_name] < database_file.sql
today it's quite common that databases and tables have utf8-collation where this command is not sufficient.
Having utf8-collation in the exported tables it's required to use this command:
mysql -u database_user -p --default-character-set=utf8 [db_name] < database_file.sql
An according export can be done with
mysqldump -u database_user -p --default-character-set=utf8 [db_name] > database_file.sql
Surely this works for other charsets too, how to show the right notation can be seen here:
https://dev.mysql.com/doc/refman/5.7/en/show-collation.html
One comment mentioned also that if a database never exists an empty database had to be created first. This might be right in some cases but depends on the export file. If the exported file includes already the command to create the database then the database never has to be created in a separate step, which even could cause an error on import. So on import, it's advisable to have a look first in the file to know which commands are included there, on export, it's advisable to note the settings, especially if the file is very large and hard to read in an editor.
There are still more parameters for the command which are listed and explained here:
https://dev.mysql.com/doc/refman/5.7/en/mysql-command-options.html
If you use another database version consider searching for the corresponding version of the manual too. The mentioned links refer to MySQL version 5.7.
EDIT:
The same parameters are working for mysqldump too. So while the commands for export and import are different, the mentioned parameters are not.
Nevertheless there exists a special site in the manual that describes the options for mysqldump: https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html
To dump a database into an SQL file use the following command.
mysqldump -u username -p database_name > database_name.sql
To import an SQL file into a database (make sure you are in the same directory as the SQL file or supply the full path to the file), do:
mysql -u username -p database_name < database_name.sql
I think it's worth mentioning that you can also load a gzipped (compressed) file with zcat like shown below:
zcat database_file.sql.gz | mysql -u username -p -h localhost database_name
Go to the directory where you have the MySQL executable. -u for username and -p to prompt for the password:
C:\xampp\mysql\bin>mysql -u username -ppassword databasename < C:\file.sql
To import a single database, use the following command.
mysql -u username -p password dbname < dump.sql
To import multiple database dumps, use the following command.
mysql -u username -p password < dump.sql
To import a database, use the following command.
mysql> create new_database;
mysql> use new_database;
mysql> source (Here you need to import the path of the SQL file);
E.g.:
mysql> source E:/test/dump.sql;
You need to use forward slashes (/) even on Windows, e.g., E:/test/dump.sql instead of E:\test\dump.sql
Or double backslashes (\\) because of escaping, i.e., E:\\test\\dump.sql
mysql --user=[user] --password=[password] [database] < news_ml_all.sql
I kept running into the problem where the database wasn't created.
I fixed it like this:
mysql -u root -e "CREATE DATABASE db_name"
mysql db_name --force < import_script.sql
For exporting a database:
mysqldump -u username -p database_name > file.sql
For importing a database:
mysql -u username -p database_name < file.sql
For importing multiple SQL files at one time, use this:
# Unix-based solution
for i in *.sql ; do mysql -u root -pPassword DataBase < $i ; done
For simple importing:
# Unix-based solution
mysql -u root -pPassword DataBase < data.sql
For WAMP:
REM mysqlVersion - replace with your own version
C:\wamp\bin\mysql\mysqlVersion\bin\mysql.exe -u root -pPassword DataBase < data.sql
For XAMPP:
C:\xampp\mysql\bin\mysql -u root -pPassword DataBase < data.sql
You do not need to specify the name of the database on the command line if the .sql file contains CREATE DATABASE IF NOT EXISTS db_name and USE db_name statements.
Just make sure you are connecting with a user that has the permissions to create the database, if the database mentioned in the .sql file does not exist.
Import a database
Go to drive:
d:
MySQL login
c:\xampp\mysql\bin\mysql -u root -p
It will ask for pwd. Enter it:
pwd
Select the database
use DbName;
Provide the file name
\.DbName.sql
Use:
mysql -u root -p password -D database_name << import.sql
Use the MySQL help for details - mysql --help.
I think these will be useful options in our context:
[~]$ mysql --help
mysql Ver 14.14 Distrib 5.7.20, for osx10.12 (x86_64) using EditLine wrapper
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--bind-address=name IP address to bind to.
-D, --database=name Database to use.
--delimiter=name Delimiter to be used.
--default-character-set=name Set the default character set.
-f, --force Continue even if we get an SQL error.
-p, --password[=name] Password to use when connecting to server.
-h, --host=name Connect to host.
-P, --port=# Port number to use for connection or 0 for default to, in order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default (3306).
--protocol=name The protocol to use for connection (tcp, socket, pipe,
-s, --silent Be more silent. Print results with a tab as separator, each row on new line.
-v, --verbose Write more. (-v -v -v gives the table output format).
-V, --version Output version information and exit.
-w, --wait Wait and retry if connection is down.
What is fun, if we are importing a large database and not having a progress bar. Use Pipe Viewer and see the data transfer through the pipe
For Mac, brew install pv
For Debian/Ubuntu, apt-get install pv.
For others, refer to pv - Pipe Viewer
pv import.sql | mysql -u root -p password -D database_name
1.45GiB 1:50:07 [339.0KiB/s] [=============> ] 14% ETA 11:09:36
1.46GiB 1:50:14 [ 246KiB/s] [=============> ] 14% ETA 11:09:15
1.47GiB 1:53:00 [ 385KiB/s] [=============> ] 14% ETA 11:05:36
Go to the directory where you have MySQL.
c:\mysql\bin\> mysql -u username -p password database_name <
filename.sql
Also to dump all databases, use the -all-databases option, and no databases’ name needs to be specified anymore.
mysqldump -u username -ppassword –all-databases > dump.sql
Or you can use some GUI clients like SQLyog to do this.
You can try this query.
Export:
mysqldump -u username –-password=your_password database_name > file.sql
Import:
mysql -u username –-password=your_password database_name < file.sql
and detail following this link:
https://chartio.com/resources/tutorials/importing-from-and-exporting-to-files-using-the-mysql-command-line/
Add the --force option:
mysql -u username -p database_name --force < file.sql
The following command works for me from the command line (cmd) on
Windows 7 on WAMP.
d:/wamp/bin/mysql/mysql5.6.17/bin/mysql.exe -u root -p db_name < database.sql
Providing credentials on the command line is not a good idea. The above answers are great, but neglect to mention
mysql --defaults-extra-file=etc/myhost.cnf database_name < file.sql
Where etc/myhost.cnf is a file that contains host, user, password, and you avoid exposing the password on the command line. Here is a sample,
[client]
host=hostname.domainname
user=dbusername
password=dbpassword
Import into the database:
mysql -u username -p database_name < /file path/file_name.sql
Export from the database:
mysqldump -u username -p database_name > /file path/file_name.sql
After these commands, a prompt will ask for your MySQL password.
Similarly to vladkras's answer to How do import an SQL file using the command line in MySQL?.
Key differences for me:
The database has to exist first
No space between -p and the password
shell> mysql -u root -ppassword #note: no space between -p and password
mysql> CREATE DATABASE databasename;
mysql> using databasename;
mysql> source /path/to/backup.sql
I am running Fedora 26 with MariaDB.
I thought it could be useful for those who are using Mac OS X:
/Applications/xampp/xamppfiles/bin/mysql -u root -p database < database.sql
Replace xampp with mamp or other web servers.

windows batch file for mysqldump command

this is first time I am upto making a windows batch file.I wish not to do experiments first as its related to live server.
I use to back up mySql database by following
open cmd
write
mysqldump -u user -p DBname >C:\DBname.sql
then cmd ask password
password = password
I want to make a windows batch file ,upon click I wish to perform the above functionality.
Can any one please give some instructions related script of .bat file
So far I wrote this
ECHO OFF
mysqldump -u user -p DBname >C:\DBname.sql
GO
SET password = password
Go
but still it prompts me for password .
I wish password get enter automatically
The above was still prompting me for a password I used whats below to get it to work correctly.
"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\mysqldump.exe" -uroot -pDBPass DBName > C:\DBname.sql
The one-liner:
mysqldump -u user -p DBname --password YOURPASSWORD > C:\DBname.sql
should do it.
You can use this:
"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" -u root -proot test > D:\DBname.sql
Please note this: -proot, the p and root must be together
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe" -u root -pmypassword --databases mydatabase --hex-blob > C:\mybackup.sql
Paste into a bat file, I couldnt get the plain command to work in task scheduler so used a BAT
You can omit the --hex-blob, its there incase you're using blob columns

How can I import a database with MySQL from terminal?

How can I import a database with mysql from terminal?
I cannot find the exact syntax.
Assuming you're on a Linux or Windows console:
Prompt for password:
mysql -u <username> -p <databasename> < <filename.sql>
Enter password directly (not secure):
mysql -u <username> -p<PlainPassword> <databasename> < <filename.sql>
Example:
mysql -u root -p wp_users < wp_users.sql
mysql -u root -pPassword123 wp_users < wp_users.sql
See also:
4.5.1.5. Executing SQL Statements from a Text File
Note: If you are on windows then you will have to cd (change directory) to your MySQL/bin directory inside the CMD before executing the command.
Preferable way for windows:
Open the console and start the interactive MySQL mode
use <name_of_your_database>;
source <path_of_your_.sql>
mysql -u <USERNAME> -p <DB NAME> < <dump file path>
-u - for Username
-p - to prompt the Password
Eg. mysql -u root -p mydb < /home/db_backup.sql
You can also provide password preceded by -p but for the security reasons it is not suggestible. The password will appear on the command itself rather masked.
Directly from var/www/html
mysql -u username -p database_name < /path/to/file.sql
From within mysql:
mysql> use db_name;
mysql> source backup-file.sql
Open Terminal Then
mysql -u root -p
eg:- mysql -u shabeer -p
After That Create a Database
mysql> create database "Name";
eg:- create database INVESTOR;
Then Select That New Database "INVESTOR"
mysql> USE INVESTOR;
Select the path of sql file from machine
mysql> source /home/shabeer/Desktop/new_file.sql;
Then press enter and wait for some times if it's all executed then
mysql> exit
From Terminal:
mysql -uroot -p --default-character-set=utf8 database_name </database_path/database.sql
in the terminal type
mysql -uroot -p1234; use databasename; source /path/filename.sql
Below command is working on ubuntu 16.04, I am not sure it is working or not other Linux platforms.
Export SQL file:
$ mysqldump -u [user_name] -p [database_name] > [database_name.sql]
Example : mysqldump -u root -p max_development > max_development.sql
Import SQL file:
$ mysqldump -u [user_name] -p [database_name] < [file_name.sql]
Example: mysqldump -u root -p max_production < max_development.sql
Note SQL file should exist same directory
I usually use this command to load my SQL data when divided in files with names : 000-tableA.sql, 001-tableB.sql, 002-tableC.sql.
for anyvar in *.sql; do <path to your bin>/mysql -u<username> -p<password> <database name> < $anyvar; done
Works well on OSX shell.
Explanation:
First create a database or use an existing database. In my case, I am using an existing database
Load the database by giving <name of database> = ClassicModels in my case and using the operator < give the path to the database = sakila-data.sql
By running show tables, I get the list of tables as you can see.
Note : In my case I got an error 1062, because I am trying to load the same thing again.
mysql -u username -ppassword dbname < /path/file-name.sql
example
mysql -u root -proot product < /home/myPC/Downloads/tbl_product.sql
Use this from terminal
After struggling for sometime I found the information in https://tommcfarlin.com/importing-a-large-database/
Connect to Mysql (let's use root for both username and password):
mysql -uroot -proot
Connect to the database (let's say it is called emptyDatabase (your should get a confirmation message):
connect emptyDatabase
3 Import the source code, lets say the file is called mySource.sql and it is in a folder called mySoureDb under the profile of a user called myUser:
source /Users/myUser/mySourceDB/mySource.sql
Open the MySQL Command Line Client and type in your password
Change to the database you want to use for importing the .sql file data into. Do this by typing:
USE your_database_name
Now locate the .sql file you want to execute.
If the file is located in the main local C: drive directory and the .sql script file name is currentSqlTable.sql, you would type the following:
\. C:\currentSqlTable.sql
and press Enter to execute the SQL script file.
If you are using sakila-db from mysql website,
It's very easy on the Linux platform just follow the below-mentioned steps, After downloading the zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and the other one is sakila-data.sql.
Open terminal
Enter command mysql -u root -p < sakila-schema.sql
Enter command mysql -u root -p < sakila-data.sql
Now enter command mysql -u root -p and enter your password, now you have entered into mysql system with default database.
To use sakila database, use this command use sakila;
To see tables in sakila-db, use show tables command
Please take care that extracted files are present in home directory.
First connect to mysql via command line
mysql -u root -p
Enter MySQL PW
Select target DB name
use <db_name>
Select your db file for import
SET autocommit=0; source /root/<db_file>;
commit;
This should do it. (thanks for clearing)
This will work even 10GB DB can be imported successfully this way. :)
In Ubuntu, from MySQL monitor, you have already used this syntax:
mysql> use <dbname>
-> The USE statement tells MySQL to use dbname as the default database for subsequent statements
mysql> source <file-path>
for example:
mysql> use phonebook;
mysql> source /tmp/phonebook.sql;
Important: make sure the sql file is in a directory that mysql can access to like /tmp
If you want to import a database from a SQL dump which might have "use" statements in it, I recommend to use the "-o" option as a safeguard to not accidentially import to a wrong database.
• --one-database, -o
Ignore statements except those those that occur while the default
database is the one named on the command line. This filtering is
limited, and based only on USE statements. This is useful for
skipping updates to other databases in the binary log.
Full command:
mysql -u <username> -p -o <databasename> < <filename.sql>
For Ubuntu/Linux users,
Extract the SQL file and paste it somewhere
e.g you pasted on desktop
open the terminal
go to your database and create a database name
Create database db_name;
Exit Mysql from your terminal
cd DESKTOP
mysql -u root -p db_name < /cd/to/mysql.sql
Enter the password:....
Before running the commands on the terminal you have to make sure that you have MySQL installed on your terminal.
You can use the following command to install it:
sudo apt-get update
sudo apt-get install mysql-server
Refrence here.
After that you can use the following commands to import a database:
mysql -u <username> -p <databasename> < <filename.sql>
The simplest way to import a database in your MYSQL from the terminal is done by the below-mentioned process -
mysql -u root -p root database_name < path to your .sql file
What I'm doing above is:
Entering to mysql with my username and password (here it is root & root)
After entering the password I'm giving the name of database where I want to import my .sql file. Please make sure the database already exists in your MYSQL
The database name is followed by < and then path to your .sql file. For example, if my file is stored in Desktop, the path will be /home/Desktop/db.sql
That's it. Once you've done all this, press enter and wait for your .sql file to get uploaded to the respective database
There has to be no space between -p and password
mysql -u [dbusername] -p[dbpassword] [databasename] < /home/serverusername/public_html/restore_db/database_file.sql
I always use it, it works perfectly. Thanks to ask this question. Have a great day. Njoy :)