Mysql处理

当主从复制遇到错误:

    手动设置:>stop slave;

                          >set global sql_slave_skip_counter=1;  #跳过事物数量

         >start slave;

           修改配置:vi /etc/my.cnf  
          [mysqld]

          slave-skip-errors=1062,1053

          slave-skip-errors=all #跳过所有错误

    动态设置跳过错误:

          >set global slave_exec_mode =strict;
          严格执行策略。大多数情况下遇到错误,同步就会终止。等待错误解决。

          >set global slave_exec_mode =idempotent;
          这个设置,可以允许同步跳过
      

一主多从--主宕机从库切换主:

    1、等待所有从的relay-log与主的binlog同步完成;

    2、在所有从上执行:

          >stop slave io_thread;

          >show processlist;

          查看到Slave_SQL_Running_State: Slave has read allrelay log; waiting for more updates;表示更新完毕

          查看所有从的数据库目录下master.info,选择最大的POS为主(可能pos会一样大,选择性能和负载较好做主)

          然后删除master.info和relay-log.info文件;

          修改配置文件:

              开启binlog,注释log-slaves-updates=1和read-only=1

          >reset master;

          > show binary logs;

 

mysql误操作恢复(一定要开启binlog,否则无法恢复,且之前有全量备份):

     简单例子:

        安装:yum -y install mariadb mariadb-server

        编辑配置:vim /etc/my.cnf

          开启binlog:log-bin=mysql-bin
        开启服务:systemctl restart mariadb.service

        登录数据库:mysql

        >create database test_abc;   

        >use test_abc; 

        >create table test_1( id int not null auto_increment, name char(20) not null, age int not null, primary key(id)) engine=InnoDB;

        >desc test_1;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| name  | char(20) | NO   |     | NULL    |                |
| age   | int(11)  | NO   |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+

        > insert into test_1 values(1,"haha",12);

        > insert into test_1 values(2,"hehe",20);

        > insert into test_1 values(3,"sisi",16);

        > select * from test_1;                  
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | haha |  12 |
|  2 | hehe |  20 |
|  3 | sisi |  16 |
|  5 | xixi |  17 |
|  6 | xexe |  27 |
|  7 | enen |  22 |
+----+------+-----+
6 rows in set (0.00 sec)

        退出数据库,进程全量备份:mysqldump -B -F -R -x --master-data=2 test_abc|gzip >/opt/backup/test_abc_$(date +%F).sql.gz

        再次登录数据库:mysql(进行误操作)

        >insert into test_1 values(5,"xixi",17);

        > insert into test_1 values(6,"xexe",27);

        > insert into test_1 values(7,"enen",22);

          > select * from test_1;                  
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | haha |  12 |
|  2 | hehe |  20 |
|  3 | sisi |  16 |
|  5 | xixi |  17 |
|  6 | xexe |  27 |
|  7 | enen |  22 |
+----+------+-----+

        > drop database test_abc; 

        > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

        退出数据库,进行恢复:

          cd /opt/backup/

          gzip -d test_abc_2017-01-10.sql.gz

        查找全量备份之后操作的binlog:

          grep CHANGE test_abc_2017-01-10.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=245;

        将全量之后操作的binlog文件都拷贝到备份的目录:

          cp /var/lib/mysql/mysql-bin.000004 .

        将binlog转换为可执行sql:

          mysqlbinlog -d test_abc mysql-bin.000004 > 04bin.sql  

        编辑转换的SQL,去除drop操作:

          vim 04bin.sql

        把之前binlog备份:

          mkdir drop_mysql

          mv /var/lib/mysql/mysql-bin.* drop_mysql/

          tar -zcf drop_mysql.tgz drop_mysql/

        恢复操作:

          mysql < test_abc_2017-01-10.sql

          mysql < 04bin.sql

          > show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test_abc           |
+--------------------+
5 rows in set (0.00 sec)

  

posted @ 2017-01-10 16:25  songge1209  阅读(523)  评论(0编辑  收藏  举报