I am running mysqldump as follows:
mysqldump -u root --password=secret -d dbname > output.sql
When I review the output, I only have the schema. However, if I connect using the same exact settings and query, there is plenty of data:
mysql -u root --password=secret -D dbname
mysql> select count(*) from account;
+----------+
| count(*) |
+----------+
| 230 |
+----------+
Based on the official docs, it seems I am using mysqldump correctly. I'm on OS X and this is my mysql info:
» mysqldump --version
mysqldump Ver 8.0.19 for osx10.15 on x86_64 (Homebrew)
The -d option means not to write the table contents. The documentation says:
--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).
Remove the -d option and you should get all the table contents.
This should work:
mysqldump --user=<username> --password=<password> --result-file=<path_to_backup_file> --databases <database_name>
Related
I want to dump data from a specific table on a specific time period.
So I added a WHERE clause to the dump command. Now I also want to have the table structure so that when I import the SQL it wouldn't return an error. I used the following command:
mysqldump -t -u root -p --host IP DBNAME TABLENAME --where="LastUpdate > '2018-01-09 00:00:00'" > result.sql
However the resulting SQL dump does not include the table structure. How to include the table structure when dumping from MySQL using mysqldump?
Try:
mysqldump -u root -p --host IP DBNAME TABLENAME --where="LastUpdate > '2018-01-09 00:00:00'" > result.sql
Remove -t flag.
4.5.4 mysqldump — A Database Backup Program :: --no-create-info, -t
Do not write CREATE TABLE statements that create each dumped table.
...
I have 2 databases, both called dataweb (these are databases of 2 different sites) I need to put the new tables I have made on site 1, into the database of site 2, without copying the data from site 1, or deleting the data from site 2, any ideas?
MySQL client version: 5.0.51a
It can be done with bash script. E.g. using the next scenario
mysql -hdb1host.com -uroot -pxxxxxx -e "use db1; show tables" >
file1.txt
mysql -hdb2host.com -uroot -pxxxxxx -e "use db2; show tables" >
file2.txt
then compare these two files, e.g. with diff command
http://www.computerhope.com/unix/udiff.htm
take diff output and stream it to files (tables_from_diff1.txt,
tables_from_diff2.txt).
Then make dump for these tables
mysqldump -hdb1host.com -uroot
-pxxxxxx db1 [tables from tables_from_diff1.txt] > db1_dump.txt
mysqldump -hdb2host.com -uroot -pxxxxxx db2 [tables from
tables_from_diff2.txt] > db2_dump.txt
Apply datadumps to necessary dbs.
mysql -hdb1host.com -uroot -pxxxxxx db1 < db2_dump.txt
mysql -hdb1host.com -uroot -pxxxxxx db2 < db1_dump.txt
I have database called Database1 and this DB have 40 tables. Now i want to delete all data from that 40 tables.I know that to display all tables from DB using
SELECT table_name
FROM INFORMATION_SCHEMA.tables
WHERE table_schema = 'Database1';
So how to delete all data from all tables form Database1 using single query?
Note :
I should delete only data, not tables.
I am using mysql workbench 6.0
You can try this:
mysqldump -d -uuser -ppass --add-drop-table yourdatabasename > yourdatabasename.sql
mysql -uuser -ppass yourdatabasename < yourdatabasename.sql
As pointed correctly by Zafar, if you want to include stored procedure/function also then you can include -R option.
Or you can try like
mysql -Nse 'show tables' yourdatabasename | while read table; do mysql -e "truncate table $yourtable" yourdatabasename; done
You can execute below command on server console.
mysql -uroot -p<pass> -Nse 'show tables' database1 | while read table; do mysql -uroot -p<pass> database1 -e "truncate table $table"; done
You can also do it by any gui like sqlyog by below steps-
right click on database1 > choose more database options > truncate database
Third option is by structure backup and restore as per below-
mysqldump -R -d -uroot -proot123 database1 | mysql -uroot -proot123 database1
Note: Always use -R if you are using stored procedures/function other wise you loose it.
enjoy...
For Oracle :
if you want to delete all the records of all the tables without drop the tables you should have a look, it may help you
https://dba.stackexchange.com/questions/74519/delete-all-the-data-from-all-tables
you can try any of following :
Delete data from all tables in MYSQL
How to empty all rows from all tables in mysql (in sql)
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
I'm attempting to assemble all the options that I need for mysqldump to create everything used by my applications database into a single script. This includes the database itself and all the database users/passwords/privileges.
I've got it all figured out with the exception of the user piece... here's what I'm currently using:
mysqldump -h host -u root -p \
--add-drop-database --routines -B database_name > backup.sql
So, what am I missing?
The database users/passwords/privileges are kept in the mysql database, and won't get dumped with your dump command. You'll have to add that database as well to the list of DBs to dump:
mysqldump ... --routines --databases database_name mysql > backup.sql
or just dump everything:
mysqldump ... --routines --all-databases > backup.sql
When dumping the mysql database, don't forget:
--flush-privileges Emit a FLUSH PRIVILEGES statement after dumping the mysql
database. This option should be used any time the dump
contains the mysql database and any other database that
depends on the data in the mysql database for proper
restore.
So, I had a fundamental misunderstanding. Users are not specific to a database, but are rather created at the server level.
You can view all existing users with the following query:
SELECT * FROM mysql.user;
Knowing this, it's pretty obvious that mysqldump shouldn't do anything with users. However, if you need an answer to exporting/importing users and perms I suggest you check the following article - it helped me out.
http://pento.net/2009/03/12/backing-up-permissions-for-individual-databases/
My apologies for the noise on the board, but I figured I'd leave the post incase anyone else has the same misunderstanding.
Full process for me when migrating from one Mac OS X dev environment to a new one.
0) Get rid of newer version of MySQL on new MacBook
I had accidentally installed MySQL 8, so I had to remove it because it was newer than my old MacBook.
# Remove binaries
$ brew uninstall mysql
# Remove data/config that is leftover
$ rm -r /usr/local/var/mysql/
1) Install same version of MySQL on new MacBook
# Install version that matched old MacBook
$ brew install mysql#5.7
# Because it is an old version, you have to do a special configuration step
$ echo 'export PATH="/usr/local/opt/mysql#5.7/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile
# Start server
$ mysql.server start
2) Dump data on old MacBook
$ mysqldump -uroot --flush-privileges --routines --all-databases > complete_dump.sql
3) Restore data on new MacBook
$ mysql -p -uroot < complete_dump.sql
You may want to take a look at mysqlpump, it can dump all databases, and can export the users as CREATE USER and GRANT statements instead of relying on the mysql system db + mysql_upgrade.
mysqlpump # Dumps (almost) all databases (see [2])
mysqlpump --exclude-databases=% --users # Dumps all user accounts
It's unclear from the docs if you can do mysqlpump --users and get both all databases and the users.
Some system tables get omitted by default, see restrictions.
mysqldump version 10.19 contains the option --system=users wich adds CREATE USER with the GRANT commands into the dump.
mysqldump -h host -u root -p \
--add-drop-database --routines -B database_name \
--system=users > backup.sql
source: https://mariadb.com/kb/en/mysqldump/
Connect to your database server and execute:
select concat('show grants for ','\'',user,'\'#\'',host,'\'') from mysql.user;
You'll get something like this:
+--------------------------------------------------------+
| concat('show grants for ','\'',user,'\'#\'',host,'\'') |
+--------------------------------------------------------+
| show grants for 'mariadb.sys'#'localhost' |
| show grants for 'mysql'#'localhost' |
| show grants for 'root'#'localhost' |
+--------------------------------------------------------+
Capture the output to some temporary file.
And then cycle through each line in that temporary file, sending it against your mysql server, capturing the output.
Output will be something that you can use to reconstruct users on another server:
GRANT ALL PRIVILEGES ON *.* TO `root`#`localhost` IDENTIFIED VIA mysql_native_password USING 'invalid' OR unix_socket WITH GRANT OPTION
GRANT PROXY ON ''#'%' TO 'root'#'localhost' WITH GRANT OPTION
Here's a script I'm currently using:
mysql -u${BACKUP_DB_USER} -p${BACKUP_DB_PASSWORD} -e"select concat('show grants for ','\'',user,'\'#\'',host,'\'') from mysql.user" > user_list_with_header.txt
sed '1d' user_list_with_header.txt > ./user.txt
while read user; do mysql -u${BACKUP_DB_USER} -p${BACKUP_DB_PASSWORD} -e"$user" > user_grant.txt; echo "-- ${user}" >> user_privileges.txt; sed '1d' user_grant.txt >> user_privileges.txt; done < user.txt
echo "flush privileges" >> user_privileges.txt;
awk '{print $0";"}' user_privileges.txt > all_user_privileges_final.sql
rm user.txt user_list_with_header.txt user_grant.txt user_privileges.txt
You'll have all grant statements in the file all_user_privileges_final.sql.
Of course, you can limit your initial query to list only user(s) you want.
You can only use the option --system=users to get the user's dump from mysql/MariaDB if mysql client is for MariaDB.
For example:
check mysql --version. If output is like mysql Ver 15.1 Distrib 10.6.8-MariaDB then
mysqldump -h <hostname> -u <usename> -p --system=users > mysqldb_users.sql