MySQL 互為主備的簡單搭建

來源:互聯網
上載者:User

MySQL 互為主備的簡單搭建

192.168.190.128《====》192.168.190.129
 master----》slave
 slave《----master
 
1.在master 128 上配置my.cnf檔案,添加下列參數:
 server-id=1
 log-bin=mysql-bin
 log-salve-updates
 sync_binlog=1
 auto_increment_increment=2
 auto_increment_offset=1
 
 重新啟動mysql
 [root@calvin1 ~]# /etc/init.d/mysqld start
 Starting MySQL:  [  OK  ]
 [root@calvin1 ~]# mysql -u root -p123456
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 6
 Server version: 5.0.77-log Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql> show databases;
 +--------------------+
 | Database          |
 +--------------------+
 | information_schema |
| calvin            |
| calvin2            |
| mysql              |
| sampdb            |
| test              |
| testdb            |
+--------------------+
 7 rows in set (0.00 sec)
 

2.在master 129 上配置my.cnf檔案,添加下列參數:
 server-id=2
 log-bin=mysql-bin
 log-salve-updates
 sync_binlog=1
 auto_increment_increment=2
 auto_increment_offset=2
 
重新啟動mysql
 [root@calvin2 ~]# /etc/init.d/mysqld start
 Starting MySQL:  [  OK  ]
 [root@calvin2 ~]# mysql -u root -p123456
 Welcome to the MySQL monitor.  Commands end with ; or \g.
 Your MySQL connection id is 7
 Server version: 5.0.77-log Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
 
mysql> show databases;
 +--------------------+
 | Database          |
 +--------------------+
 | information_schema |
| calvin            |
| calvin2            |
| mysql              |
| sampdb            |
| test              |
| testdb            |
+--------------------+
 7 rows in set (0.00 sec)
 

3.在在master 128上建立複製帳號:
mysql> grant replication slave,file on *.* to mysync@'192.168.190.129' identified by '123456';
 Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
 Query OK, 0 rows affected (0.00 sec)
 
對所有表添加唯讀鎖:
 mysql> flush tables with read lock;
 Query OK, 0 rows affected (0.00 sec)
 
查看master二進位檔案,pos號:
 mysql> show master status\G;
 *************************** 1. row ***************************
            File: mysql-bin.000004
        Position: 906
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
 
ERROR:
No query specified
 
mysql> unlock tables;
 Query OK, 0 rows affected (0.00 sec)
 
mysql> stop slave;
 Query OK, 0 rows affected, 1 warning (0.00 sec)
 

4.在在master 129上建立複製帳號:
 mysql> grant replication slave,file on *.* to mysync@'192.168.190.128' identified by '123456';
 Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
 Query OK, 0 rows affected (0.00 sec)
 
對所有表添加唯讀鎖:
 mysql> flush tables with read lock;
 Query OK, 0 rows affected (0.00 sec)
 
查看master二進位檔案,pos號:
 mysql> show master status\G;
 *************************** 1. row ***************************
            File: mysql-bin.000001
        Position: 318
    Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
 
ERROR:
No query specified
 
mysql> unlock tables;
 Query OK, 0 rows affected (0.00 sec)
 
mysql> stop slave;
 Query OK, 0 rows affected (0.00 sec)
 
 
 
5.修改同步參數:
 根據第3步讀取的二進位檔案和pos號,配置master 128的同步參數:
 mysql> change master to
    -> master_host='192.168.190.129',
    -> master_user='mysync',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=318;
 Query OK, 0 rows affected (0.01 sec)
 
mysql> start slave;
 Query OK, 0 rows affected (0.00 sec)
 
根據第4步讀取的二進位檔案和pos號,配置master 129的同步參數:
 mysql> change master to
    -> master_host='192.168.190.128',
    -> master_user='mysync',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000004',
    -> master_log_pos=906;           
Query OK, 0 rows affected (0.01 sec)
 
mysql> start slave;
 Query OK, 0 rows affected (0.01 sec)
 

6.查看slave同步狀態(查看Slave_IO_Running和Slave_SQL_Running是否均為Yes):
 master128:
 mysql> show slave status\G;
 *************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.190.129
                Master_User: mysync
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 318
              Relay_Log_File: mysqld-relay-bin.000002
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000001
            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: 318
            Relay_Log_Space: 235
            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
 1 row in set (0.00 sec)
 
ERROR:
No query specified
 
master129:
 mysql>  show slave status\G;   
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.190.128
                Master_User: mysync
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000004
        Read_Master_Log_Pos: 906
              Relay_Log_File: mysqld-relay-bin.000002
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000004
            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: 906
            Relay_Log_Space: 235
            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
 1 row in set (0.00 sec)
 
ERROR:
No query specified

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.