MySql export schema without data - mysql

I'm using a MySql database with a Java program, now I want to give the program to somebody else.
How to export the MySql database structure without the data in it, just the structure?

You can do with the --no-data option with mysqldump command
mysqldump -h yourhostnameorIP -u root -p --no-data dbname > schema.sql

Yes, you can use mysqldump with the --no-data option:
mysqldump -u user -h localhost --no-data -p database > database.sql

you can also extract an individual table with the --no-data option
mysqldump -u user -h localhost --no-data -p database tablename > table.sql

You can use the -d option with mysqldump command
mysqldump -u root -p -d databasename > database.sql

Dumping without using output.
mysqldump --no-data <database name> --result-file=schema.sql

Beware though that --no-data option will not include the view definition. So if yo had a view like following
create view v1
select `a`.`id` AS `id`,
`a`.`created_date` AS `created_date`
from t1;
with --no-data option, view definition will get changed to following
create view v1
select 1 AS `id`, 1 AS `created_date`

In case you are using IntelliJ you can enable the Database view (View -> Tools Window -> Database)
Inside that view connect to your database. Then you can rightclick the database and select "Copy DDL". Other IDEs may offer a similar function.

You Can Use MYSQL Administrator Tool its free
http://dev.mysql.com/downloads/gui-tools/5.0.html
you'll find many options to export ur MYSQL DataBase

If you want to dump all tables from all databases and with no data (only database and table structures) you may use:
mysqldump -P port -h hostname_or_ip -u username -p --no-data --all-databases > db_backup.sql
This will produce a .sql file that you can load onto a mysql server to create a fresh database.
Use cases for this are not many in a production environment, but I do this on a weekly basis to reset servers which are linked to demo websites, so whatever the users do during the week, on sunday nights everything rolls back to "new" :)

Add the --routines and --events options to also include stored routine and event definitions
mysqldump -u <user> -p --no-data --routines --events test > dump-defs.sql

shell> mysqldump --no-data --routines --events test > dump-defs.sql

Using mysql-backup4j its quite easy to backup any database with few lines of code.this will generate the sql dump for database that can later be use to restore database easily.
maven dependency required for this is :
<dependency>
<groupId>com.smattme</groupId>
<artifactId>mysql-backup4j</artifactId>
<version>1.0.1</version>
</dependency>
And here goes the implementation in java..you can play with the cool parameter sets
/**
* Creator : Tanvir Chowdhury
* Date : 2021-11-15
*/
public class BackupDatabase {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty(MysqlExportService.DB_NAME, "hcs_test_db");
properties.setProperty(MysqlExportService.DB_USERNAME, "root");
properties.setProperty(MysqlExportService.DB_PASSWORD, "root");
properties.setProperty(MysqlExportService.DELETE_EXISTING_DATA, "true");
properties.setProperty(MysqlExportService.DROP_TABLES, "true");
properties.setProperty(MysqlExportService.ADD_IF_NOT_EXISTS, "true");
properties.setProperty(MysqlExportService.JDBC_DRIVER_NAME, "com.mysql.cj.jdbc.Driver");
properties.setProperty(MysqlExportService.JDBC_CONNECTION_STRING, "jdbc:mysql://localhost:3306/hcs_test_db");
properties.setProperty(MysqlExportService.TEMP_DIR, new File("/Users/tanvir/Desktop/backups/backup.sql").toString());
properties.setProperty(MysqlExportService.PRESERVE_GENERATED_ZIP, "true");
MysqlExportService mysqlExportService = new MysqlExportService(properties);
mysqlExportService.export();
}
}
if required you can also use the mail sending options quite easily to send the backup file to any email address.
Or You can do with the --no-data option with mysqldump command.If you want to do this from java then pass this command to runtime exec() method as param.
mysqldump -u root -h localhost --no-data -proot hcs_db_one?useSSL=false > db_one_dump.sql

To get an individual table's creation script:
- select all the table (with shift key)
- just right click on the table name and click Copy to Clipboard > Create Statement.

You can take using the following method
mysqldump -d <database name> > <filename.sql> // -d : without data
Hope it will helps you

From phpmyadmin you can do the following:
Export
Export method: Custom - display all possible options
Format-specific options: structure NOT structure and data
To export the entire database:

Related

how to get the query of my exist database [duplicate]

I'm using a MySql database with a Java program, now I want to give the program to somebody else.
How to export the MySql database structure without the data in it, just the structure?
You can do with the --no-data option with mysqldump command
mysqldump -h yourhostnameorIP -u root -p --no-data dbname > schema.sql
Yes, you can use mysqldump with the --no-data option:
mysqldump -u user -h localhost --no-data -p database > database.sql
you can also extract an individual table with the --no-data option
mysqldump -u user -h localhost --no-data -p database tablename > table.sql
You can use the -d option with mysqldump command
mysqldump -u root -p -d databasename > database.sql
Dumping without using output.
mysqldump --no-data <database name> --result-file=schema.sql
Beware though that --no-data option will not include the view definition. So if yo had a view like following
create view v1
select `a`.`id` AS `id`,
`a`.`created_date` AS `created_date`
from t1;
with --no-data option, view definition will get changed to following
create view v1
select 1 AS `id`, 1 AS `created_date`
In case you are using IntelliJ you can enable the Database view (View -> Tools Window -> Database)
Inside that view connect to your database. Then you can rightclick the database and select "Copy DDL". Other IDEs may offer a similar function.
You Can Use MYSQL Administrator Tool its free
http://dev.mysql.com/downloads/gui-tools/5.0.html
you'll find many options to export ur MYSQL DataBase
If you want to dump all tables from all databases and with no data (only database and table structures) you may use:
mysqldump -P port -h hostname_or_ip -u username -p --no-data --all-databases > db_backup.sql
This will produce a .sql file that you can load onto a mysql server to create a fresh database.
Use cases for this are not many in a production environment, but I do this on a weekly basis to reset servers which are linked to demo websites, so whatever the users do during the week, on sunday nights everything rolls back to "new" :)
Add the --routines and --events options to also include stored routine and event definitions
mysqldump -u <user> -p --no-data --routines --events test > dump-defs.sql
shell> mysqldump --no-data --routines --events test > dump-defs.sql
Using mysql-backup4j its quite easy to backup any database with few lines of code.this will generate the sql dump for database that can later be use to restore database easily.
maven dependency required for this is :
<dependency>
<groupId>com.smattme</groupId>
<artifactId>mysql-backup4j</artifactId>
<version>1.0.1</version>
</dependency>
And here goes the implementation in java..you can play with the cool parameter sets
/**
* Creator : Tanvir Chowdhury
* Date : 2021-11-15
*/
public class BackupDatabase {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty(MysqlExportService.DB_NAME, "hcs_test_db");
properties.setProperty(MysqlExportService.DB_USERNAME, "root");
properties.setProperty(MysqlExportService.DB_PASSWORD, "root");
properties.setProperty(MysqlExportService.DELETE_EXISTING_DATA, "true");
properties.setProperty(MysqlExportService.DROP_TABLES, "true");
properties.setProperty(MysqlExportService.ADD_IF_NOT_EXISTS, "true");
properties.setProperty(MysqlExportService.JDBC_DRIVER_NAME, "com.mysql.cj.jdbc.Driver");
properties.setProperty(MysqlExportService.JDBC_CONNECTION_STRING, "jdbc:mysql://localhost:3306/hcs_test_db");
properties.setProperty(MysqlExportService.TEMP_DIR, new File("/Users/tanvir/Desktop/backups/backup.sql").toString());
properties.setProperty(MysqlExportService.PRESERVE_GENERATED_ZIP, "true");
MysqlExportService mysqlExportService = new MysqlExportService(properties);
mysqlExportService.export();
}
}
if required you can also use the mail sending options quite easily to send the backup file to any email address.
Or You can do with the --no-data option with mysqldump command.If you want to do this from java then pass this command to runtime exec() method as param.
mysqldump -u root -h localhost --no-data -proot hcs_db_one?useSSL=false > db_one_dump.sql
To get an individual table's creation script:
- select all the table (with shift key)
- just right click on the table name and click Copy to Clipboard > Create Statement.
You can take using the following method
mysqldump -d <database name> > <filename.sql> // -d : without data
Hope it will helps you
From phpmyadmin you can do the following:
Export
Export method: Custom - display all possible options
Format-specific options: structure NOT structure and data
To export the entire database:

ssh Mysql dump table parts (11GB DB to smaller pieces)

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 to export mysql database to another computer?

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.

generating database with different name from mysqldump backup

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.

Dump only the data with mysqldump without any table information?

I am looking for the syntax for dumping all data in my mysql database. I don't want any table information.
mysqldump --no-create-info ...
Also you may use:
--skip-triggers: if you are using triggers
--no-create-db: if you are using --databases ... option
--compact: if you want to get rid of extra comments
This should work:
# To export to file (data only)
mysqldump -u [user] -p[pass] --no-create-info mydb > mydb.sql
# To export to file (structure only)
mysqldump -u [user] -p[pass] --no-data mydb > mydb.sql
# To import to database
mysql -u [user] -p[pass] mydb < mydb.sql
NOTE: there's no space between -p & [pass]
If you just want the INSERT queries, use the following:
mysqldump --skip-triggers --compact --no-create-info
>> man -k mysqldump [enter in the terminal]
you will find the below explanation
--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.
--no-data, -d
Do not write any table row information (that is, do not dump table
contents). This is useful if you want to dump only the CREATE TABLE
statement for the table (for example, to create an empty copy of the
table by loading the dump file).
# To export to file (data only)
mysqldump -t -u [user] -p[pass] -t mydb > mydb_data.sql
# To export to file (structure only)
mysqldump -d -u [user] -p[pass] -d mydb > mydb_structure.sql
Best to dump to a compressed file
mysqldump --no-create-info -u username -hhostname -p dbname | gzip > /backupsql.gz
and to restore using pv apt-get install pv to monitor progress
pv backupsql.gz | gunzip | mysql -uusername -hhostip -p dbname
Would suggest using the following snippet. Works fine even with huge tables (otherwise you'd open dump in editor and strip unneeded stuff, right? ;)
mysqldump --no-create-info --skip-triggers --extended-insert --lock-tables --quick DB TABLE > dump.sql
At least mysql 5.x required, but who runs old stuff nowadays.. :)
Just dump the data in delimited-text format.
Try to dump to a delimited file.
mysqldump -u [username] -p -t -T/path/to/directory [database] --fields-enclosed-by=\" --fields-terminated-by=,
When attempting to export data using the accepted answer I got an error:
ERROR 1235 (42000) at line 3367: This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
As mentioned above:
mysqldump --no-create-info
Will export the data but it will also export the create trigger statements. If like me your outputting database structure (which also includes triggers) with one command and then using the above command to get the data you should also use '--skip-triggers'.
So if you want JUST the data:
mysqldump --no-create-info --skip-triggers