Consultas y Transformaci贸n Avanzada de Datos
Dise帽o y Optimizaci贸n Avanzada de Bases de Datos con SQL y MySQL
Construcci贸n de Base de Datos en MySQL: Terminal, Tablas y Datos Simulados
Transformaci贸n de Datos con SQL: De Consultas Simples a Superqueries
Columnas Generadas en SQL
Expresiones Regulares y Slugs en SQL y MySQL: Casos de Uso Reales
Automatizaci贸n, Reutilizaci贸n y Eficiencia en Consultas
Vistas y Tablas Virtuales en SQL
Consultas Anidadas y Vistas Materializadas
Triggers y Vistas Materializadas en MySQL (Kinda)
Automatizaci贸n de Bases de Datos con Triggers en MySQL
Llaves Primarias e 脥ndices
Trabajo con Datos Avanzados (JSON)
Uso de JSON en MySQL: Almacenamiento Eficiente de Datos Estructurados
B煤squedas Avanzadas con JSON en MySQL: Indexaci贸n y Optimizaci贸n
Joins en SQL: Conecta Tablas y Mejora tus Consultas
Motores de Almacenamiento y Encodings
Gesti贸n de Usuarios y Permisos en SQL
Gesti贸n Avanzada y An谩lisis de Bases de Datos
Information Schema en MySQL: An谩lisis Interno de tu Base de Datos
Mysqldump
Domina las Migrations: Evoluciona y Gestiona tu Base de Datos con Eficiencia
Optimiza tus Decisiones con Metabase
You don't have access to this class
Keep learning! Join and start boosting your career
MySQL backup management is a fundamental skill for any professional working with databases. Having reliable backups not only protects against data loss, but also facilitates the migration and replication of environments. In this guide, we will explore how to use MySQLdump, a powerful tool that allows us to create complete and customized backups of our databases.
MySQLdump is a standalone program that comes bundled with most MySQL distributions. This tool allows us to create full or partial database backups, including:
The importance of MySQLdump lies in its flexibility and reliability. Through the command line, we can specify exactly what we want to back up and in what format, generating SQL files that can be used to restore the information when necessary.
It is crucial to understand the difference between these two concepts:
This distinction is fundamental to maintain proper database management practices.
To use MySQLdump, we need to access the command line and run the program with the appropriate parameters. Let's look at some practical examples:
mysqldump -u root -p platzi_sql > platzi_sql_complete.sql
This command will create a file named platzi_sql_complete.sql
containing:
The resulting file is a complete SQL script that can be run to recreate the database in its entirety.
mysqldump -u root -p --no-data platzi_sql > platzi_sql_schema.sql
This command generates a file containing only the database structure:
This type of backup is ideal for versioning, as it is compact and contains only the structural logic of the database.
A file generated by MySQLdump is a SQL script that contains instructions to recreate the database. Some interesting elements we can find:
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
These special comments (delimited by /*!
and */
) contain code that will only run on specific MySQL versions. For example:
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER....
This code will only run on MySQL 5.00.03 or higher, which ensures compatibility between different versions.
DROP TABLE IF EXISTS `build_products`;
CREATE TABLE `build_products` ( `id` int NOT NULL AUTO_INCREMENT, `bill_id` int NOT NULL, `product_id` int NOT NULL, `price` decimal(10,2) NOT NULL, `quantity` int NOT NULL, `total` decimal(10,2) GENERATED ALWAYS AS ((`price` * `quantity`)) VIRTUAL, PRIMARY KEY (`id`), KEY `bill_id` (`bill_id`), KEY `product_id` (`product_id`)) ENGINE=InnoDB AUTO_INCREMENT=1016 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
LOCK TABLES `build_products` WRITE;/*!40000 ALTER TABLE `build_products` DISABLE KEYS */;INSERT INTO `build_products` VALUES (1,87,5346,7406.35,15,'2025-02-10 01:30:00',...);/* ... more data ...*//*!40000 ALTER TABLE `build_products` ENABLE KEYS */;UNLOCK TABLES;
Notice how MySQLdump:
Restoring a database from a backup file is a simple process:
mysql -u root -p -d platzi_dos < platzi_sql_schema.sql
This command will execute all SQL statements contained in the backup file on the specified database.
When restoring a database, we must keep a few things in mind:
For example, if a view was created with:
CREATE VIEW db_status ASSELECT * FROM information_schema.tablesWHERE table_schema = 'platzi_sql';
When restoring it to a database named platzi_dos
, we will need to modify it:
CREATE OR REPLACE VIEW db_status ASSELECT * FROM information_schema.tablesWHERE table_schema = 'platzi_dos';
To keep your databases secure and recoverable:
MySQLdump is a powerful tool that every database professional should master. Its flexibility and reliability make it an indispensable ally for managing backups in MySQL environments. Take advantage of its capabilities to keep your data safe and your database structure correctly versioned.
Have you had any interesting experience with database backups? Do you use any particular strategy to manage your backups? Share your experience in the comments.
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?