mysql主從配置

根據網絡加實踐整理

 

一、環境
        主機:IP:172.17.31.125

        從機:IP:172.17.32.210

        需要同步的數據庫:hbws

二、master機和slave機的相關配置

1、修改master機器中mysql配置文件my.cnf,該文件在/etc目錄下
在[mysqld]配置段添加如下字段
server-id        = 1           #主機標示,整數
log_bin          = MySQL-bin   #確保此文件可寫
read-only        = 0           #主機,讀寫都可以
binlog-do-db     = hbws        #需要備份數據,多個寫多行
binlog-ignore-db = mysql       #不需要備份的數據庫,多個寫多行

在master機上爲slave機添加一同步帳號
GRANT REPLICATION SLAVE ON *.* TO 'backup'@'172.17.32.210' IDENTIFIED BY 'oracle' 
重啓master機的mysql服務:service mysqld restart

用show master status 命令看日誌情況
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000001 |       98 | hbws         | mysql            | 
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)

2、修改slave機中mysql配置文件
同樣在[mysqld]字段下添加如下內容
server-id       = 2
log_bin         = MySQL-bin
master-host     =172.17.32.210
master-user     =backup
master-pass     =oracle
master-port     =3306
master-connect-retry=60        #如果從服務器發現主服務器斷掉,重新連接的時間差(秒)
replicate-do-db =hbws          #只複製某個庫
replicate-ignore-db=mysql      #不復制某個庫

然後重啓slave機的mysql

在slave機中進入mysql
mysql>start slave;
mysql> show  slave status\G
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 172.17.31.125
                Master_User: backup
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: MySQL-bin.000003
        Read_Master_Log_Pos: 386
             Relay_Log_File: mysqld-relay-bin.000007
              Relay_Log_Pos: 523
      Relay_Master_Log_File: MySQL-bin.000003
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB: hbws
        Replicate_Ignore_DB: mysql
         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: 386
            Relay_Log_Space: 523
            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)


顯示       Slave_IO_Running: Yes
          Slave_SQL_Running: Yes

表示配置正確

三、測試主從服務器是否能同步
在主服務器上面新建一個表,必須在需要同步的數據庫(hbws)下
mysql> use hbws;
Database changed
mysql> insert into sss values ('liuliu');
Query OK, 1 row affected (0.00 sec)

mysql> select * from sss;
+--------+
| name   |
+--------+
| asdf   | 
| liuliu | 
+--------+
2 rows in set (0.00 sec)

mysql> 


在從服務器查看是否同步過來
mysql> use hbws;
Database changed
mysql> select * from sss;
+--------+
| name   |
+--------+
| asdf   | 
| liuliu | 
+--------+
2 rows in set (0.00 sec)

 

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