How take mysqldump with UTF8? - mysql

I am trying to take mysql dump with command:
mysqldump -u xxxx -p dbxxx > xxxx270613.sql
what is command to take mysqldump with UTF8 ?

Hi please try the following.
mysqldump -u [username] –p[password] --default-character-set=utf8 -N --routines --skip-triggers --databases [database_name] > [dump_file.sql]

I had the problem, that even with applied utf-8 flags when creating the dump I could not avoid broken characters importing a dump that was created from a DB with many text columns using latin1.
Some googling and especially this site helped me to finally figure it out.
mysqldump with --skip-set-charset --default-character-set=latin1 flags,
to avoid MySQL attempt of reconversion and setting a charset.
fix the dump by replacing the charset strings using sed on terminal
sed -i 's/latin1_swedish_ci/utf8mb4/g' mysqlfile.sql
sed -i 's/latin1/utf8mb4/g' mysqlfile.sql
to make sure you don't miss anything you can do grep -i 'latin1' mysqlfile.sql before step 2 - and then come up with more sed orders. Introduction to sed here
create a clean DB
CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
apply fixed dump

--default-character-set=utf8 is the option you are looking for the one can be used together with these others:
mysqldump --events \
--routines \
--triggers \
--add-drop-database \
--compress \
--hex-blob \
--opt \
--skip-comments \
--single-transaction \
--skip-set-charset \
--default-character-set=utf8 \
--databases dbname > my.dump
Also, check the --hex-blob it helps to dump binary strings in hexadecimal format, so I can guaranty (be more portable) making the import to work.
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.
This helps to restore using:
# mysql < dump.sql
Instead of:
# mysql dbname < dump.sql

Related

How to export a MySQL database to CSV

What is the best way to export a MySQL database to a CSV file without including indexes, table structures etc?
I just need to get all the data, I have a lot tables so I don't want to do it one by one.
I'm using 0xdbe and Workbench running on Linux.
Thanks!
mysqldump has a mode to dump tab-separated files, one per table.
mysqldump -u <username> -p<password> -T <output_directory> --no-create-info <database_name>
With a bit of tweaking this can be make to look like a CSV file.
mysqldump -u <username> -p<password> -T <output_directory> --fields-terminated-by ',' --fields-enclosed-by '"' --fields-escaped-by '\' --no-create-info <database_name>
Note that the file is written by the database, so whatever user your database is running as needs to have write access to the output directory!
This worked well for me:
mysqldump DBNAME TABLENAME --fields-terminated-by ',' \
--fields-enclosed-by '"' --fields-escaped-by '\' \
--no-create-info --tab /var/lib/mysql-files/
I'm dumping to /var/lib/mysql-files/ to avoid this error:
mysqldump: Got error: 1290: The MySQL server is running with the --secure-file-priv option so it cannot execute this statement when executing 'SELECT INTO OUTFILE'
mysqldump might be useful in this case. Even though it's not the csv output, it has all the indices and table structures including the data.
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

mysqldump wont dump my data

here is the command I'm using:
mysqldump.exe -u root -d capstone -verbse --skip-quote-names > capstone.sql
and the output I get
mysqldump: Warning: Can't set SQL_QUOTE_SHOW_CREATE option ()
-- Skipping dump data for table 'users', --no-data was used
any ideas? if I dump to XML it works but the place I'm importing it to doesn't handle XML and my data ruins the CSV output somehow too.
the -d option is alias of --no-data, see https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#option_mysqldump_no-data
perhaps you intended to state "use database capstone" but in that case it wouldn't be -d capstone, the database name doesn't need any switch/option, just put it in there
shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases
https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html#mysqldump-syntax
I think you mean to use either -B / --databases (which includes allows you to indicate multiple databases to dump instead of a database and tables) or no such argument at all. I think you also mistyped --verbose.
Note that if you include --databases a CREATE DATABASE statement is also included. This could be important depending up on how you intend to use the data.

mysql restore to different database

I back up my production database with the following command:
mysqldump -u root --opt --skip-extended-insert --databases my_production_db
The resulting dump file has the following lines near the top:
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `my_production_db` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `my_production_db `;
In order to restore the database to a different destination ie. my_debvelopment_db I have to open the dump file and edit the bits where the database is named.
Then I run:
mysql -u root -p <password> < mydumpfile
I have not figured out another way to do it.
As the database gets bigger this becomes impractical.
Am I missing something? Cant I somehow specify where I want to restore the database? Would I need a different backup command?
#minaz answer was good, but I want to append a little bit more.
The problem was caused by --databases keyword. If you omit the keyword, it will not contain any database creation contents.
So, Dump without --databases keyword.
mysqldump -u username -p database_name > dump.sql
And restore it with the target database name.
mysql -u username -p target_database_name < dump.sql
Also, there are several ways to do this. See the similar problem on here (dba.stackexchange).
If you drop the option --databases but still specify the database name, you will NOT get the create database statements. ie:
mysqldump -u root --opt --skip-extended-insert my_production_db
On your dev machine simply create any database you wish to restore to.
If you already have your dump you can strip the commands for creating and using the database. Simply remove the fourth and the new fifth line.
sed '4d' dump.sql | sed '5d' > dump-striped.sql
On windows xampp I used following commands to achieve this
Export
mysqldump -u root -p mydb > mydb.sql
Import
mysql -u root -p mynewdb < mydb.sql

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

How to backup stored procedures in MySQL

I use mysqldump with MySQL 5.0 and I back it up every day, but do not understand the method that only stored procedure backs up.
How can I back it up?
I'm not sure whether you're asking to back up stored procedures as well as everything else, or just the stored procedures on their own...
Stored procedured in dump with everything else:
mysqldump -R <dbname> #or
mysqldump --routines <dbname>
Just the stored procedures:
mysqldump -n -t -d -R <dbname> #or
mysqldump --no-create-db --no-create-info --no-data --routines <dbname>
Does that help?
You can also put routines=true in the [mysqldump] section of your my.cnf file (you may have to add this section as it is not usually present in a virgin my.cnf file) to include routines in a normal dump.
mysqldump - u dbusername (ex: -uroot) -ppassword (ex:-pmysql#dbpas) --routines <dbname>
use the username and password could be more helpful.