阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

mysqldump –master-data参数实现主从复制快速部署

153次阅读
没有评论

共计 3827 个字符,预计需要花费 10 分钟才能阅读完成。

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…

mysqldump --master-data 参数实现主从复制快速部署

我们将全量备份恢复到从库
[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 status\G;
*************************** 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)

验证成功!

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133529.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3827字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中