日期:2014-05-16  浏览次数:20637 次

MySQL的经典用法(十三)----主从备份
主数据库,2台数据库的结构要相同

主数据库
./bin/mysql --defaults-file=/root/mysql/my.cnf -uroot

【可选】给test数据库授权,给远程数据库登录使用
grant all privileges on test.* to test@'%'  identified by 'test';

建立账户,供slave同步使用
GRANT REPLICATION SLAVE ON *.* to 'test'@'192.168.56.3' identified by 'test';

Mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 261 | | |
+------------------+----------+--------------+------------------+
记录下 FILE 及 Position 的值,在后面进行从服务器操作的时候需要用到。




从数据库
./bin/mysql --defaults-file=/root/mysql/my.cnf -uroot

【可选】给test数据库授权,给远程数据库登录使用
grant all privileges on test.* to test@'%'  identified by 'test';

执行同步SQL语句,地址为主数据库地址,和主数据库上的用户名和密码,以及 File ,Position

change master to
master_host='192.168.56.2',
master_user='test',
master_password='test',
master_log_file='mysql-bin.000005',
master_log_pos=261;

正确执行后启动Slave同步进程
start slave;




主从同步检查
mysql> show slave status\G
==============================================
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.2
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 258
               Relay_Log_File: Slave2-relay-bin.000003
                Relay_Log_Pos: 404
        Relay_Master_Log_File: mysql-bin.000008
             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: 258
              Relay_Log_Space: 561
              Until_Condition: None
             &nbs