环境:某项目的DB19数据库服务器,无从库也无开过binlog日志,负载有时比较高但基本运行稳定。备份情况:本地每天完整备份保留一周再rsync到异地备份机保留一月,由于数据量的增长加上每天晚上rsync到异地机时流量非常大,所以单独建台虚拟机作为DB19的备份。

备份措施:

1) DB19作为主库开启binlog日志。
2) DB19为从库开设同步帐号并开放复制权限。
3) DB19写脚本在凌晨一点锁表进行一次完整备份,并记录锁表前的binlog文件和position位置点(show master status\G;可查看信息)
4)  将备份的文件导入到从库并设置主从同步.
5)  从库上脚本实施每月1号本地进行一次全备,每天00-20每3小时进行一次增量备份(备份binlog日志)

1.DB19 主库设置并开启binlog日志

my.cnf配置文件更改:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

server-id=1       #id号在LAN中唯一

log-bin=log              #开启binlog日志
binlog-ignore-db=mysql   #忽略权限库的同步

max_connections=1000

long_query_time = 10
log-slow-queries = /var/log/mysql/mysql_slow.log  #开启慢查询日志

2. DB19为从库开设同步帐号

mysql>grant replication slave on *.* to identified by '123456';
mysql> flush privileges;
mysql> show grants for ";

3. DB19凌晨一点完整备份脚本并记录锁表前master status信息

#!/bin/bash
################################################
#this scripts is bakdb19
################################################

MYUSER=root

MYPASS="pwd19"
MYSOCK=/var/lib/mysql/mysql.sock

DATA_PATH=/backup/0314

LOG_FILE=${DATA_PATH}/mysqllogs_0314.log
DATA_FILE=${DATA_PATH}/mysql_backup_0314.sql.gz

MYSQL_PATH=/usr/bin

MYSQL_CMD="$MYSQL_PATH/mysql -u$MYUSER -p$MYPASS -S $MYSOCK"

MYSQL_DUMP="$MYSQL_PATH/mysqldump -u$MYUSER -p$MYPASS -S $MYSOCK -A  -B --flush-logs --single-transaction"

$MYSQL_CMD -e "flush tables with read lock;"

echo "-----show master status result-----" >>$LOG_FILE

$MYSQL_CMD -e "show master status;" >>$LOG_FILE
${MYSQL_DUMP} | gzip > $DATA_FILE
$MYSQL_CMD -e "unlock tables;"

脚本加入到crontab中:

0   01  *  *  *  sh /root/dbbak0314.sh>>/root/tasks/backup.log 2>&1

4. 将备份文件和记录master信息的文件.log复制到从库服务器

备份文件mysql_backup_0314.sql.gz
master信息的文件mysqllogs_0314.log
1)从库安装:
#yum install mysql mysq-server
#my.cnf配置文件:
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql

log-bin=log

#mysql_install_db
#/etc/init.d/mysqld start

2)数据导入

#gzip -d mysql_backup_0314.sql.gz
#mysql -S /data/mysql/mysql.sock <mysql_backup_0314.sql
#more mysqllogs_0314.log
-----show master status result-----
File    Position        Binlog_Do_DB    Binlog_Ignore_DB
log.000003      61139130                mysql

3)配置同步

# vi /root/.my.cnf   创建密码文件,省得mysql进去每次输入密码
[client]
password=pwd19

mysql> CHANGE MASTER TO

    ->   MASTER_HOST='192.168.7.19',
    ->   MASTER_USER='sqlsync',
    ->   MASTER_PASSWORD='123456',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='log.000003',    #指向锁表前记录的binlog文件和位置点,可查看mysqllogs_0314.log
    ->   MASTER_LOG_POS=61139130,
    ->   MASTER_CONNECT_RETRY=60;
Query OK, 0 rows affected (0.08 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;

*************************** 1. row ***************************
             Slave_IO_State: Queueing master event to the relay log
                Master_Host: 192.168.7.19
                Master_User: sqlsync
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: log.000028
        Read_Master_Log_Pos: 4
             Relay_Log_File: mysqld-relay-bin.000002
              Relay_Log_Pos: 229
      Relay_Master_Log_File: log.000003
           Slave_IO_Running: Yes
          Slave_SQL_Running: No
            Replicate_Do_DB:
        Replicate_Ignore_DB:
         Replicate_Do_Table:
     Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                 Last_Errno: 1062
                 Last_Error: Error 'Duplicate entry '197963' for key 1' on query. Default database: 'agrocms'. Query: 'INSERT INTO rocms.web_swgkbdxx (web_id,town_code,name,area,bdxx,swgk,bdxxsum,swgksum,day) values ('116597','330522105212','xx村','xx/xx/xx县/xx镇/xx村',0,0,0,17,'2013-03-14')'
               Skip_Counter: 0
        Exec_Master_Log_Pos: 61139130
            Relay_Log_Space: 5383075
            Until_Condition: None
             Until_Log_File:
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File:
         Master_SSL_CA_Path:
            Master_SSL_Cert:
          Master_SSL_Cipher:
             Master_SSL_Key:
      Seconds_Behind_Master: NULL
1 row in set (0.25 sec)

ERROR:

No query specified

mysql> INSERT INTO rocms.web_swgkbdxx (web_id,town_code,name,area,bdxx,swgk,bdxxsum,swgksum,day) values ('116597','330522105212','xx村','xx/xx/xx县/xx镇/xx村',0,0,0,17,'2013-03-14');

ERROR 1062 (23000): Duplicate entry '116597-2013-03-14' for key 2 提示错误重复插入
解决方法:
mysql>set global sql_slave_skip_counter=1;不行,因为后面很多重复的insert语句执行不了同样的提示错误
只好将slave-skip-errors=1062加入到my.cnf中 跳过错误代码为1062(重复更新)的所有语句.
mysql> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.7.19
                Master_User: sqlsync
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: log.000029
        Read_Master_Log_Pos: 189077906
             Relay_Log_File: mysqld-relay-bin.000058
              Relay_Log_Pos: 34636888
      Relay_Master_Log_File: log.000029
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB:
        Replicate_Ignore_DB:
         Replicate_Do_Table:
     Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                 Last_Errno: 0
                 Last_Error:
               Skip_Counter: 0
        Exec_Master_Log_Pos: 189077906
            Relay_Log_Space: 34636888
            Until_Condition: None
             Until_Log_File:
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File:
         Master_SSL_CA_Path:
            Master_SSL_Cert:
          Master_SSL_Cipher:
             Master_SSL_Key:
      Seconds_Behind_Master: 0
1 row in set (0.00 sec)

ERROR:

No query specified

已同步OK,可用checksum来检验主从同步数据的一致性,线上机器需锁表,暂不检验了

补充:

Slave_IO_Running: No 极有可能是网络问题或主库设了防火墙阻挡了,或设置的MASTER_LOG_POS位置信息不对.

测试OK,最好再将change master to信息在my.cnf配置文件中指定下:

# more /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql

log-bin=log

expire-logs-days=3
log-slave-updates

server-id=2

master-host=192.168.7.19
master-user=sqlsync
master-password=wer852
master-port=3306
master-connect-retry=60
relay-log=mysqld-relay-bin
 

5. 从库完整+增量备份脚本(暂略)

6. 恢复

1)目录级的备份,恢复时可直接进行目录的恢复frm,MYD,MYI文件

2)mysqldump全备加--tab参数时会生成sql和txt两个文件,sql记录所有包括create table的相关表结构更新语句,txt是数据文件。恢复时mysql先执行sql语句,再mysqlimport执行txt文件,如果表结构无更改,恢复时可直接mysqlimport导入txt文件.

3)mysqdump全备+增量备份,先最近一次的全备恢复,如果有多个binlog可按时间顺序逐个导入.