I need to know to how to export all the tables in the database as .sql files using mysqldump. i need to set them into separate files per table for easy restore process.
Please note that there is over 100+ tables in the database that following option is NOT GOING to help me.
mysqldump -p --user=username dbname tableName > tableName.sql
Note:
Also prefer a Single Command rather than set of commands in shell scripts with forloops.
Mysqldump does not have an option to dump a separate .sql file per table.
You can use mysqldump --tab to dump a separate CSV file per table (but keep in mind this dumps the files on the database server, not on the host where you run the command, so it won't work for example if you use RDS).
You can also use mydumper which is an open-source tool. This dumps two .sql files for each table: one with the CREATE TABLE definition, and a second file that contains the data.
Related
I am new to databases and MySQL and am still in the process of learning it. I have been tasked to see if it is possible to store the MySQL Data Directory in a Network Drive... The purpose is to have a backup of the directory and allowing multiple users to point to that particular directory.
I have been able to successfully move the data directory to a different location on my PC but have been unsuccessful when I tried moving the data directory into a Network Drive.
Is it possible to move the data directory into a shared Network Drive, and if so, what steps should I take?
Notes:
Windows 10
Attempted moving the directory and editing the my.ini
file
Perhaps your approach is not optimal or I'm misunderstanding the question (or whoever gave you the task isn't clear on the best ways to back up MySQL databases). If I were you, I'd put it to whoever asked you to do this task that making plain-text SQL (*.sql) dumps of the databases and putting those into the backup directory would be easier/simpler than making a backup of the data directory itself, which contains binary file representations of the databases.
From the MySQLdump manual page:
To dump all databases:
$ mysqldump --all-databases > dump.sql
To dump only specific databases, name them on the command line and use the --databases option:
$ mysqldump --databases db1 db2 db3 > dump.sql
The --databases option causes all names on the command line to be treated as database names. Without this option, mysqldump treats the first name as a database name and those following as table names.
With --all-databases or --databases, mysqldump writes CREATE DATABASE and USE statements prior to the dump output for each database. This ensures that when the dump file is reloaded, it creates each database if it does not exist and makes it the default database so database contents are loaded into the same database from which they came. If you want to cause the dump file to force a drop of each database before recreating it, use the --add-drop-database option as well. In this case, mysqldump writes a DROP DATABASE statement preceding each CREATE DATABASE statement.
To dump a single database, name it on the command line:
$ mysqldump --databases test > dump.sql
Exercise for the reader: Write a script (crontab) or set up a scheduled task to dump the databases and move the output to the network drive.
If that's not what is required, but access to the database by multiple people is, create user accounts using the MySQL Server RDBMS instead. (You might need to configure the server to allow remote access. In that case, remove any test or anonymous/blank password accounts and change the root password to something more secure than root, admin or password1.)
I have dumped a MySQL database (all tables) for use with PHPUnit using this command:
mysqldump --xml -t -u [username] --password=[password] [database] > /path/to/file.xml
I need to modify the test data, so I need to re-import it back into the database so I can work on it with MySQL workbench.
What is the command to reimport it back into a specified database?
And no, it's not as simple as LOAD XML, that works on a table by table basis, but this export has all tables in it.
NO, there is no built in way present to restore a XML formatted dump file. You can check this blog Restoring XML-formatted MySQL dumps for information on this as well as this existing post How to restore a mysql xml database file from mysql command line?
I'm trying to dump all of my mysql data for one database into a single file. That said, I don't want to include the table structures in this file (there is another file which will have the structure of the tables in it).
Is it possible to strictly extract the data and not the table structures?
What I am doing right now ...
# Extracts the database structure
mysqldump -d -hlocalhost -uusername -ppassword database -r database.sql
# Extracts each table and their data individually
mysqldump -d -hlocalhost -uusername -ppassword database --tab .
The first command will spit out a .sql file with the structure of all entities in the database whereas the second one automatically makes the .sql and .txt files with the structure and entities split out.
What I need is one copy of the entire database which is done the same way.
Thanks
Use the --no-create-info option, or its shorthand -t:
--no-create-info, -t
Do not write CREATE TABLE statements that re-create each dumped table.
Note
This option does not not exclude statements creating log file groups or tablespaces from mysqldump output; however, you can use the --no-tablespaces option for this purpose.
I want to import an sqldump file in my database "dbname" into the table "data" without using the network interface.
when i import the file via
mysql dbname -u databaseuser -pdatabasepass<data.sql
this is really slow on my ubuntu 12.04
but when i use this instead:
mysqlimport -u databaseuser -pdatabasepass --local data.sql
it is as fast as normal.
i think, because on my mashine it is parsing each line separately when using "mysql" and "<"
is there a way to use the mysqlimport syntax for importing an sql-file with CREATE and DROP TABLE stuff?
maybe just split the upper part in the sql-dump (with the drop table and create table statement) automatically and then only send the rest of the file with the INPUT-statements to mysqlinsert, that should work
There is no way to do that directly. What you need to do is run mysqldump --tab instead of the normal mysqldump, that will create both a .sql files containing table definitions and a CSV file containing data that can be imported with mysqlimport.
You can specify the path to a UNIX socket file with --socket=/path/to/... (or -S), which may be faster. You can find the socket path in the server's my.cnf. So, for example (and I'm just making up the path, here);
mysql dbname -u databaseuser -pdatabasepass -S /var/run/mysqld/mysqld.sock < data.sql
You can use this script: How do I split the output from mysqldump into smaller files?
and modify it, so you have only the create-statements in one file and the data statements in another.
I have a mysql dump created with mysqldump that holds all the tables in my database and all their data. However I only want to restore two tables. (lets call them kittens and kittens_votes)
How would I restore those two tables without restoring the entire database?
Well, you have three main options.
You can manually find the SQL statements in the file relating to the backed up tables and copy them manually. This has the advantage of being simple, but for large backups it's impractical.
Restore the database to a temporary database. Basically, create a new db, restore it to that db, and then copy the data from there to the old one. This will work well only if you're doing single database backups (If there's no CREATE DATABASE command(s) in the backup file).
Restore the database to a new database server, and copy from there. This works well if you take full server backups as opposed to single database backups.
Which one you choose will depend upon the exact situation (including how much data you have)...
You can parse out CREATE TABLE kittens|kitten_votes AND INSERT INTO ... using regexp, for example, and only execute these statements. As far as I know, there's no other way to "partially restore" from dump.
Open the .sql file and copy the insert statements for the tables you want.
create a new user with access to only those 2 tables. Now restore the DB with -f (force) option that will ignore the failed statements and execute only those statements it has permission to.
What you want is a "Single Table Restore"
http://hashmysql.org/wiki/Single_table_restore
A few options are outlined above ... However the one which worked for me was:
Create a new DB
$ mysql -u root -p CREATE DATABASE temp_db
Insert the .sql file ( the one with the desired table ) into the new DB
$ mysql -u root -p temp_db < ~/full/path/to/your_database_file.sql
dump the desired table
$ mysqldump -u root -p temp_db awesome_single_table > ~/awesome_single_table.sql
import desired table
$ mysql -u root -p original_database < ~/awesome_single_table.sql
Then delete the temp_db and you're all golden!