I created a database using Mysql Workbench. Now I want to export this database to my home PC.
How can I do this if the 2 PCs have no network connection?
I use mysqldump to export the database. You can use something like
mysqldump -u [username] -p [database name] > backup.sql
to store it in a file. After that you can import into another database via
mysql -u [username] -p [database name] < backup.sql
As edit was rejected posting it as an answer; hope it will be helpful.
Followed to the queries give by "Marc Hauptmann" -
Few quick-tips for generic issues that can be faced while performing DB dump and restore:-
As correctly mentioned above by "Marc" it is always advised not to provide db password in command line export [if you do so, it can be easily sniffed in history or reverse-search]
If you are transferring large dump file it is advised to compress it before transferring. [it should be uncompressed before restore]
While exporting if you want to export data with 'new database name' it can also be done. [It will require new Db to be created before using it in import]
Also if we are exporting data from production servers to make sure it doesn't impact performance, export from other servers with below additional option "-h [hostname]"
mysqldump -h [hostname] -u [username] -p [database name] > backup.sql
Using gzip is pretty painless and really shrinks these files.
mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]
gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]
But man, it is 2014 - this stuff is also easy to do via a secure shell connection.
Related
Does anybody know how to export database?
I'm using TeraTerm and i want to export the database for backup purpose. I tried searching on google about the command but i can't find it.
EDIT:
Thank you for your time and effort in answering my question.The database was already exported. I use this :
mysqldump -u root -ptest test_db > backup.sql
You can use the mysqldump command this way:
mysqldump -u root -ptest test_db > backup.sql
Where username is root, root password is test and database name is test_db
If you are using mysql database, then there are numerous ways you can do that.
One is that you can set up 'phpmyadmin' on your server and export the database using the phpmyadmin web interface.
Or you can use this command:
"mysqldump -u [user] -p[password] [database-name]" > [database-export-file-name.sql]
Note that there is no space between -p and password.
See this for reference: http://www.itworld.com/article/2833078/it-management/3-ways-to-import-and-export-a-mysql-database.html
I'm facing the following:
We have a DB table of 11GB with over 257 million records and need a backup. Exporting via PHPmyAdmin isn't possible (chrome keeps crashing) and backing up with SSH mysqldump tablename will give a insufficient space disk error (error 28).
Now I'd like to know if there is a way to export a mysqldump with a row 0 till ~100.000.000 command so we can make 3 parts (or smaller parts if required).
What I'm using:
mysqldump -p -u username database_name database_table > dbname.sql
[EDIT]
Found out how to get a row of <50.0000.0000 to SQL with the following:
mysqldump -p -u db_name db_table --where='id<50000000'
But the big question remains now, how to go further? Now I want to get all records between 50.000.000 and 100.000.000 ..
Anybody knows the answer if it's possible and what command I should use?
Problem solved:
Part 1 (<50.000.000):
mysqldump -p -u db_name db_table --where='id<50000000' >part_1.sql
Part 2 (>50.000.000 till <100.0000.000):
mysqldump -p -u db_name db_table --where='id>=50000000 &&
id<100000000' >part_2.sql
Part last (>250.000.000)
mysqldump -p -u db_name db_table --where='id>250000000' >part_final.sql
And so on..
mysqldump creates a text file that contains sql statements, if want to take mysql backup in parts then you will have to run mysqldump like this
mysqldump --where "id%2=0" database_name table > table_even.sql
mysqldump --where "id%2=1" database_name table > table_odd.sql
OR
you need to write some program, script to achieve that
I found a nice solution for heavy transfers! This might also help you to avoid to transfer your database in parts (as in this example) - since it does this super fast:
Exporting a full database or in parts as mentioned using mysqldump:
mysqldump -p -u db_name db_table --where='id<50000000' >part_1.sql
To import to the new database - login via terminal to the new database:
mysql -h localhost -upotato -p123456
Enter the database:
USE databasename;
Use the source command:
source /path/to/file.sql;
This works X1000 faster than the standard:
mysql -h localhost_new -upotato -p1234567 table_name < /path/to/file.sql
Since you enter the database.
How can I backup a mysql database which is running on a remote server, I need to store the back up file in the local pc.
Try it with Mysqldump
#mysqldump --host=the.remotedatabase.com -u yourusername -p yourdatabasename > /User/backups/adump.sql
Have you got access to SSH?
You can use this command in shell to backup an entire database:
mysqldump -u [username] -p[password] [databasename] > [filename.sql]
This is actually one command followed by the > operator, which says, "take the output of the previous command and store it in this file."
Note: The lack of a space between -p and the mysql password is not a typo. However, if you leave the -p flag present, but the actual password blank then you will be prompted for your password. Sometimes this is recommended to keep passwords out of your bash history.
No one mentions anything about the --single-transaction option. People should use it by default for InnoDB tables to ensure data consistency. In this case:
mysqldump --single-transaction -h [remoteserver.com] -u [username] -p [password] [yourdatabase] > [dump_file.sql]
This makes sure the dump is run in a single transaction that's isolated from the others, preventing backup of a partial transaction.
For instance, consider you have a game server where people can purchase gears with their account credits. There are essentially 2 operations against the database:
Deduct the amount from their credits
Add the gear to their arsenal
Now if the dump happens in between these operations, the next time you restore the backup would result in the user losing the purchased item, because the second operation isn't dumped in the SQL dump file.
While it's just an option, there are basically not much of a reason why you don't use this option with mysqldump.
This topic shows up on the first page of my google result, so here's a little useful tip for new comers.
You could also dump the sql and gzip it in one line:
mysqldump -u [username] -p[password] [database_name] | gzip > [filename.sql.gz]
mysqldump -h [domain name/ip] -u [username] -p[password] [databasename] > [filename.sql]
Tried all the combinations here, but this worked for me:
mysqldump -u root -p --default-character-set=utf8mb4 [DATABASE TO BE COPIED NAME] > [NEW DATABASE NAME]
If you haven't install mysql_client yet and using Docker container instead:
sudo docker exec MySQL_CONTAINER_NAME /usr/bin/mysqldump --host=192.168.1.1 -u username --password=password db_name > dump.sql
You can directly pipe it to the remote server where you wish to copy your data to:
mysqldump -u your_db_user_name -p --set-gtid-purged=OFF --triggers --routines --events --compress --skip-lock-tables --verbose your_local_sql_db_name | mysql -u your_db_user_name -p -h your_remote_server_ip your_remote_server_db_name
You need to have created the db on your remote sql server.
Using the above command, I was able to copy from my local sql server version 8.0.23 to my remote sqlserver running 8.0.25
This is how you would restore a backup after you successfully backup your .sql file
mysql -u [username] [databasename]
And choose your sql file with this command:
source MY-BACKED-UP-DATABASE-FILE.sql
I may have gone completely braindead today, but I'm having trouble copying my DB down to my local MAMP server. I'm not too familiar with mysqldump, etc, but I want to know how to copy a database from a test server to my MAMP local server in the easiest way possible. I have very limited experience with server stuff, but have a bit of experience with command line.
Any straight-forward help would be much appreciated. I look forward to smacking myself in the head when I realise what a dick I've been ;)
Dalogi
mysqldump's the best way:
on the test server: mysqldump -p name_of_db > dump.sql
on the map server: mysql -p < dump.sql
The dump file contains the full instructions in SQL query format to recreating the db, it stable structure, and data. The -p option forces both apps to prompt for your password. If your MySQL username is different than your system's login account, then you'll need the -u option as well:
mysqldump -p -u yourDBusername name_of_db > dump.sql
mysql -p -u yourDBusername < dump.sql
mysqldump -h 'remotehost' -uremoteuser -premotepass db_name | mysql -ulocaluser -plocalpass db_name
The database "db" is backuped in backup.sql. Is there a way to restore database from script with different from "db" name?
thank you in advance!
Sure, when you import it you do this right:
mysql -uuser -ppassword databasename < mydump.sql
You can put anything you want where I wrote databasename - as long as that database actually exists :)
This depends on how you created your MySQL dB dump file
for example, if you do
mysqldump -h localhost -u user mydb -pXXX > mydb.sql
There won't be any CREATE DATABASE statements in your sql dump file. But I think you can only backup one database.
If you create your mysql dump file with --database or --all-databases option
for example
mysqldump -h localhost -u user --database mydb -pXXX > mydb.sql
mysqldump -h localhost -u user --all-databases -pXXX > alldb.sql
then you will see CREATE DATABASE statement in your mysql dump file. If you want a different dB name, you will need to change it before DB restore.
If the name of the database is include the SQL file, I didn't find any other way than modify the SQL file.
My favorite command to do it :
sed -i "s/\`old_db_name\`/\`new_db_name\`/g" my_sql_file.sql
Open up the .sql file and change the database name inside.
You can use a text editor, like Notepad or gedit.