Welcome 微信登录

首页 / 数据库 / MySQL / mysqldump --master-data参数实现主从复制快速部署

mysqldump --help
--master-data[=#] This causes the binary log position and filename to be
                      appended to the output. If equal to 1, will print it as a
                      CHANGE MASTER command; if equal to 2, that command will
                      be prefixed with a comment symbol. This option will turn
                      --lock-all-tables on, unless --single-transaction is
                      specified too (in which case a global read lock is only
                      taken a short time at the beginning of the dump; don"t
                      forget to read about --single-transaction below). In all
                      cases, any action on logs will happen at the exact moment
                      of the dump. Option automatically turns --lock-tables
                      off.--master-data[=#]     在备份导出的文件里追加二进制binlog文件的位置和名称
                        如果值等于1,就会添加一个CHANGE MASTER语句
                        如果值等于2,就会在CHANGE MASTER语句前添加注释(不起作用了呗~)
                        这个参数会--lock-all-tables锁表,除非你指定了--single-transaction
                        这种情况下,锁表只会在dump开始的时候持续一小段时间,照理说
                        在dump的时候,任何动作都会影响到binlog文件
                        dump结束之后,选项会自动关闭锁表功能不知道翻译的对不对,凑合看吧~~
简单的说,就是主从复制在做全量备份的时候,这个选项可以自动帮我们锁表和识别binlog临界文件,就不需要我们锁表,再看临界文件编号,再执行CHANGE MASTER填写binglong位置信息到从库master.info文件中了,提高了从库部署效率吧。实例测试一下 
备份当前数据库
#注意    在做主从复制备份数据库的时候,最好不要带mysql自带的几个库,如mysql、information_schema否则开启slave开关进行复制的时候会出现“Last_SQL_Error: Error "Can"t create database”错误,所以备份的时候要排除这几个库,又由于mysqldump只有ignore-table参数,并没有ignore-database可以用一下命令实现[root@db02 3309]# mysql -uroot -poldboy1234 -S /data/3306/mysql.sock -e "show databases;"| grep -Ev "Database|information_schema|performance_schema|mysql"|xargs mysqldump -uroot -poldboy1234 -S /data/3306/mysql.sock -B -F -R --master-data=1 --events|gzip > /server/backup/mysql_$(date +%F).sql.gz--maste-data参数自动在备份文件中添加了CHANGE MASTES TO...我们将全量备份恢复到从库
[root@db02 3309]# mysql -uroot -S /data/3309/mysql.sock < /server/backup/mysql_2016-07-07.sql配置CHANGE MASTER TO..命令
mysql> CHANGE MASTER TO MASTER_HOST="172.16.2.10", MASTER_PORT=3306, MASTER_USER="rep", MASTER_PASSWORD="oldboy123";
# 注意此时我没有配置MASTER_LOG_FILE和MASTER_LOG_POS
开启salve 开关
mysql> start slave;
mysql> show slave status;
mysql> show slave statusG;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.2.10
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000023
          Read_Master_Log_Pos: 279
             Relay_Log_File: relay-bin.000037
                Relay_Log_Pos: 344
        Relay_Master_Log_File: mysql-bin.000023
           Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql
         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: 279
              Relay_Log_Space: 640
              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
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
             Last_SQL_Errno: 0
             Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
           Master_Server_Id: 1
1 row in set (0.00 sec)我们在主库创建一个lilongzi数据库,来验证主库和从库是否连接成功[root@db02 3309]# mysql -uroot -poldboy1234 -S /data/3306/mysql.sock
mysql> cr
mysql> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| lilongzi         |
| lilongzi_gbk     |
| mysql              |
| performance_schema |
| test             |
| www                |
| zzz                |
+--------------------+
8 rows in set (0.00 sec)
eate database lilongzi;从库这边
[root@db02 3309]# mysql -uroot -S /data/3309/mysql.sock
mysql> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| lilongzi         |
| lilongzi_gbk     |
| mysql              |
| performance_schema |
| test             |
| www                |
| zzz                |
+--------------------+
8 rows in set (0.00 sec)验证成功!本文永久更新链接地址