Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

You typically save the verbose output of a Linux command like this:

# command > output.txt

But when you use a command like mysqldump, the > option outputs the dump of the database tables to the desired file:

# mysqldump --username=whatever --password=whatever -h localhost database > dump.sql

If you add in the -v (verbose) option to mysqldump, it outputs help information about what the command is doing. How do I save this verbose output to a file since the > option is used to output the database table information to a file?

More specifically, I'm dumping the output of one database into another database like this:

mysqldump -alv -h 123.123.123.123 --user=username --password=p@ssw0rd --add-drop-table databasename | mysql --user=username --password=p@ssw0rd -h localhost localdatabase

I tried the following but the output file was empty in the end

mysqldump -alv -h 123.123.123.123 --user=username --password=p@ssw0rd --add-drop-table databasename | mysql --user=username --password=p@ssw0rd -h localhost localdatabase > output.log

Also tried the following, but it redirected the database information to the output.log instead of the target databse:

mysqldump -alv -h 123.123.123.123 --user=username --password=p@ssw0rd --add-drop-table databasename > output.log | mysql --user=username --password=p@ssw0rd -h localhost localdatabase

And I don't see any sort of --log-file=FILE option for mysqldump either.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Your 1st mysqldump makes table structures and INSERTs and puts it in dump.sql.

Your 2nd dump is a remote dump that is piped straight into mysql in localhost.

If you are trying to catch any output based on errors, try this:

mysqldump -alv -h 123.123.123.123 --user=username --password=p@ssw0rd --add-drop-table databasename 2> output.log | mysql --user=username --password=p@ssw0rd -h localhost localdatabase

Using 2> will catch any error-based output (aka stderr). The mysqldump should still pipe normal console output (aka stdout) to the other mysql session and load the data as intended.

EXAMPLE : I have a small database called sample on my PC.

I ran this:

C:\LWDBA>mysqldump -u... -p... --verbose sample 2>sample.txt > sample.sql

C:\LWDBA>type sample.txt
-- Connecting to localhost...
-- Retrieving table structure for table users...
-- Sending SELECT query...
-- Retrieving rows...
-- Disconnecting from localhost...

C:\LWDBA>type sample.sql
-- MySQL dump 10.13  Distrib 5.5.12, for Win64 (x86)
--
-- Host: localhost    Database: sample
-- ------------------------------------------------------
-- Server version       5.5.12-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `users`
--

DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `users_tbl_points` int(11) NOT NULL,
  `users_tbl_rank` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `users_tbl_points` (`users_tbl_points`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `users`
--

LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` VALUES (1,785523,9),(2,443080,20),(3,858830,7),(4,964909,3),(5,248056,24),
(6,345553,21),(7,983596,2),(8,881325,6),(9,455836,19),(10,635204,16),(11,808514,8),
(12,136960,28),(13,259255,22),(14,885399,5),(15,649229,15),(16,589948,18),(17,2055,30),
(18,240429,25),(19,195981,26),(20,258620,23),(21,705158,12),(22,749931,11),(23,634182,17),
(24,921117,4),(25,703038,13),(26,751842,10),(27,650093,14),(28,994943,1),(29,24437,29),
(30,137355,27);
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2012-03-02 15:49:54

C:\LWDBA>

Give it a Try !!!

share|improve this answer
    
Thanks that worked great. Using 2> instead of > was the key. What does that 2 mean exactly? I thought 2> mean't you were outputting stderr in Linux... –  Jakobud Mar 2 '12 at 21:40
    
2> is stderr. I guess MySQL (eh Oracle [hate saying that]) meant --verbose to go to stderr to avoid confusion of mysqldump output with status output. –  RolandoMySQLDBA Mar 2 '12 at 21:52

mysqldump -v outputs to the stderr stream so you just need to redirect that

mysqldump -alv -h 123.123.123.123 --user=username --password=p@ssw0rd --add-drop-table databasename 2> dump.log | mysql --user=username --password=p@ssw0rd -h localhost localdatabase

example

 kormoc@Janet: ~ > mysqldump -v -u root mysql > /dev/null 2> output.log
 kormoc@Janet: ~ > cat output.log 
 -- Connecting to localhost...
 -- Retrieving table structure for table columns_priv...
 -- Sending SELECT query...
 -- Retrieving rows...
 -- Retrieving table structure for table db...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.