mysql主從複製(超簡單)

本文主要參考:  
http://369369.blog.51cto.com/319630/790921/ 並對文中錯誤進行更正    
其它參考內容:    
http://blog.csdn.net/hguisu/article/details/7325124    
http://pengranxiang.iteye.com/blog/1141118

推薦文檔:

http://www.cnblogs.com/kristain/articles/4142970.html


服務器ip  
DB1     10.10.6.211    
DB2     10.10.6.212

 

1、主、從服務器安裝mysql  

[root@DB1 ~]# yum install mysql mysql-server

2、修改主服務器配置:  

[root@DB1 ~]# more /etc/my.cnf  增加如下內容    
[mysqld]    
log-bin=mysql-bin      \\[必須]啓用二進制日誌    
server-id=211

3、修改從服務器配置:  

[root@DB2 ~]# more /etc/my.cnf  增加如下內容    
[mysqld]    
log-bin=mysql-bin             \\[必須]啓用二進制日誌    
server-id=212                 \\必須]服務器唯一ID,默認是1,一般取IP最後一段

4、重啓兩臺服務器的mysql    

[root@DB1 ~]# service mysqld restart

5、在主服務器上建立帳戶並授權給從服務器:  

[root@DB1 ~]# mysql    mysql> GRANT REPLICATION SLAVE ON *.* to 'mysync'@'10.10.6.212' identified by '123456';    Query OK, 0 rows affected (0.00 sec)    mysql>
6、登錄主服務器的mysql,查詢master的狀態   mysql> show master status;    +------------------+----------+--------------+------------------+    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |    +------------------+----------+--------------+------------------+    | mysql-bin.000003 |      260 |              |                  |    +------------------+----------+--------------+------------------+    1 row in set (0.00 sec)    \\注:執行完此步驟後不要再操作主服務器MYSQL,防止主服務器狀態值變化    mysql>

7、配置從服務器Slave:  
注意下面的語句,網上有很多資料都有錯誤    

mysql> change master to Master_host='10.10.6.211',master_user='mysync',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=260;
Query OK, 0 rows affected (0.01 sec)

\\replicate-ignore-db=mysql //不需要備份的數據庫;  這兩個語句可以根據實際情況
\\replicate-do-db=data //需要備份的數據庫 

mysql> start slave;  \\啓動從服務器複製功能
Query OK, 0 rows affected (0.00 sec)

mysql>

8、檢查從服務器複製功能狀態:  

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.6.211       \\主服務器地址
                  Master_User: mysync            \\授權帳戶名,儘量避免使用root
                  Master_Port: 3306              \\數據庫端口,部分版本沒有此行
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 260              \\同步讀取二進制日誌的位置,大於等於Exec_Master_Log_Pos
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes              \\此狀態必須YES
            Slave_SQL_Running: Yes              \\此狀態必須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: 260
              Relay_Log_Space: 407
              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)

mysql>

注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,否則都是錯誤的狀態(如:其中一個NO均屬錯誤)。

以上操作過程,主從服務器配置完成。


9、主從服務器測試:  
主服務器Mysql,建立數據庫,並在這個庫中建表插入一條數據:

mysql> create database jedy_db; 
Query OK, 1 row affected (0.00 sec)

mysql> use jedy_db;
Database changed
mysql> create table jedy_test(id int(3),name char(10));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into jedy_test values(001,'tian');
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jedy_db            |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql>


從服務器Mysql查詢:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jedy_db            |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> 


mysql> use jedy_db;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_jedy_db |
+-------------------+
| jedy_test         |             \\主服務器上新建的庫傳過來了
+-------------------+
1 row in set (0.00 sec)

mysql> select * form jedy_test;   \\可以看到在主服務器上新增的具體數據
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'form jedy_test' at line 1
mysql> select * from jedy_test;  
+------+------+
| id   | name |
+------+------+
|    1 | tian |
+------+------+
1 row in set (0.00 sec)

mysql>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章