mysql主從複製

############mysql主從複製##########

實驗環境:
Redhat6.5    ##主從服務器版本一致
IP:172.25.8.1 master    ##主服務器或稱主庫
IP:172.25.8.2 slave    ##從服務器或稱從庫(可以多個)

實驗內容:
1.修改master主服務器
[mysqld]        ##在[mysqld]模塊內添加
log-bin=mysql-bin    ##啓用二進制日誌
server-id=1        ##服務器唯一ID,一般使用IP最後一段,master此處爲1

2.修改從服務器slave
[mysqld]        ##在[mysqld]模塊內添加
#log-bin=mysql-bin    ##從庫一般不設置,若有A-->B-->C級聯同步,中間的B數據庫服務要開啓log-bin
server-id=2        ##服務器唯一ID,一般使用IP最後一段,slave此處爲2

3.重啓主從服務器的mysql
/etc/init.d/mysql restart

4.在主服務器建立賬戶及授權slave
mysql> grant replication slave on *.* to 'rep'@'172.25.8.%' identified by 'hjy123456'
##設置複製用戶rep,“%”表示任意段,密碼hjy123456
mysql> show variables like 'server_id';    ##查看系統變量及其值
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 1     |    ##server-id=1
+---------------+-------+
1 row in set (0.00 sec)
mysql> select user,host from mysql.user;
+-------+------------+
| user  | host       |
+-------+------------+
| root  | 127.0.0.1  |
| rep   | 172.25.8.% |        ##rep用戶,172.25.8.%
| root  | localhost  |
| user1 | localhost  |
+-------+------------+
mysql> show master status;  ##顯示記錄在案的信息,主從複製時就要從這裏(259)開始
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      259 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)    
【執行完此步驟不要再操作master的mysql,防止Position變化】

5.在從服務器配置連接主服務器
mysql> change master to master_user='rep',master_host='172.25.8.1',master_port='3306',master_password='hjy123456',master_log_file='mysql-bin.000001',master_log_pos=259;
  ##rep是主服務器上建立的用於複製的用戶,172.25.8.1是主服務器的IP,3306是主庫端口,password是rep用戶的密碼,mysql-bin.000001是剛查的二進制日誌文件名,259是剛查的二進制日誌偏移量,複製讀取位置
mysql> show slave status\G;    ##查看複製狀態
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.8.1  ##主庫地址
                  Master_User: rep  ##授權賬戶
                  Master_Port: 3306 ##主庫端口
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 259  ##二進制日誌偏移量
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes    ##IO線程負責從從庫到主庫讀取log-bin日誌,此狀態必須爲YES
            Slave_SQL_Running: Yes    ##SOL線程負責讀取中繼日誌(relay-log)的數據轉化爲SQL語句應用到從庫中,此狀態必須爲YES【可以使用監控軟件,監控IO和SQL線程,NO時發送警報】                               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: 259
              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:


6.測試主從複製
1)主庫建立rep_db數據庫,並插入數據
mysql> create database rep_db ;    ##創建rep_db數據庫
mysql> use rep_db;        ##進入rep_db庫
mysql> create table rep_tb(id varchar(10),name varchar(20)); ####創建rep_tb表
mysql> desc rep_tb ;    ##查看rep_tb表結構
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | varchar(10) | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
mysql> insert into rep_tb values(001,'Tom'); ##插入以一條數據
mysql> select * from rep_tb ;    ##查看數據
+------+------+
| id   | name |
+------+------+
| 1    | Tom  |
+------+------+

2)從庫查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| rep_db             |
+--------------------+
3 rows in set (0.00 sec)
mysql> select * from rep_db.rep_tb;
+------+------+
| id   | name |
+------+------+
| 1    | Tom  |
+------+------+

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