用mysqldump --master-data 建立slave

用mysqldump --master-data 建立slave

先来看看官方文档的解释
Use this option to dump a master replication server to produce a dump file that can be used to set up another server as a slave of the master. It causes the dump output to include a  CHANGE MASTER TO  statement that indicates the binary log coordinates (file name and position) of the dumped server. These are the master server coordinates from which the slave should start replicating.
If the option value is 2, the  CHANGE MASTER TO  statement is written as an SQL comment, and thus is informative only; it has no effect when the dump file is reloaded. If the option value is 1, the statement takes effect when the dump file is reloaded. If the option value is not specified, the default value is 1.
大概就这么个意思:这个参数在建立slave数据库的时候会用到,当这个参数的值为1的时候,mysqldump出来的文件就会包括 CHANGE MASTER TO 这个 语句, CHANGE MASTER TO 后面紧接着就是fileposition的记录,fileposition记录的位置就是slavemaster端复制文件的起始位置。默认情况下这个值是1
当这个值是2的时候,chang master to也是会写到dump文件里面去的,但是不会有上面那个作用了thus is information only
翻译过来真感觉拗口,呵呵,凑活看看吧。上实例!
一、先dump一个库
mysqldump -uroot -p123456 --master-data=1  --quick --all-databases --flush-logs   --lock-all-tables > alldb.sql
 
二、观察fileposition的值,此时的table是被lock住不能写入的
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000019 |      106 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000019 |      106 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> show processlist;
+----+------+------------+--------+-------------+------+----------------------------------------------------------------+-------------------------------------------------------------+
| Id | User | Host       | db     | Command     | Time | State                                                          | Info                                                        |
+----+------+------------+--------+-------------+------+----------------------------------------------------------------+-------------------------------------------------------------+
|  1 | rep  | node2:2514 | NULL   | Binlog Dump |  757 | Has sent all binlog to slave; waiting for binlog to be updated | NULL                                                        |
|  3 | root | localhost  | test   | Query       |    0 | NULL                                                           | show processlist                                            |
|  4 | root | localhost  | orders | Query       |   23 | Writing to net                                                 | SELECT /*!40001 SQL_NO_CACHE */ * FROM `order_status_track` |
+----+------+------------+--------+-------------+------+----------------------------------------------------------------+-------------------------------------------------------------+
3 rows in set (0.00 sec)
 
三、 观察dump出来的文件,fileposition的值和上面是相同的
[root@node1 ~]# grep -i "CHANGE MASTER TO" alldb.sql
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000019', MASTER_LOG_POS=106;
 
四、编辑slave端配置文件如下
log-bin=mysql-bin
server-id = 3
master-host=192.168.1.201
master-port=3306
master-user=rep
master-password=rep
master-connect-retry=60
 
五、 重启slavemysql
[root@node1 mysql]# /etc/init.d/mysql stop
Shutting down MySQL......                                  [  OK  ]
[root@node1 mysql]# /etc/init.d/mysql start
Starting MySQL...                                          [  OK  ]
 
六、 此时netstat -an|grep 3306 看到slavemaster的连接已经建立。现在不需要它们之间连接,登陆slave端,执行stop slave,连接成功断开
 
七、 Mysqldump 导入master 导出的文件dumpfile
mysql  -uroot -p123456 < alldb.sql
 
八、开启slave端同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
在这一步骤,如果 --master-data  参数为二,此时你会发现你还需要输入
CHANGE MASTER TO
MASTER_HOST='192.168.1.202',
MASTER_USER='rep',
MASTER_PASSWORD='rep';
类似的参数后, 才能继续执行start slave。当然这里你也可以写到配置文件中去
九、查看slave端状态,大功告成
mysql> start slave;
Query OK, 0 rows affected (0.05 sec)
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.201
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000019
          Read_Master_Log_Pos: 106
               Relay_Log_File: node1-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000019
             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: 106
              Relay_Log_Space: 406
              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:
1 row in set (0.00 sec)
 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/758322/viewspace-680471/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/758322/viewspace-680471/

引用[1]:cat dump_and_replication.sh #!/bin/bash source ~/.bashrc mysql -uroot -p123456 -S /data/3306/mysqldata/mysql.sock -D db1 -e " set global sql_log_bin=0; alter table t1 drop primary key, drop index Index_1, drop index Index_2; " date mysqldump -uroot -p123456 -h192.168.1.36 -P3306 --quick --no-create-info --apply-slave-statements --single-transaction db1 t1 --master-data=1 --set-gtid-purged=OFF | mysql -uroot -p123456 -S /data/3306/mysqldata/mysql.sock -D db1 date mysql -uroot -p123456 -S /data/3306/mysqldata/mysql.sock -D db1 -e " stop slave; alter table t1 add primary key (id), add index Index_1 (c1,c2), add index Index_2 (c3,c4); start slave;" date mysql -uroot -p123456 -S /data/3306/mysqldata/mysql.sock -D db1 -e "set global sql_log_bin=1;" 问题: mysqldump 配置 回答: 在这个脚本中,mysqldump命令被用来备份MySQL数据库。具体的配置如下:使用mysqldump命令时,指定了以下参数: -uroot:指定MySQL的用户名为root -p123456:指定MySQL的密码为123456 -h192.168.1.36:指定要备份的MySQL服务器的IP地址为192.168.1.36 -P3306:指定要备份的MySQL服务器的端口号为3306 --quick:指定使用快速备份模式,加快备份速度 --no-create-info:指定不备份表的创建语句,只备份数据 --apply-slave-statements:指定备份时应用从库的语句 --single-transaction:指定使用单个事务来备份数据 db1 t1:指定要备份的数据库名为db1,要备份的表名为t1 --master-data=1:指定在备份文件中添加主库的信息 --set-gtid-purged=OFF:指定不清除GTID信息 这些配置可以根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值