Showing posts with label Mysql DUMP. Show all posts
Showing posts with label Mysql DUMP. Show all posts

Wednesday, 14 November 2018

how to mysqldump remote db from local machine

I need to do a mysqldump of a database on a remote server, but the server does not 
have mysqldump installed. I would like to use the mysqldump on my machine to connect
 to the remote database and do the dump on my machine.
I have tried to create an ssh tunnel and then do the dump, but this does not seem to work.
 I tried:
ssh -f -L3310:remote.server:3306 user@remote.server -N
The tunnel is created with success. If I do
telnet localhost 3310
I get some blurb which shows the correct server mysql version. However, doing the 
following seems to try to connect locally
mysqldump -P 3310 -h localhost -u mysql_user -p database_name table_name

 Answers


As I haven't seen it at serverfault yet, and the answer is quite simple:
Change:
ssh -f -L3310:remote.server:3306 user@remote.server -N
To:
ssh -f -L3310:localhost:3306 user@remote.server -N
And change:
mysqldump -P 3310 -h localhost -u mysql_user -p database_name table_name
To:
mysqldump -P 3310 -h 127.0.0.1 -u mysql_user -p database_name table_name
(do not use localhost, it's one of these 'special meaning' nonsense that probably connects
 by socket rather then by port)
edit: well, to elaborate: if host is set to localhost, a configured (or default) --socket 
option is assumed. See the manual for which option files are sought / used. Under Windows, 
this can be a named pipe.

Friday, 9 November 2018

How do I use mysqldump to export only the CREATE TABLE commands?

I'm trying to use mysqldump to export only the DB schema -- no data, no additional SQL comments, just the CREATE TABLE commands. Here's what I've got so far:
mysqldump -h localhost -u root -p --no-data --compact  some_db
It almost achieves what I want, but I'd like to eliminate the "character set" lines (those like the first 3 lines in the example output below). Is there a mysqldump option to do that?
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `foo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `bar_id` int(11) DEFAULT NULL,
  `bazz` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=369348 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bar` (
...etc.
Here's my version info, in case that matters:
mysqldump Ver 10.13 Distrib 5.1.34, for Win32 (ia32)
mysql Ver 14.14 Distrib 5.1.34, for Win32 (ia32)

 Answers


This uses grep as well, but it seems to work:
mysqldump -d --compact --compatible=mysql323 ${dbname}|egrep -v "(^SET|^/\*\!)"
I'm using:
Ver 10.11 Distrib 5.0.51a, for debian-linux-gnu (x86_64)



mysqldump --compact --no-set-names --skip-opt --no-data DB | sed "/ SET /d"



Thursday, 8 November 2018

Mysql: mysqldump data only

I am looking for the syntax for dumping all data in my mysql database. I don't want any table information.

 Answers


mysqldump --no-create-info ...
If you are using triggers you also need to include --skip-triggers
And if you are using the --databases ... option you also need to include --no-create-db



 >> 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



If you just want the INSERT queries, use the following:
mysqldump --skip-triggers --compact --no-create-info



Try to dump to a delimited file.
mysqldump -u [username] -p -t -T/path/to/directory [database] --fields-enclosed-by=\" --fields-terminated-by=,

Using a .php file to generate a MySQL dump

Here's the information I have:
I am working with a Linux based system using MySQL and PHP5. I need to be able to generate a mysqldump from within a .php file, and then have that dump be stored in a file on the server in a location I would specify.
As I'm a PHP nooblet, I'd like someone to give me some assistance, guidance, or code, that would do what I require. This would have to be run remotely from the Internet.

 Answers


You can use the exec() function to execute an external command.
Note: between shell_exec() and exec(), I would choose the second one, which doesn't return the output to the PHP script -- no need for the PHP script to get the whole SQL dump as a string : you only need it written to a file, and this can be done by the command itself.

That external command will :
  • be a call to mysqldump, with the right parameters,
  • and redirect the output to a file.
For example :
mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql

Which means your PHP code would look like this :
exec('mysqldump --user=... --password=... --host=... DB_NAME > /path/to/output/file.sql');

Of course, up to you to use the right connection information, replacing the ... with those.



Take a look here: https://github.com/ifsnop/mysqldump-php ! It is a native solution written in php.
You can install it using composer, and it is as easy as doing:
<?php

use Ifsnop\Mysqldump as IMysqldump;

try {
    $dump = new IMysqldump\Mysqldump('database', 'username', 'password');
    $dump->start('storage/work/dump.sql');
} catch (\Exception $e) {
    echo 'mysqldump-php error: ' . $e->getMessage();
}

?>
It supports advanced users, with lots of options copied from the original mysqldump.
All the options are explained at the github page, but more or less are auto-explicative:
$dumpSettingsDefault = array(
    'include-tables' => array(),
    'exclude-tables' => array(),
    'compress' => 'None',
    'no-data' => false,
    'add-drop-database' => false,
    'add-drop-table' => false,
    'single-transaction' => true,
    'lock-tables' => false,
    'add-locks' => true,
    'extended-insert' => true,
    'disable-foreign-keys-check' => false,
    'where' => '',
    'no-create-info' => false
);



As long as you are allowed to use exec(), you can execute shell commands through your PHP code.
So assuming you know how to write the mysqldump in the command line, i.e.
mysqldump -u [username] -p [database] > [database].sql
then you can use this as the parameter to exec() function.
exec("mysqldump -u mysqluser -p my_database > my_database_dump.sql");



For security reasons, it's recommended to specify the password in a configuration file and not in the command (a user can execute a ps aux | grep mysqldump and see the password).
//create a temporary file
$file   = tempnam(sys_get_temp_dir(), 'mysqldump');

//store the configuration options
file_put_contents($file, "[mysqldump]
user={$user}
password=\"{$password}\"");

//execute the command and output the result
passthru("mysqldump --defaults-file=$file {$dbname}");

//delete the temporary file
unlink($file);



None of the above codes worked for me. I am using windows. Below Code worked for me...
$sql = "SELECT * FROM  $tableName WHERE yourclause";
$result = $conn->query($sql);


if($result){

        if ($result->num_rows > 0) {

            $myfile = fopen("daily_events_$district.sql", "w") or die("Unable to open file!");

            while($row = $result->fetch_assoc()) {  

                $rowToString = implode("','",$row);
                $writeToFile = "INSERT INTO $tableName VALUES('$rowToString');". PHP_EOL;
                fwrite($myfile,$writeToFile);
            }
            echo "File saved successfully";
        }
    } else {
        echo "No result found";
    }
This will save file in your project folder according to your query whatever data you want. If you want to dump whole database.



<?php exec('mysqldump --all-databases > /your/path/to/test.sql'); ?>
You can extend the command with any options mysqldump takes ofcourse. Use man mysqldump for more options (but I guess you knew that ;))



To dump database using shell_exec(), below is the method :
shell_exec('mysqldump -h localhost -u username -ppassword databasename  | gzip > dbname.sql.gz');



<?php
    $toDay = date('d-m-Y');

    $dbhost =   "localhost";
    $dbuser =   "YOUR DB USER";
    $dbpass =   "USER PASSWORD";
    $dbname =   "DB NAME";

    exec("mysqldump --user=$dbuser --password='$dbpass' --host=$dbhost $dbname > /home/....../public_html/".$toDay."_DB.sql");


?>

Mysqldump: create column names for inserts when backing up

How do I instruct mysqldump to backup with column names in insert statements?
In my case I didn’t a normal back up with insert sql’s resulting in
LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` 
INSERT INTO `users` VALUES (1
structure.
Now I went ahead and removed a column from the schema in users. After this when I run the backup sql’s I get a column number mismatch error.
To fix this how do I go about instructing mysqldump to write column names too? Here is how I do it now
mysqldump --host=${dbserver} --user=${dbusername} --password=${dbpassword} \
          --no-create-db --no-create-info --extended-insert --single-transaction \
          --compress tablename  
On a broader level, what’s the best practice to manage these schema changes?

 Answers


Use --complete-insert in the mysqldump command params

mysqldump entire structure but only data from selected tables in a single command

My database has 3 tables: table1, table2 and table3
I would like to do a mysqldump on this database with the following conditions:
  • Dump structure for all tables
  • Only dump data for table1 and table2, ignore data in table3
Currently, I do this with 2 mysqldump statements
mysqldump -u user -p -d db > db_structure.sql
mysqldump -u user -p db --ignore-table=db.table3 > table1_and_table2_data.sql
Import them in the same order they were dumped (structure, then data from table1 and table2)
Is there a way to combine this into a single mysqldump command?

 Answers


You can't combine them in one command but you can execute both commands at the same time and output to the same file.
mysqldump -u user -p --no-data db > structure.sql; mysqldump -u user -p db table1 table2 >> structure.sql
to avoid having to enter the password twice you can do -ppassword (note the lack of space!). Also use --no-data in the first command or you end up with the data as well. -d isn't needed when you're doing just one database.



I don't think you can do it in one command. But you definitely can merge the output to one file. Why not to wrap it in some shell script that does following:
mysqldump -u $1 -p$2 -d db > dump.sql && mysqldump -u $1 -p$2 db --ignore-table=db.table3 >> dump.sql
You will run this script with two parameters: username and password.



You can remove the INSERT INTO ... part:
mysqldump \
  --opt \
  -u ${DB_USER} -p${DB_PASS} \
  ${DB_NAME} \
  | grep -v 'INSERT INTO `table3`' \
  | grep -v 'INSERT INTO `table4`'

Minimum GRANTs needed by mysqldump for dumping a full schema? (TRIGGERs are missing!!)

I have a MySQL user called dump with the following perms:
GRANT USAGE ON *.* TO 'dump'@'%' IDENTIFIED BY ...
GRANT SELECT, LOCK TABLES ON `mysql`.* TO 'dump'@'%'
GRANT SELECT, LOCK TABLES ON `myschema`.* TO 'dump'@'%'
I want to dump all data (included triggers and procedures) using the dump user. I call mysqldump in the following way:
mysqldump -u dump -p --routines --triggers --quote-names --opt \
    --add-drop-database --databases myschema > myschema.sql
Everything is OK with the dumped file except for the triggers, they are missing!!
The triggers are dumped correctly if I try mysqldump with root MySQL user:
mysqldump -u root -p --routines --triggers --quote-names --opt \
    --add-drop-database --databases myschema > myschema.sql
So, I guess it is a perms issue... what are the extra grants my dump MySQL user needs for doing the full dump correctly?

 Answers


Assuming by full dump you also mean the VIEWs and the EVENTs, you would need:
GRANT USAGE ON *.* TO 'dump'@'%' IDENTIFIED BY ...;
GRANT SELECT, LOCK TABLES ON `mysql`.* TO 'dump'@'%';
GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER ON `myschema`.* TO 'dump'@'%';
and if you have VIEWs that execute a function, then unfortunately you also need EXECUTE.
My own problem is: why do I need SELECT if I only want to make a no-data dump?



I found, that sometime if VIEW DEFINER user does not exist, dump fails.
Change it, as described there