天天看點

MySQL Replication主從複制搭建MySQL安裝Replication主從複制配置測試總結參考

MySQL安裝

環境版本:

Linux:CentOS6.5

MySQL:5.7.19

IP:192.168.48.33(Master)、192.168.48.34(Slave)

安裝步驟:

1.根據Linux版本下載下傳RPM

2.安裝RPM

yum install mysql-community-release-el6-noarch.rpm
           

安裝成功後,我們可以看到/etc/yum.repos.d/目錄下增加了以下兩個檔案

# ls /etc/yum.repos.d
mysql-community-source.repo
mysql-community.repo
           

3.檢視MySQL可用安裝源

如果沒看到mysql57,可修改配置檔案(/etc/yum.repos.d/mysql-community.repo)使mysql57下面的enable=1

4.使用yum安裝MySQL

5.啟動MySQL服務

shell> service mysqld start
Initializing MySQL database:                               [  OK  ]
Starting mysqld:                                           [  OK  ]
           

6.檢視初始密碼

初始化MySQL會生成一個臨時密碼,需要馬上修改

shell> sudo grep 'temporary password' /var/log/mysqld.log
-09-T12::Z  [Note] A temporary password is generated for root@localhost: !q0Tp+OPXw
           

7.修改密碼

MySQL預設開啟了validate_password插件,要求密碼至少一個大寫字母一個小寫字母一個數字和一個特殊符号,長度至少8字元

shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass4!';
           

8.關閉IP通路限制

如果需要從其他主機通路,比如Windows用戶端連接配接,需要關閉IP通路限制:

grant all on 資料庫名.* to 使用者名@’用戶端IP位址’ identified by ‘密碼’;

部分步驟示例:

MySQL Replication主從複制搭建MySQL安裝Replication主從複制配置測試總結參考

Replication主從複制配置

在配置MySQL主從複制之前,最好先對MySQL主從複制的原理過程有個認識,這樣配置起來會輕松的多,可以看看筆者的上一篇部落格: MySQL Replication主從複制原理及拓撲結構

1.配置主庫

修改/etc/my.cnf配置檔案,啟用二進制日志并建立唯一服務ID

[mysqld]
log-bin=mysql-bin
server-id=
           

2.重新開機服務

shell> service mysqld start
           

3.主庫建立使用者

備庫連接配接到主庫需要賬号密碼,是以需要在主庫上建立使用者,并授予REPLICATION SLAVE權限。可以為每個備庫建立一個賬号也可以一起共用一個

mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com';
           
MySQL Replication主從複制搭建MySQL安裝Replication主從複制配置測試總結參考

4.确認二進制日志是否在主庫上建立

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      595 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
           

這裡顯示的二進制日志檔案名稱和偏移量可能和你定義的會有點不一樣

5.配置備庫

在另一台Linux伺服器上配置備庫,并重新開機服務

[mysqld]
log-bin=mysql-bin
server-id=
           

這裡還可以配置relay_log,指定中繼日志的位置和命名,log_slave_updates,允許備庫将其重放的事件也記錄到自身的二進制日志中。

6.啟動複制

文法:

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='master_host_name',
    ->     MASTER_USER='replication_user_name',
    ->     MASTER_PASSWORD='replication_password',
    ->     MASTER_LOG_FILE='recorded_log_file_name',
    ->     MASTER_LOG_POS=recorded_log_position;
           

比如筆者配置:

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='192.168.48.33',
    ->     MASTER_USER='repl',
    ->     MASTER_PASSWORD='Slave123!',
    ->     MASTER_LOG_FILE='mysql-bin.000001',
    ->     MASTER_LOG_POS=;
           

CHANGE MASTER TO文法詳情傳送門

7.檢查複制是否正确執行

mysql> show slave status\G;

*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.48.33
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 4
               Relay_Log_File: test-sz-34-relay-bin.000002
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
                            ...
           

可以看到Slave_IO_State、Slave_IO_Running、Slave_SQL_Running 三列顯示目前備庫複制尚未運作。

需要注意Read_Master_Log_Pos顯示目前日志開頭不是0而是4,這是因為0其實不是日志真正開始的位置,第一個事件從檔案的第4位開始讀。

8.啟動slave線程,開始複制

mysql> start slave;
           

再次檢視slave狀态:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.48.33
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 595
               Relay_Log_File: test-sz-34-relay-bin.000002
                Relay_Log_Pos: 808
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
                            ...
           

主庫上檢視線程,可以看到備庫I/O線程向主庫發起的連接配接

mysql> show processlist;
+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host                | db   | Command     | Time | State                                                         | Info             |
+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
|  4 | root | localhost           | NULL | Query       |    0 | starting                                                      | show processlist |
| 15 | repl | 192.168.48.34:45862 | NULL | Binlog Dump |  271 | Master has sent all binlog to slave; waiting for more updates | NULL             |
+----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
           

備庫上也可以看到一個I/O線程和SQL線程

mysql> show processlist;
+----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time  | State                                                  | Info             |
+----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
|  4 | root        | localhost | NULL | Query   |     0 | starting                                               | show processlist |
|  6 | system user |           | NULL | Connect |   204 | Waiting for master to send event                       | NULL             |
|  7 | system user |           | NULL | Connect | 12551 | Slave has read all relay log; waiting for more updates | NULL             |
+----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
           

測試

1.建庫建表示範:

MySQL Replication主從複制搭建MySQL安裝Replication主從複制配置測試總結參考

2.插入資料示範

MySQL Replication主從複制搭建MySQL安裝Replication主從複制配置測試總結參考

3.更新資料示範

MySQL Replication主從複制搭建MySQL安裝Replication主從複制配置測試總結參考

4.删除資料示範

MySQL Replication主從複制搭建MySQL安裝Replication主從複制配置測試總結參考

總結

本文介紹了MySQL一主一從拓撲結構的複制配置過程,總結起來就三步:

1.建立複制賬号

2.配置主庫和備庫

3.通知備庫連接配接到主庫複制資料

這裡介紹的都是主庫備庫剛剛安裝好且都是預設一緻的資料,并且知道目前主庫的二進制日志,但是通常的場景往往是主庫已經運作一段時間,然後安裝一台新的備庫與之同步,那麼我們就得首先初始化備庫,先将主庫之前的資料拷貝過來,然後找到主庫日志檔案坐标(logfile coordinates,就是二進制日志和位置),這樣一來就非常麻煩,好在MySQL5.6版本推出了新特性GTID(global transaction identifier),即全局事務辨別,能替代基于日志檔案坐标的主從複制,有興趣的同學可以自行學習,推薦閱讀:

1.https://dev.mysql.com/doc/refman/5.7/en/replication-gtids.html

2.https://yq.aliyun.com/articles/57731#

3.http://blog.csdn.net/leshami/article/details/50630691

參考

[1] Replication官網參考手冊

[2]《高性能MySQL》

繼續閱讀