MySQL Dump using --all-databases - mysql

I'm trying to create a full backup of multiple databases in MySQL using the --all-databases command but the output sql file is always only 1kb.
When I specify each database individually using just the --databases command the databases are backed up but it does not restore all tables when I upload the dump file to another MySQL installation.

Related

mysqldump include "use db_name" statement with single table dump

I'm trying to use mysqldump to programmatically create exports of single tables for a system of exporting and importing tables from our production environment into local development environments.
I need the .sql file to include a "USE database_name" statement at the top. The production db name is different than our local db name (we prefix test_ on development). It is imperative I preserve the .sql dump files as they are used on docker container initialization.
I can achieve this by including the --databases database_name flag in the mysqldump command. The problem is, this dumps the entire database. I only want a single table.
Does anyone know if this is possible somehow - to dump a single table but also include the "USE data_basename" line?
Here's what I have in a bash script, so far - where $1 is the specific table name. This works, but I have to manually enter the "USE database_name" statement at the top of the generated .sql file.
mysqldump -h {host i.p.} -u {username} --password="{password}" db_name $1 > docker/local_mysql_db/files/docker-entrypoint-initdb.d/$1.sql
docker exec -i db_container_name mysql {test_db_name} < docker/local_mysql_db/files/docker-entrypoint-initdb.d/$1.sql

Import MySQL XML Dump?

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?

how to migrate a large database to new server

I need to migrate my database from my old server to my new server. I have a very big problem by transferring the database because I have a large database with 5gb. I tried to transfer using c panel transfer but I can't it is not useful. I need a more efficient way to transfer the data.
Can anyone guide me with the full transfer details? How to transfer using import and export or do I need to use any other method?
MySQL type is MyISAM and size is 5gb.
You can try command line if you have access to SSH for both server as command below if not you can try using Navicat application to sync databases
SSH commands
Take mysqldump of database
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
create tar ball of SQL dump file using
tar -zcvf db.tar.gz db.sql
now upload tar.gz file to other server using scp command
scp -Cp db.gz {username}#{server}:{path}
now login on other server using SSH
Untar file using linux cmd
tar -zxvf db.tar.gz
import to database
mysql -u{username} -p {database} < db.sql
Please have a look at syntax though syntax will work but consider this as direction only
Thanks..
For large databases I would suggest to use mysqldump if you have SSH access to the server.
From the manual:
Use mysqldump --help to see what options are available.
The easiest (although not the fastest) way to move a database between two machines is to run the following commands on the machine on which the database is located:
shell> mysqladmin -h 'other_hostname' create db_name
shell> mysqldump db_name | mysql -h 'other_hostname' db_name
If you want to copy a database from a remote machine over a slow network, you can use these commands:
shell> mysqladmin create db_name
shell> mysqldump -h 'other_hostname' --compress db_name | mysql db_name
You can also store the dump in a file, transfer the file to the target machine, and then load the file into the database there. For example, you can dump a database to a compressed file on the source machine like this:
shell> mysqldump --quick db_name | gzip > db_name.gz
Transfer the file containing the database contents to the target machine and run these commands there:
shell> mysqladmin create db_name
shell> gunzip < db_name.gz | mysql db_name
You can also use mysqldump and mysqlimport to transfer the database. For large tables, this is much faster than simply using mysqldump. In the following commands, DUMPDIR represents the full path name of the directory you use to store the output from mysqldump.
First, create the directory for the output files and dump the database:
shell> mkdir DUMPDIR
shell> mysqldump --tab=DUMPDIR db_name
Then transfer the files in the DUMPDIR directory to some corresponding directory on the target machine and load the files into MySQL there:
shell> mysqladmin create db_name # create database
shell> cat DUMPDIR/*.sql | mysql db_name # create tables in database
shell> mysqlimport db_name DUMPDIR/*.txt # load data into tables
Do not forget to copy the mysql database because that is where the grant tables are stored. You might have to run commands as the MySQL root user on the new machine until you have the mysql database in place.
After you import the mysql database on the new machine, execute mysqladmin flush-privileges so that the server reloads the grant table information.

Transfer of complete databases in Mysql (WAMP)

I am working in a project heavily involving WAMP. The complete size of all the databases on the Mysql server in WAMP is around 150 mb. How do I transfer this between computers ? There seems to be a limit on the size of the file. Altering PHP MyAdmin doesn't seem to help.
Also, I usually import databases individually by creating a database with the same name and then selecting the database file to be imported. However, I have now exported all the databases collectively and it gave me a file called localhost.sql (With 8 databases in it)
How do I import this in a Mysql server in another computer?
Don't use PHPMyAdmin. Instead use mysqldump utility to create a dump file:
mysqldump --user=root --all-databases > dump.sql
This will create a dump.sql with all databases on your server. For information on how to select individual databases/tables see the the documentation
Then on another computer load the dump into mysql
mysql --user=root < dump.sql

Is there a way to copy all the data in a mysql database to another? (phpmyadmin)

I want to copy all the tables, fields, and data from my local server mysql to my hosting sites mysql. Is there a way to copy all the data? (It's only 26kb, very small)
In phpMyAdmin, just export a dump (using the export) tab and re-import it on the other server using the sql tab.
Make sure you compare the results, I have had phpMyAdmin screw up the import more than once.
If you have shell access to both servers, a combination of
mysqldump -u username -p databasename > dump.sql
and a
mysql -u username -p databasename < dump.sql
on the target server is the much more fast and reliable alternative in my experience.
Have a look at
Copying MySQL Databases to Another Machine
Copy MySQL database from one server to another remote server
Please follow the following steps:
Create the target database using MySQLAdmin or your preferred method. In this example, db2 is the target database, where the source database db1 will be copied.
Execute the following statement on a command line:
mysqldump -h [server] -u [user] -p[password] db1 | mysql -h [server]
-u [user] -p[password] db2
Note: There is NO space between -p and [password]
I copied this from Copy/duplicate database without using mysqldump.
It works fine. Please ensure that you are not inside mysql while running this command.
If you have the same version of mysql on both systems (or versions with compatible db file sytsem), you may just copy the data files directly. Usually files are kept in /var/lib/mysql/ on unix systems.