Home > Database > Mysql Tutorial > [MySQL] 复制(3)- 创建主备复制(从另一个服务器开始复制)_MySQL

[MySQL] 复制(3)- 创建主备复制(从另一个服务器开始复制)_MySQL

WBOY
Release: 2016-06-01 13:27:05
Original
804 people have browsed it

bitsCN.com

前一篇讲到的创建主备复制是假设主备库都为刚刚安装好的数据库,也就是说两台服务器上的数据相同,这不是典型的案例,大多数情况下有一个已经运行了一段时间的主库,然后用一台新安装的备库与之同步,本文讲述在这种情况下该如何配置。

1、在备库的主机上安装MySQL,注意备库的版本不能低于主库。

2、备份主库,复制备份文件到备库,并在备库上恢复。

可以有很多种方法实现上述过程,我这里介绍的是利用innobackupex在线备份主库,这样就不需要停主库(注意:innobackupex只适用于MyISAM和innodb引擎)。

首先,在主库上执行如下语句进行全备:

innobackupex --defaults-file=/opt/mysql/my.cnf  --user=root --password=*** /backup/mysql/data  
Copy after login
然后把备份文件全部拷贝至备库相同目录下,接着执行如下语句进行恢复:

innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password=***--use-memory=4G --apply-log /data/mysql/backup/2013-11-27_18-18-51innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password=***--copy-back /data/mysql/backup/2013-11-27_18-18-51
Copy after login
恢复完成后,启动MySQL服务器:

service mysqld start
Copy after login
3、启动复制

在备库启动复制之前,我们需要在主库上创建备份账号:

GRANT REPLICATION SLAVE ON *.* TO repluser@'192.168.1.%' IDENTIFIED BY 'replpwd';
Copy after login
然后,我们要确定主库的日志及其偏移量。这里要特别注意,我们不能通过show master status获得,因为在之前备份和复制的过程中,主库一直有新的日志产生,为了不造成数据丢失,我们必须从备份日志里获得。

[mysql@lx16 2013-11-27_18-18-51]$ cat xtrabackup_binlog_infomysql-bin.000149        69191646
Copy after login
从上面的备份日志里我们就能获得备份时主库的日志及其偏移量,这样我们就可以通过如下语句在备库启动复制了:

change master to        master_host='192.168.1.15',        master_user='repluser',        master_password='replpwd',        master_log_file='mysql-bin.000149',        master_log_pos=69191646; start slave;
Copy after login
启动复制后,备库的状态如下:

root@(none) 03:43:47>show slave status/G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 192.168.1.15                  Master_User: repluser                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000149          Read_Master_Log_Pos: 194385112               Relay_Log_File: mysqld-relay-bin.000002                Relay_Log_Pos: 125193719        Relay_Master_Log_File: mysql-bin.000149             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: 194385112              Relay_Log_Space: 125193876              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: 0Master_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: 101 row in set (0.00 sec)
Copy after login

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template