Volcado de MySQL por consulta


¿Es posible hacer mysqldump por SQL query?

Me refiero a volcar toda la base de datos , como hace phpmyadmin cuando exportas a SQL

Author: LIGHT, 2009-06-01

9 answers

No mysqldump, sino mysql cli...

mysql -e "select * from myTable" -u myuser -pxxxxxxxxx mydatabase

Puede redirigirlo a un archivo si lo desea :

mysql -e "select * from myTable" -u myuser -pxxxxxxxx mydatabase > mydumpfile.txt

Actualizar: Post original preguntó si podía volcar desde la base de datos por consulta. Lo que preguntó y lo que quiso decir eran diferentes. Él realmente quería sólo mysqldump todas las mesas.

mysqldump --tables myTable --where="id < 1000"
 233
Author: Zak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-11 11:11:10

Esto debería funcionar

mysqldump --databases X --tables Y --where="1 limit 1000000"
 228
Author: Thomas Ahle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-01-14 23:42:34

Puede volcar una consulta como csv de esta manera:

SELECT * from myTable
INTO OUTFILE '/tmp/querydump.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
 61
Author: Guy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-01 03:48:54

Volcar una tabla usando una consulta where:

mysqldump mydatabase mytable --where="mycolumn = myvalue" --no-create-info > data.sql

Volcar una tabla completa:

mysqldump mydatabase mytable > data.sql

Notas:

  • Sustitúyase mydatabase, mytable, y la declaración where con sus valores deseados.
  • Por defecto, mysqldump incluirá las sentencias DROP TABLE y CREATE TABLE en su salida. Por lo tanto, si desea no eliminar todos los datos de su tabla al restaurar desde el archivo de datos guardado, asegúrese de usar la opción --no-create-info.
  • Es posible que deba agregar el -h, -u, y -p opciones a los comandos de ejemplo anteriores para especificar el host de base de datos deseado, el usuario y la contraseña, respectivamente.
 48
Author: Gary,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-25 00:29:46

Puede usar la opción where where en mysqldump para producir una salida que está esperando:

mysqldump -u root -p test t1 --where="1=1 limit 100" > arquivo.sql

Como máximo 100 filas a partir del ensayo.t1 será volcado desde la tabla de la base de datos.

Saludos, WB

 37
Author: Wagner Bianchi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-07 22:18:52

Combinando mucho de lo anterior aquí está mi verdadero ejemplo práctico, seleccionando registros basados en ambos meterid y timestamp. He necesitado este comando durante años. Se ejecuta muy rápido.

mysqldump -uuser -ppassword main_dbo trHourly --where="MeterID =5406 AND TIMESTAMP<'2014-10-13 05:00:00'" --no-create-info --skip-extended-insert | grep  '^INSERT' > 5406.sql
 2
Author: zzapper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-20 10:01:44

MySQL Workbench también tiene esta característica claramente en la GUI. Simplemente ejecute una consulta, haga clic en el icono guardar junto a Exportar / importar:

introduzca la descripción de la imagen aquí

Luego elija " SQL INSERT statements ( * .sql)" en la lista.

introduzca la descripción de la imagen aquí

Introduzca un nombre, haga clic en guardar, confirme el nombre de la tabla y tendrá su archivo de volcado.

 2
Author: MPelletier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-07 03:00:34

Si desea exportar su última n cantidad de registros a un archivo, puede ejecutar lo siguiente:

mysqldump -u user -p -h localhost --where "1=1 ORDER BY id DESC LIMIT 100" database table > export_file.sql

Lo anterior guardará los últimos 100 registros en export_file.sql, suponiendo que la tabla desde la que está exportando tiene una columna de id de incremento automático.

Tendrá que modificar los valores de usuario, host local, base de datos y tabla. Opcionalmente, puede modificar la columna id y exportar el nombre del archivo.

 1
Author: AUllah1,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-03 01:34:28

Mysql Exportar los resultados de la consulta línea de comandos:

mysql -h120.26.133.63 -umiyadb -proot123 miya -e "select * from user where id=1" > mydumpfile.txt
 -2
Author: lanni654321,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-29 12:26:30