1. 程式人生 > >讓兩臺伺服器的MySQL(5.7)資料同步_主主同步(互為主從關係)

讓兩臺伺服器的MySQL(5.7)資料同步_主主同步(互為主從關係)

網上找的大部分定義master-host的其實在新的MySQL中已經停用了,於是乎很悲劇的自己探索寫出了這麼一個東西

RedHat版本7.0

MySQL版本 5.7

伺服器名稱為假的!!!伺服器名稱為假的!!!伺服器名稱為假的!!!

重要事情說三遍~

另外需要注意的是,你同步的database裡面的內容必須一致!!必須一致!!必須一致!!!

你想要電腦精神錯亂嗎?給倆不一樣的資料讓人家同步,還互為主從,直接短路啊!

伺服器A:0.0.0.1

伺服器B: 0.0.0.2

1. 首先在兩個伺服器上分別插入可以訪問對方伺服器的賬號

在伺服器A中設定:

Create user 'clare'@'0.0.0.2' identitied by 'clare-test123';

grant all on *.* to 'clare'@'0.0.0.2';

grant replication slave on *.* to 'clare'@'0.0.0.2';

伺服器B則新增一模一樣的賬號:

Create user 'clare'@'0.0.0.1' identitied by 'clare-test123';

grant all on *.* to 'clare'@'0.0.0.1';

grant replication slave on *.* to 'clare'@'0.0.0.2';

2. 然後要在my.cnf中修改設定

就是這一步!!我累個去的網上的都是舊版本的不頂用了~!

伺服器A作為主伺服器,注意server-id=1

在伺服器A中的/etc/my.cnf中新增如下設定:

server-id = 1

log-bin=my-bin

log-bin-index=my-bin.index

binlog-ignore-db = mysql

binlog-ignore-db = information_schema

binlog-db=test;

replicate-ignore-db=mysql;

replicate-ignore-db=information_schema

replicate-db=test;

#主主需加入的部分
log-slave-updates
sync_binlog=1
auto_increment_offset=1
auto_increment_increment=2

注意!最後兩個log-bin和log-bin-index如果不配置的話,則在MySQL中使用show master status查不到任何記錄!!!

檔名設定隨便,但是目錄位置在my.cnf配置檔案中的datadir下

3. 伺服器B重複上面的操作。注意server-id=2,別的內容都一樣

4. 這時候重啟伺服器A\B的MySQL,應該是成功進入的

使用命令show master status應該能看到下面結果:


+---------------+----------+--------------+--------------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+---------------+----------+--------------+--------------------------+-------------------+
| my-bin.000001 |      154 |              | information_schema,mysql |                   |
+---------------+----------+--------------+--------------------------+-------------------+

5. 分別改變master的設定

A伺服器:

mysql>change master to master_host='0.0.0.2', master_user='clare', master_password='clare-test123', master_log_file='my-bin.000001', master_log_pos=154;

B伺服器:

mysql>change master to master_host='0.0.0.1', master_user='clare', master_password='clare-test123', master_log_file='my-bin.000001', master_log_pos=154;

這裡講解一下,master_log_file就是你用show master status命令看到的file一行的內容,而master_log_pos則是position的內容,不要設定錯了

6. 然後在A、B伺服器上開啟slave

start slave;

7. 檢視slave狀態

show slave status\G

我擷取一部分展示

  Slave_IO_State:
                  Master_Host: 0.0.0.1
                  Master_User: pccw
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: my-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: apisec-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: my-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

              Replicate_Do_DB: mobile
          Replicate_Ignore_DB: mysql,information_schema
       

中間有顏色的那倆貨是YES,就成功了

另外!!重要的事情說三遍!重要的事情說三遍!重要的事情說三遍!

如果你的tomcat 專案同時在A/B伺服器上部署的話,要記得,連線伺服器的URL這樣寫!

jdbc:mysql:loadbalance://[A伺服器IP地址]:3306,[B伺服器IP地址]:3306/mobile?useUnicode=true&characterEncoding=UTF-8

連線DB的賬戶要重新建立:

例如,使用者名稱叫test,密碼叫test123的話,那麼A,B伺服器的MySQL資料庫中需要存在如下(使用語句select user,host from user時候要顯示下面的情況)

---------------------------

user              host

--------------------------

test              A伺服器IP

test              B伺服器IP

建立賬戶的語句是:

create user 'test'@'A伺服器IP' identified by 'test123';

grant all on *.* to 'test'@'A伺服器IP' with grant option;

create user 'test'@'B伺服器IP' identified by 'test123';

grant all on *.* to 'test'@'B伺服器IP' with grant option;


記得!!!兩個伺服器的MySQL都要建立如上同樣一個賬號!!!

最近碰到一個問題,在這裡馬克一下

兩個資料庫同步因為時間差的關係,經常出現不一致,即使是設定了半同步也沒有用

so,為了保證執行通暢,目前只能採取最簡單粗暴的辦法解決

使用show slave status\G命令經常看到出錯的原因是1032錯誤,直接在配置檔案my.cnf中新增忽略1032錯誤......

具體方法就不說了,可以上網找到