Linux下MySQL备份与恢复实战
前言:本文主要介绍MySQL用备份与恢复,涉及到mysqldump,mysql,mysqlbinlog等技术实践。
一>Mysqldump备份库实战(有时候要分库备份)
1>备份mysql这个库到/opt/mysql_bak.sql文件里面(把数据库里的数据以sql形式的导出,属于逻辑备份)
[root@lll2 mysql]# mysqldump -uroot -p'123456' mysql >/opt/mysql_bak.sql -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
2>加上-B参数是使得导出语句中增加创建数据库和连接数据库的命令
[root@lll2 /]# mysqldump -uroot -p'123456' -B mysql >/opt/mysql_bak_B.sql
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly. [root@lll2 opt]# ls mysql_bak_B.sql mysql_bak.sql rh [root@lll2 opt]# diff mysql_bak_B.sql mysql_bak.sql 19,26d18 < -- Current Database: `mysql` < -- < < CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mysql` /*!40100 DEFAULT CHARACTER SET utf8 */; < < USE `mysql`; < < -- 745c737 < -- Dump completed on 2018-10-02 20:47:00 --- > -- Dump completed on 2018-10-01 17:43:24
3>将备份的mysql_bak_B.sql恢复
[root@lll2 opt]# mysql -uroot -p'123456' </opt/oldboy_bak_B.sql
4>压缩备份mysql某个库的数据
[root@lll2 opt]# mysqldump -uroot -p'123456' -B oldboy|gzip >/opt/oldboy_bak.sql.gz [root@lll2 opt]# ll 总用量 8 -rw-r--r-- 1 root root 1997 10月 2 20:58 oldboy_bak_B.sql -rw-r--r-- 1 root root 730 10月 2 21:13 oldboy_bak.sql.gz drwxr-xr-x. 2 root root 6 3月 26 2015 rh
5>利用-B参数备份多个库
[root@lll2 opt]# mysqldump -uroot -p'123456' -B oldboy mysql>/opt/oldboy_mysql.sql -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly. [root@lll2 opt]# ll 总用量 528 -rw-r--r-- 1 root root 1997 10月 2 20:58 oldboy_bak_B.sql -rw-r--r-- 1 root root 730 10月 2 21:13 oldboy_bak.sql.gz -rw-r--r-- 1 root root 528677 10月 2 21:42 oldboy_mysql.sql drwxr-xr-x. 2 root root 6 3月 26 2015 rh
二>MySQL单多表的备份实战(有时候要分表备份)
[root@lll2 /]# mysqldump -uroot -p'123456' --compact oldboy bm_jgbm -----备份单表:oldboy为库名,bm_jgbm为表名
/*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `bm_jgbm` ( `bm` varchar(20) NOT NULL, `mc` varchar(100) DEFAULT NULL, `version` int(10) DEFAULT '1', PRIMARY KEY (`bm`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; [root@lll2 /]# mysqldump -uroot -p'123456' --compact oldboy bm_jgbm bm_jgbm1 ----备份多表:oldboy为库名,bm_jgbm为表名,bm_jgbm1为表名 /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `bm_jgbm` ( `bm` varchar(20) NOT NULL, `mc` varchar(100) DEFAULT NULL, `version` int(10) DEFAULT '1', PRIMARY KEY (`bm`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `bm_jgbm1` ( `bm` varchar(20) NOT NULL, `mc` varchar(100) DEFAULT NULL, `version` int(10) DEFAULT '1' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */;
三>只备份MySQL表结构及只备份MySQL表数据
[root@lll2 /]# mysqldump -uroot -p'123456' --compact -d oldboy bm_jgbm -------d参数只备份表结构
/*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `bm_jgbm` ( `bm` varchar(20) NOT NULL, `mc` varchar(100) DEFAULT NULL, `version` int(10) DEFAULT '1', PRIMARY KEY (`bm`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; [root@lll2 /]# mysqldump -uroot -p'123456' --compact -t oldboy bm_jgbm -------t参数只备份表数据 INSERT INTO `bm_jgbm` VALUES ('1','1',111111111);
四>利用source恢复mysql数据库实践
mysql> drop database oldboy; Query OK, 2 rows affected (0.15 sec) mysql> source /opt/oldboy_bak_B.sql -----source恢复库 Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.01 sec) Database changed Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) mysql> mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | oldboy | | performance_schema | +--------------------+ 4 rows in set (0.01 sec) [root@lll2 /]# mysql -uroot -p123456 -e "use oldboy;select * from bm_jgbm where bm=1;" -----e实现非交互式的操作数据库 +----+--------+---------+ | bm | mc | version | +----+--------+---------+ | 1 | 嘿嘿 | 0 | +----+--------+---------+
五>MySQL进程,状态重要参数
1>查看数据库的连接
[root@lll2 /]# mysql -uroot -p123456 -e "show processlist;"
+----+------+---------------------+--------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+---------------------+--------+---------+------+-------+------------------+ | 7 | root | 192.168.0.103:52811 | NULL | Sleep | 7602 | | NULL | | 8 | root | 192.168.0.103:52813 | oldboy | Sleep | 7585 | | NULL | | 10 | root | 192.168.0.103:52823 | oldboy | Sleep | 7544 | | NULL | | 19 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+---------------------+--------+---------+------+-------+------------------+
2>查看参数
root@lll2 /]# mysql -uroot -p123456 -e "show variables;"|grep log_bin
log_bin OFF log_bin_trust_function_creators OFF sql_log_bin ON
3>查看状态
[root@lll2 /]# mysql -uroot -p123456 -e "show status;"
+------------------------------------------+-------------+ | Variable_name | Value | +------------------------------------------+-------------+ | Aborted_clients | 0 | | Aborted_connects | 0 | | Binlog_cache_disk_use | 0 | | Binlog_cache_use | 0 | | Binlog_stmt_cache_disk_use | 0 | | Binlog_stmt_cache_use | 0 | | Bytes_received | 137 | | Bytes_sent | 179 | | Com_admin_commands | 0 | | Com_assign_to_keycache | 0 | | Com_alter_db | 0 | | Com_alter_db_upgrade | 0 | | Com_alter_event | 0 | | Com_alter_function | 0 | | Com_alter_procedure | 0 | | Com_alter_server | 0 | | Com_alter_table | 0 | | Com_alter_tablespace | 0 | | Com_analyze | 0 | | Com_begin | 0 | | Com_binlog | 0 | | Com_call_procedure | 0 | | Com_change_db | 0 | | Com_change_master | 0 | | Com_check | 0 | | Com_checksum | 0 | | Com_commit | 0 | | Com_create_db | 0 | | Com_create_event | 0 | | Com_create_function | 0 | | Com_create_index | 0 | | Com_create_procedure | 0 | | Com_create_server | 0 | | Com_create_table | 0 | | Com_create_trigger | 0 | | Com_create_udf | 0 | | Com_create_user | 0 | | Com_create_view | 0 | | Com_dealloc_sql | 0 | | Com_delete | 0 | | Com_delete_multi | 0 | | Com_do | 0 | | Com_drop_db | 0 | | Com_drop_event | 0 | | Com_drop_function | 0 | | Com_drop_index | 0 | | Com_drop_procedure | 0 | | Com_drop_server | 0 | | Com_drop_table | 0 | | Com_drop_trigger | 0 | | Com_drop_user | 0 | | Com_drop_view | 0 | | Com_empty_query | 0 | | Com_execute_sql | 0 | | Com_flush | 0 | | Com_grant | 0 | | Com_ha_close | 0 | | Com_ha_open | 0 | | Com_ha_read | 0 | | Com_help | 0 | | Com_insert | 0 | | Com_insert_select | 0 | | Com_install_plugin | 0 | | Com_kill | 0 | | Com_load | 0 | | Com_lock_tables | 0 | | Com_optimize | 0 | | Com_preload_keys | 0 | | Com_prepare_sql | 0 | | Com_purge | 0 | | Com_purge_before_date | 0 | | Com_release_savepoint | 0 | | Com_rename_table | 0 | | Com_rename_user | 0 | | Com_repair | 0 | | Com_replace | 0 | | Com_replace_select | 0 | | Com_reset | 0 | | Com_resignal | 0 | | Com_revoke | 0 | | Com_revoke_all | 0 | | Com_rollback | 0 | | Com_rollback_to_savepoint | 0 | | Com_savepoint | 0 | | Com_select | 1 | | Com_set_option | 0 | | Com_signal | 0 | | Com_show_authors | 0 | | Com_show_binlog_events | 0 | | Com_show_binlogs | 0 | | Com_show_charsets | 0 | | Com_show_collations | 0 | | Com_show_contributors | 0 | | Com_show_create_db | 0 | | Com_show_create_event | 0 | | Com_show_create_func | 0 | | Com_show_create_proc | 0 | | Com_show_create_table | 0 | | Com_show_create_trigger | 0 | | Com_show_databases | 0 | | Com_show_engine_logs | 0 | | Com_show_engine_mutex | 0 | | Com_show_engine_status | 0 | | Com_show_events | 0 | | Com_show_errors | 0 | | Com_show_fields | 0 | | Com_show_function_status | 0 | | Com_show_grants | 0 | | Com_show_keys | 0 | | Com_show_master_status | 0 | | Com_show_open_tables | 0 | | Com_show_plugins | 0 | | Com_show_privileges | 0 | | Com_show_procedure_status | 0 | | Com_show_processlist | 0 | | Com_show_profile | 0 | | Com_show_profiles | 0 | | Com_show_relaylog_events | 0 | | Com_show_slave_hosts | 0 | | Com_show_slave_status | 0 | | Com_show_status | 1 | | Com_show_storage_engines | 0 | | Com_show_table_status | 0 | | Com_show_tables | 0 | | Com_show_triggers | 0 | | Com_show_variables | 0 | | Com_show_warnings | 0 | | Com_slave_start | 0 | | Com_slave_stop | 0 | | Com_stmt_close | 0 | | Com_stmt_execute | 0 | | Com_stmt_fetch | 0 | | Com_stmt_prepare | 0 | | Com_stmt_reprepare | 0 | | Com_stmt_reset | 0 | | Com_stmt_send_long_data | 0 | | Com_truncate | 0 | | Com_uninstall_plugin | 0 | | Com_unlock_tables | 0 | | Com_update | 0 | | Com_update_multi | 0 | | Com_xa_commit | 0 | | Com_xa_end | 0 | | Com_xa_prepare | 0 | | Com_xa_recover | 0 | | Com_xa_rollback | 0 | | Com_xa_start | 0 | | Compression | OFF | | Connections | 23 | | Created_tmp_disk_tables | 0 | | Created_tmp_files | 5 | | Created_tmp_tables | 0 | | Delayed_errors | 0 | | Delayed_insert_threads | 0 | | Delayed_writes | 0 | | Flush_commands | 1 | | Handler_commit | 0 | | Handler_delete | 0 | | Handler_discover | 0 | | Handler_prepare | 0 | | Handler_read_first | 0 | | Handler_read_key | 0 | | Handler_read_last | 0 | | Handler_read_next | 0 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | | Handler_rollback | 0 | | Handler_savepoint | 0 | | Handler_savepoint_rollback | 0 | | Handler_update | 0 | | Handler_write | 0 | | Innodb_buffer_pool_pages_data | 189 | | Innodb_buffer_pool_bytes_data | 3096576 | | Innodb_buffer_pool_pages_dirty | 0 | | Innodb_buffer_pool_bytes_dirty | 0 | | Innodb_buffer_pool_pages_flushed | 48 | | Innodb_buffer_pool_pages_free | 8002 | | Innodb_buffer_pool_pages_misc | 1 | | Innodb_buffer_pool_pages_total | 8192 | | Innodb_buffer_pool_read_ahead_rnd | 0 | | Innodb_buffer_pool_read_ahead | 0 | | Innodb_buffer_pool_read_ahead_evicted | 0 | | Innodb_buffer_pool_read_requests | 1253 | | Innodb_buffer_pool_reads | 187 | | Innodb_buffer_pool_wait_free | 0 | | Innodb_buffer_pool_write_requests | 163 | | Innodb_data_fsyncs | 35 | | Innodb_data_pending_fsyncs | 0 | | Innodb_data_pending_reads | 0 | | Innodb_data_pending_writes | 0 | | Innodb_data_read | 5246976 | | Innodb_data_reads | 197 | | Innodb_data_writes | 55 | | Innodb_data_written | 1590272 | | Innodb_dblwr_pages_written | 48 | | Innodb_dblwr_writes | 8 | | Innodb_have_atomic_builtins | ON | | Innodb_log_waits | 0 | | Innodb_log_write_requests | 18 | | Innodb_log_writes | 10 | | Innodb_os_log_fsyncs | 19 | | Innodb_os_log_pending_fsyncs | 0 | | Innodb_os_log_pending_writes | 0 | | Innodb_os_log_written | 12800 | | Innodb_page_size | 16384 | | Innodb_pages_created | 3 | | Innodb_pages_read | 186 | | Innodb_pages_written | 48 | | Innodb_row_lock_current_waits | 0 | | Innodb_row_lock_time | 0 | | Innodb_row_lock_time_avg | 0 | | Innodb_row_lock_time_max | 0 | | Innodb_row_lock_waits | 0 | | Innodb_rows_deleted | 0 | | Innodb_rows_inserted | 4 | | Innodb_rows_read | 7 | | Innodb_rows_updated | 0 | | Innodb_truncated_status_writes | 0 | | Key_blocks_not_flushed | 0 | | Key_blocks_unused | 13 | | Key_blocks_used | 0 | | Key_read_requests | 0 | | Key_reads | 0 | | Key_write_requests | 0 | | Key_writes | 0 | | Last_query_cost | 0.000000 | | Max_used_connections | 4 | | Not_flushed_delayed_rows | 0 | | Open_files | 4 | | Open_streams | 0 | | Open_table_definitions | 35 | | Open_tables | 3 | | Opened_files | 202 | | Opened_table_definitions | 0 | | Opened_tables | 0 | | Performance_schema_cond_classes_lost | 0 | | Performance_schema_cond_instances_lost | 0 | | Performance_schema_file_classes_lost | 0 | | Performance_schema_file_handles_lost | 0 | | Performance_schema_file_instances_lost | 0 | | Performance_schema_locker_lost | 0 | | Performance_schema_mutex_classes_lost | 0 | | Performance_schema_mutex_instances_lost | 0 | | Performance_schema_rwlock_classes_lost | 0 | | Performance_schema_rwlock_instances_lost | 0 | | Performance_schema_table_handles_lost | 0 | | Performance_schema_table_instances_lost | 0 | | Performance_schema_thread_classes_lost | 0 | | Performance_schema_thread_instances_lost | 0 | | Prepared_stmt_count | 0 | | Qcache_free_blocks | 0 | | Qcache_free_memory | 0 | | Qcache_hits | 0 | | Qcache_inserts | 0 | | Qcache_lowmem_prunes | 0 | | Qcache_not_cached | 0 | | Qcache_queries_in_cache | 0 | | Qcache_total_blocks | 0 | | Queries | 227 | | Questions | 2 | | Rpl_status | AUTH_MASTER | | Select_full_join | 0 | | Select_full_range_join | 0 | | Select_range | 0 | | Select_range_check | 0 | | Select_scan | 0 | | Slave_heartbeat_period | 0.000 | | Slave_open_temp_tables | 0 | | Slave_received_heartbeats | 0 | | Slave_retried_transactions | 0 | | Slave_running | OFF | | Slow_launch_threads | 0 | | Slow_queries | 0 | | Sort_merge_passes | 0 | | Sort_range | 0 | | Sort_rows | 0 | | Sort_scan | 0 | | Table_locks_immediate | 60 | | Table_locks_waited | 0 | | Tc_log_max_pages_used | 0 | | Tc_log_page_size | 0 | | Tc_log_page_waits | 0 | | Threads_cached | 0 | | Threads_connected | 4 | | Threads_created | 22 | | Threads_running | 1 | | Uptime | 13127 | | Uptime_since_flush_status | 13127 | +------------------------------------------+-------------+
[root@lll2 /]# mysql -uroot -p123456 -e "show status;"|grep select -----查看当前会话的数据库状态信息
Com_insert_select 0
Com_replace_select 0
Com_select 1
[root@lll2 /]# mysql -uroot -p123456 -e "show global status;"|grep select ----查看整个数据库运行状态信息
Com_insert_select 0
Com_replace_select 0
Com_select 54
六>MySQL的mysqlbinlog命令
binlog日志存在于MySQL的数据文件下面,用来记录MySQL内部增删改查等对MySQL数据库
有更新的内容的记录。MySQL的mysqlbinlog命令是解析binlog日志的。
[root@lll2 data]# ll 总用量 28728 -rw-rw---- 1 mysql mysql 18874368 10月 3 12:26 ibdata1 -rw-rw---- 1 mysql mysql 5242880 10月 3 12:26 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 7月 28 11:34 ib_logfile1 -rw-r----- 1 mysql root 28467 10月 3 12:26 lll2.err -rw-rw---- 1 mysql mysql 5 10月 3 12:26 lll2.pid -rw-r----- 1 mysql root 5498 7月 28 22:37 lll.err drwx------ 2 mysql root 4096 9月 23 22:30 mysql -rw-rw---- 1 mysql mysql 107 10月 3 12:26 mysql-bin.000001 -rw-rw---- 1 mysql mysql 19 10月 3 12:26 mysql-bin.index drwx------ 2 mysql mysql 39 10月 3 10:48 oldboy drwx------ 2 mysql mysql 4096 7月 28 11:25 performance_schema [root@lll2 data]# cat mysql-bin.index ./mysql-bin.000001
[root@lll2 data]# mysql -uroot -p123456 -e "use oldboy;update bm_jgbm set bm='000001' where bm=1"
[root@lll2 data]# mysqlbinlog mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!40019 SET @@session.max_insert_delayed_threads=0*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #181003 12:26:29 server id 1 end_log_pos 107 Start: binlog v 4, server v 5.5.32-log created 181003 12:26:29 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' 9US0Ww8BAAAAZwAAAGsAAAABAAQANS41LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAD1RLRbEzgNAAgAEgAEBAQEEgAAVAAEGggAAAAICAgCAA== '/*!*/; # at 107 #181003 12:36:22 server id 1 end_log_pos 177 Query thread_id=1 exec_time=1 error_code=0 SET TIMESTAMP=1538541382/*!*/; SET @@session.pseudo_thread_id=1/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=0/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; BEGIN /*!*/; # at 177 #181003 12:36:22 server id 1 end_log_pos 283 Query thread_id=1 exec_time=1 error_code=0 use `oldboy`/*!*/; SET TIMESTAMP=1538541382/*!*/; update bm_jgbm set bm='000001' where bm=1 /*!*/; # at 283 #181003 12:36:22 server id 1 end_log_pos 310 Xid = 4 COMMIT/*!*/; DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@lll2 data]# mysqlbinlog -d oldboy mysql-bin.000001 >/opt/oldboyBinLog.log ---- -d参数分库恢复binlog日志
总结mysqldump的主要常用参数:
1>--routines, -R(备份存储过程和函数) Include stored routines (procedures and functions) for the dumped databases in the output. 2>--triggers,(备份触发器) Include triggers for each dumped table in the output. This option is enabled by default; disable it with --skip-triggers.
一技之长,理财投资,写作思考 敬请关注“石林笔记”公众号或者扫我喲: