MySQL5.6 Replication主從複製(讀寫分離) 配置完整版

來源:互聯網
上載者:User

MySQL5.6 Replication主從複製(讀寫分離) 配置完整版

MySQL5.6主從複製(讀寫分離)教程

1、MySQL5.6開始主從複製有兩種方式:

基於日誌(binlog);

基於GTID(全域事務標示符)。

需要注意的是:GTID方式不支援暫存資料表!所以如果你的業務系統要用到暫存資料表的話就不要考慮這種方式了,至少目前最新版本MySQL5.6.12的GTID複製還是不支援暫存資料表的。

所以本教程主要是告訴大家如何通過日誌(binlog)方式做主從複製!

2、MySQL官方提供的MySQL Replication教程:

 http://dev.mysql.com/doc/refman/5.6/en/replication.html

第一步:準備工作

主伺服器: 192.168.1.100

從伺服器: 192.168.1.101

MySQL軟體版本:

MySQL-server-advanced-5.6.18-1.el6.x86_64.rpm

MySQL-cient-advanced-5.6.18-1.el6.x86_64.rpm

第二步:在主伺服器和從伺服器上安裝MySQL資料庫軟體

安裝方法,請參見 

RedHat6.5下MySQL5.6叢集配置完整版   


MySQL資料庫軟體安裝完成後,不要急著做mysql啟動操作。建議把mysql初始化產生的/usr/my.cnf

(如果是從源檔案編譯安裝時,路徑應該是在/usr/local/mysql/mysql.cnf)刪除,然後把最佳化好的mysql

設定檔my.cnf放到/etc下。 

第三步:修改主要資料庫的設定檔/usr/my.cnf 

[mysqld]

 

server-id=1

log-bin=mysqlmaster-bin.log

sync_binlog=1

 


innodb_buffer_pool_size=512M

innodb_flush_log_at_trx_commit=1

 


sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 


lower_case_table_names=1

log_bin_trust_function_creators=1

 


第四步:修改從資料庫設定檔/usr/my.cnf

 


server-id=2

log-bin=mysqlslave-bin.log

sync_binlog=1

innodb_buffer_pool_size=512M

innodb_flush_log_at_trx_commit=1

 


sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

lower_case_table_names=1

log_bin_trust_function_creators=1

 


第五步:在主要資料庫和從資料庫伺服器上分別執行以下命令重新啟動主要資料庫和從資料庫

[root@master ~]# service mysql restart

[root@slave ~]# service mysql restart

 


第六步:在主要資料庫上建立用於主從複製的賬戶

[root@master ~]# mysql -uroot -p

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.101' IDENTIFIED BY '111111';

Query OK, 0 rows affected (0.00 sec)

 


注意:以上命令中的IP地址,是從資料庫伺服器的IP地址。

 

--------------------------------------分割線 --------------------------------------

Ubuntu 14.04下安裝MySQL

《MySQL權威指南(原書第2版)》清晰中文掃描版 PDF

Ubuntu 14.04 LTS 安裝 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主從伺服器

Ubuntu 12.04 LTS 構建高可用分布式 MySQL 叢集

Ubuntu 12.04下原始碼安裝MySQL5.6以及Python-MySQLdb

--------------------------------------分割線 --------------------------------------


第七步:主要資料庫鎖表(禁止再插入資料以擷取主要資料庫的的二進位日誌座標)

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

 


第八步:查看主要資料庫的狀態(並記錄下File欄位和Position欄位的值,在配置從伺服器時有用到)

mysql> show master status;

+------------------------+----------+--------------+------------------+-------------------+

| File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------------+----------+--------------+------------------+-------------------+

| mysqlmaster-bin.000004 |      327 |              |                  |                  |

+------------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

第九步:建立主要資料庫的快照檔案

[root@master ~]# cd /usr/bin/

# ./mysqldump -uroot -p -h127.0.0.1 -P3306 --all-databases --triggers --routines --events >>/mnt/windows/all.sql

上面命令中的紅色部分,是一個共用目錄,這個目錄可以同時被主要資料庫伺服器和從資料庫伺服器訪問到。

如果沒有這樣的共用目錄,可以將all.sql放在其它任何目錄下,然後使用scp命令複製到遠程從資料庫伺服器的某個目錄中

這條命令的執行時間根據資料量的不同,會有所不同,如果主要資料庫的資料量很大,可能需要很長時間,那麼在這種情況下,就最好在晚上沒有業務的時候進行這個操作,否則第七步中的鎖表操作會對業務系統造成很大的影響

第十步:解鎖主要資料庫的鎖表操作

[root@master ~]# mysql -uroot -p    (本命令在主要資料庫伺服器上執行)

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

第十一步:在從資料庫伺服器上匯入第七步建立的快照檔案到從資料庫中

[root@slave ~]# mysql -uroot -p -h127.0.0.1 -P3306 < /mnt/windows/all.sql

第十二步:在從資料庫伺服器上設定主要資料庫伺服器向從資料庫伺服器同步

[root@slave ~]# mysql -uroot -p

mysql> change master to master_host = '192.168.1.100',master_user='repl',master_password='111111',master_log_file='mysqlmaster-bin.000004',master_log_pos=327;

注意:紅色部分的值,是在第八步中查出來的,這裡不能弄錯了

第十三步:啟動從資料庫複寫線程

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

第十四步:查詢從資料庫的複製線程狀態

mysql> show slave status \G

*************************** 1. row ***************************

              Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.100

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysqlmaster-bin.000004

          Read_Master_Log_Pos: 327

              Relay_Log_File: slave-relay-bin.000002

                Relay_Log_Pos: 289

        Relay_Master_Log_File: mysqlmaster-bin.000004

          Slave_IO_Running: Yes

        Slave_SQL_Running: 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: 327

              Relay_Log_Space: 462

              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:

  Replicate_Ignore_Server_Ids:

            Master_Server_Id: 1

                  Master_UUID: 2e5e1b22-f0a9-11e3-bbac-000c297799e0

            Master_Info_File: /var/lib/mysql/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

          Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

    Last_SQL_Error_Timestamp:

              Master_SSL_Crl:

          Master_SSL_Crlpath:

          Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

1 row in set (0.00 sec)

 

如果Slave_IO_Running和Slave_SQL_Running兩項都為yes,就表示主從複製配置成功了.

下面可以開始測試組態是否成功了,首先在主要資料庫的test資料庫中建立一張表,然後插入幾條資料,然後到從資料庫看看是否同步過來了。

注意:當從資料庫有大量的查詢時,可以暫時將從資料庫的複製線程關閉掉,等查詢量降下來了,再開啟,這樣也不會遺失資料。

更多詳情見請繼續閱讀下一頁的精彩內容:

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.