- Backup Tools
The mysqldump utility can be used on both the client and server sides.
The SELECT INTO OUTFILE command is limited to writing data directly to the server side. - Backup by Table
a. Backing up a single table
mysqldump -uusername -p database table1 >tableback.sql
mysql> select * into outfile 'D:/someBookes/mysql/pracitce/mysql_user.sql' fields terminated by ','
-> optionally enclosed by '"' escaped by '#'
-> lines terminated by '\n' from user;
Query OK, 7 rows affected (0.07 sec)
mysql> exit
Bye
A note: On Windows operating systems, backslashes are often ignored, so the file ends up being created directly in the root of the D drive. To create it in the correct directory, use forward slashes instead, as shown below:
mysql> use mysql;
Database changed
mysql> select * into outfile 'D:/someBookes/mysql/pracitce/mysql_user.sql' fields terminated by ','
-> optionally enclosed by '"' escaped by '#'
-> lines terminated by '\n' from user;
Query OK, 7 rows affected (0.00 sec)
mysql>
This method only exports data to the server side. To write the data to the client, wrap the SQL statement and execute it from the client side: mysql -h192.168.1.119 -uroot -ppasswd -e"select * from std.ygxxwh_b0;" > D:/someBookes/mysql/pracitce/stdd.sql
b. Backing up multiple tables
mysqldump -uusername -p database table1 table2 >tablesback.sql
- Backup by Database
a. Backing up a single database
mysqldump -uusername -p --database database1 >databaseback.sql
b. Backing up multiple databases
mysqldump -uusername -p --database database1 database2 >databasesback.sql - Common mysqldump Options
--add-drop-database Add a DROP DATABASE before each CREATE.
--add-drop-table Add a DROP TABLE before each CREATE (enabled by default).
--hex-blob Dump binary strings (BINARY, VARBINARY, BLOB) in hexadecimal format.
--no-data Export only the schema.
--single-transaction Create a consistent snapshot by dumping all tables in a single transaction. Works only for storage engines that support multiversioning (INNODB). - Recovery Tools
Since MySQL backups are generated in SQL format, running the backup script will restore the data.
- Components of MySQL Log File System
a. Error Log: Records issues encountered during startup, operation, or shutdown of MySQL.
b. Genarel Log: Records established client connections and executed SQL statements.
c. Binary Log: Records all statements that modify data. Also used for replication.
d. Slow Query Log: Records all queries that take longer than long_query_time seconds or do not use indexes.
e. InnoDB Log: InnoDB redo log
By default, all logs are created in the MySQL data directory.
You can force MySQL to close and reopen log files by refreshing them.
Execute FLUSH LOGS or mysqladmin flush-logs or mysqladmin refresh to rotate the logs.