Backup DB from MYSQL command prompt - mysql

I recently tried to put my web application and mysql database on the same virtual server. Now I receive a port error. In order to fix the port issue, I want to back up the database and reinstall WAMP. Since I can't get to the phymyadmin, I have to use the mysql command prompt to do a dump.
Would anyone happen to know the EXACT command to put in, if my username is root, I have no password, and the database name is dev? I've tried it multiple ways and I get an error saying that my syntax is wrong.
Thanks in advance.

You can use the following command provided you know the name of the database:
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
Alternatively I would install the MySQL Workbench which can connect to your local database instance and provides a GUI to run the export: MySQL Workbench.
Unfortunately if you've uninstalled WAMP then MySQL might not be running anymore, and the above commands will not work because the utilities cannot see the database. If this is the case, try installing WAMP to a new directory and manually copying over the MySQL data files:
How to restore MySQL database from WAMP?

mysqldump -u root -p dev > devBackUp.sql

DOS batch file to export the database with date and time in the filename
FOR /f "tokens=1-8 delims=:./ " %%G IN ("%date%_%time%") DO (
SET dt=%%G%%H%%I_%%J_%%K
)
mysqldump -u root -p[root_password] [database_name]> [database_name]_%dt%.sql
pause

Related

Transfering mysql data from wamp enviroment to centOS

I have copy of .sql file that contains large data. I saved it from phpmyadmin while I was using WAMP for development. Now I am working with CentOS, and I have transferred the data to my VirtualBox running CentOs already.
So, the problem is not about transferring the file but running the .sql file using shell, so the data can be transferred to the new mysql server.
Does anyone know any commands?
Initially I thought moving this entire directory:
C:\wamp\bin\mysql\mysql5.5.24\data
To my new server environment would be a good idea, but I can't seem to find where the data folder is kepyt in centos-mysql.
whereis mysql gives mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
I have checked all this folders to find the data folder but to no avail.
if you created the .sql file with mysqldump or if it is otherwise a legal mysql script containing sql commands you can simply pipe this contents to your centOs mysql instance:
$ mysql -uroot -p dbname < dump.sql
where dbname is the name of your database and dump.sql your .sql file.
Follow these steps:
1- In your Windows Environment, from command line(CMD), go to the folder:
cd "C:\wamp\bin\mysql\mysql5.5.24\bin
2- Run: mysqldump -uroot -pYourPassword DataBaseName > myBackup.sql
3- On your centOs machine, open a terminal:
mysql -uroot -p
4- In mysql console:
create database DataBaseName;
exit;
5- Transfer the myBackup from your Windows System, to centOS, open a terminal in
the same directory where myBackup.sql lives:
mysql -uroot -p DataBaseName < myBackup.sql

How To Export/Import Large Database On MAMP

How do I export/import large database on MAMP ? Using PHPMyAdmin does not work as it supposed to be.
It should be done via terminal as below.
In the terminal navigate to bin folder of MAMP using below command cd /Applications/MAMP/library/bin
Use this command to export the file ./mysqldump -u [USERNAME] -p [DATA_BASENAME] > [PATH_TO_FILE]. EG would be ./mysqldump -u root -p my_database_name > /Applications/MAMP/htdocs/folder_name/exported_db.sql
Line should appear saying Enter password:. Here enter the MySQL password. keep in mind that the letters will not appear, but they are there.
If you need to import use BigDump Which is a MySQL Dump Importer.
Turn on MAMP!
Then, for both operations, open the terminal and type:
EXPORTING:
/Applications/MAMP/library/bin/mysqldump -u [USERNAME] -p [DATABASE_NAME] > [PATH_TO_SQL_FILE]
Then type your password in prompter (default root) and press enter.
Example:
/Applications/MAMP/library/bin/mysqldump -u root -p my_database_name > /Applications/MAMP/htdocs/folder_name/exported_db.sql
IMPORTING (will erase current database):
/Applications/MAMP/library/bin/mysql -u [USERNAME] -p [DATABASE_NAME] < [PATH_TO_SQL_FILE]
Then type your password in prompter (default root) and press enter.
Example:
/Applications/MAMP/library/bin/mysql -u root -p my_database_name < /Applications/MAMP/htdocs/folder_name/exported_db.sql
/!\ Important Warning: Make sure to backup your current database before doing this command in case you need a copy of it before importing. This will erase your current database!
You can also use the Sequel Pro App if you are using MAMP PRO version to get around the defaults that phpMyAdmin gives you.
I had a problem to dump my db from MAMP. Tried a lot of things, but always this error:
Got error: 2002: Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock'
Tried to create a symlink, but it was already created "ln: /tmp/mysql.sock: File exists"
Thought the problem was new Mac or new MAMP.
Finally in my case (problem was in me) solution was really simple: just turn on MAMP :)
Maybe it will help someone to save a little bit time (I spent around 4 hours)

Back up MYSQL data after loosing access to PHPMYADMIN interface

I just want to know how to back up data From MYSQL database and Still be able to use that data
again on reinstalling MYSQL ?
NOTE-- i got into this problem after changing my ROOT password . I posted a question regarding fixing this .But unfortunately don't got the solution .So i decided the last step will be to BACK UP my data !
Here is a LINK to that
post
Can't connect after changing MySQL root password in WAMP
P.S----I HAVE NO ACCESS TO PHPMYADMIN
go to your wamp installation drive, and then go to wamp folder
traverse it if the drive is C: and installation folder is WAMP
C:\wamp\mysql\data\
you will see the folder as the name of your database here
copy it to other place and paste it after reinstall phpmyadmin in the same position
Okay, assuming that you know the new/old/current root passwords run the following
mysqldump -u root --all-databases -p
This should ask you the password and dump sql onto the stdout.
To save it to backup.sql,
mysqldump -u root --all-databases -p > backup.sql

How do you use MySQL's source command to import large files in windows

I have a large (~150mb) sql file that I am trying to import. It is too large to do it through PHPMyAdmin even splitting it into many pieces and it is too large to import through a php script as it times out after 30 seconds of processing the script. So I'm looking for how to directly import the file from MySQL command line.
Searching online shows that I want to either use database_name < file_name.sql or source file_name.sql but I can't get either of these to work.
Using < gives the generic MySQL syntax error while using source give a slightly more promising failed to open file 'file_name.sql', error: 2 so I am inclined to think that the source command is on the right track.
I am in windows and am using xampp as a localhost server (note I'm only trying to import this file on the localhost so that I can execute the sql). I've tried placing the file in xampp\mysql\bin and xampp\mysql\data\database_name.
Any suggestions of how to import this .sql file into MySQL either from the MySQL command line or by any other means would be greatly appreciated.
On Windows this should work (note the forward slash and that the whole path is not quoted and that spaces are allowed)
USE yourdb;
SOURCE D:/My Folder with spaces/Folder/filetoimport.sql;
With xampp I think you need to use the full path at the command line, something like this, perhaps:
C:\xampp\mysql\bin\mysql -u {username} -p {databasename} < file_name.sql
Don't use "source", it's designed to run a small number of sql queries and display the output, not to import large databases.
I use Wamp Developer (not XAMPP) but it should be the same.
What you want to do is use the MySQL Client to do the work for you.
Make sure MySQL is running.
Create your database via phpMyAdmin or the MySQL shell.
Then, run cmd.exe, and change to the directory your sql file is
located in.
Execute: mysql -u root -p database_name_here < dump_file_name_here.sql
Substitute in your database name and dump file name.
Enter your MySQL root account password when prompted (if no password set, remove the "-p" switch).
This assumes that mysql.exe can be located via the environmental path, and that sql file is located in the directory you are running this from. Otherwise, use full paths.
Option 1. you can do this using single cmd where D is my xampp or wampp install folder so i use this where mysql.exe install and second option database name and last is sql file so replace it as your then run this
You can try this:
mysql -u root -p test < /test.sql
Another option
D:\xampp\mysql\bin\mysql.exe -u root -p databse_name < D:\yoursqlfile.sql
Option 1 for wampp
D:\wamp64\bin\mysql\mysql5.7.14\bin\mysql.exe -u root -p databse_name< D:\yoursqlfile.sql
change your folder and mysql version
Option 2 Suppose your current path is which is showing command prompt
C:\Users\shafiq;
then change directory using cd..
then goto your mysql directory where your xampp installed. Then cd.. for change directory. then go to bin folder.
C:\xampp\mysql\bin;
C:\xampp\mysql\bin\mysql -u {username} -p {database password}.then please enter when you see enter password in command prompt.
choose database using
mysql->use test (where database name test)
then put in source sql in bin folder.
then last command will be
mysql-> source test.sql (where test.sql is file name which need to import)
then press enter
This is full command
C:\Users\shafiq;
C:\xampp\mysql\bin
C:\xampp\mysql\bin\mysql -u {username} -p {database password}
mysql-> use test
mysql->source test.sql
C:\xampp\mysql\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
That worked for me to import 400MB file into my database.
For importing a large SQL file using the command line in MySQL.
First go to file path at the command line.
Then,
Option 1:
mysql -u {user_name} -p{password} {database_name} < your_file.sql
It's give a warning mesaage : Using a password on the command line interface can be insecure.
Done.Your file will be imported.
Option 2:
mysql -u {user_name} -p {database_name} < your_file.sql
in this you are not provide sql password then they asked for password just enter password and your file will be imported.
use mysql source command to avoid redirection failures, especially on windows.
mysql [-u <username>] [-p<password>] <databasename> -e "source /path/to/dump.sql"
where e for "Execute command"
On Windows, please remember to use double quote for sql command.
However, either backslash \ or slash / will work on Windows.
Hello I had the same problem but I tried many different states and I came to it:
SOURCE doesn't work with ; at the end in my case:
SOURCE D:\Barname-Narmafzar\computer programming's languages\SQL\MySQL\dataAug-12-2019\dataAug-12-2019.sql;
and the error was:
ERROR: Unknown command '\B'.
'>
it also didn't work with a quotation for the address.
But it works without ; at the end:
SOURCE D:\Barname-Narmafzar\computer programming's languages\SQL\MySQL\dataAug-12-2019\dataAug-12-2019.sql
But remember to use USE database_name; before that.
I think it's so because the SOURCE or USE or HELP are for the Mysql itself and they are not such query codes although when you write HELP it says:
"Note that all text commands must be first on line and end with ; ".
but here doesn't work.
I should say that I have done it in CMD and I didn't try it in Mysql Workbench.
That was it
This is the result
On my Xampp set-up I was able to use the following to import a database into MySQL:
C:\xampp\mysql\bin\mysql -u {username goes here} -p {leave password blank} {database name} < /path/to/file.sql [enter]
My personal experience on my local machine was as follows:
Username: Root
Database Name: testdatabase
SQL File Location: databasebackup.sql is located on my desktop
C:\xampp\mysql\bin\mysql -u root -p testdatabase < C:\Users\Juan\Desktop\databasebackup.sql
That worked for me to import my 1GB+ file into my database.
Username as root without password
mysql -h localhost -u root databasename < dump.sql
I have faced the problem on my local host as i don't have any password for root user. You can use it without -p password as above. If it ask for password, just hit enter.
On windows:
Use explorer to navigate to the folder with the .sql file.
Type cmd in the top address bar. Cmd will open.
Type:
"C:\path\to\mysql.exe" -u "your_username" -p "your password" < "name_of_your_sql_file.sql"
Wait a bit and the sql file will have been executed on your database.
Confirmed to work with MariaDB in feb 2018.

mysql import on windows

I have a MySQL file, db.sql. I have tried to import it using:
mysql -uroot -p[password] db < db.sql
All I get is a listing of mysql commands, or I get a syntax error. The weird thing is I used this file last week and, as far as I know, I'm doing it the same way.
I create the database, then in command line enter the above but it's not working. I've tried being inside mysql and just at command line and nothing seems to be working.
Is there something I should be doing differently in windows or MySQL5? I don't know how the heck I got it to work the first time...
TIA
Try this instead:
mysql -u root -p
(prompts for password)
use db;
source db.sql
I found out it is different to run this command from Windows Command Line (cmd.exe) and Windows PowerShell.
Using CMD.exe the command works okay, but in PowerShell I get this error:
mysql -uroot exampledb < exampledb.sql
The '<' operator is reserved for future use.
Not sure if your example was a typo or not, but for starters you need to have a space in between your flags and their values, roughly like this:
mysql -u root -p [password] db < db.sql
If you are already logged in the try this it will be very useful, but depend upon the MySQL version, it works on MySQL 5.0
For log in if you are not already logged in.
mysql>[your password]
Other wise, use the database to which you want to import the SQLDump file by command.
mysql>use [your database name]
And then give source the database Dump file path as blow command(If not works the copy Dump database file to the bin folder where the MySQL installed for eg. "C:/programfiles/mysql/mqlserver5.0/bin")
mysql> source [dataBasePath+name.sql or dataBaseName.sql]
I've been using PHP script called "BigDump":
http://www.ozerov.de/bigdump.php
This perfectly works
mysql>[your password]
Other wise, use the database to which you want to import the SQLDump file by command.
mysql>use [your database name]
And then give source the database Dump file path as blow command(If not works the copy Dump database file to the bin folder where the MySQL installed for eg. "C:/programfiles/mysql/mqlserver5.0/bin")
mysql> source [dataBasePath+name.sql or dataBaseName.sql]EG: source C:.....sql
I am using mysql server 5.5
In Windows PowerShell, you can pipe in the contents like so:
Get-Content db.sql | mysql -u root -p [password]