MySQL主從複製

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   for   strong   檔案   資料   

MySQL主從複製(Master-Slave)與讀寫分離(MySQL-Proxy)實踐

Mysql作為目前世界上使用最廣泛的免費資料庫,相信所有從事系統營運的工程師都一定接觸過。但在實際的生產環境中,由單台Mysql作為獨立的資料庫是完全不能滿足實際需求的,無論是在安全性,高可用性以及高並發等各個方面。

因此,一般來說都是通過 主從複製(Master-Slave)的方式來同步資料,再通過讀寫分離(MySQL-Proxy)來提升資料庫的並發負載能力 這樣的方案來進行部署與實施的。

如所示:

下面是我在實際工作過程中所整理的筆記,在此分享出來,以供大家參考。

一、MySQL的安裝與配置
具體的安裝過程,建議參考我的這一篇文章:http://heylinux.com/archives/993.html
值得一提的是,我的安裝過程都是源碼包編譯安裝的,並且所有的配置與資料等都統一規划到了/opt/mysql目錄中,因此在一台伺服器上安裝完成以後,可以將整個mysql目錄打包,然後傳到其它伺服器上解包,便可立即使用。

二、MySQL主從複製
情境描述:
主要資料庫伺服器:192.168.10.130,MySQL已經安裝,並且無應用資料。
從資料庫伺服器:192.168.10.131,MySQL已經安裝,並且無應用資料。

2.1 主伺服器上進行的操作
啟動mysql服務
/opt/mysql/init.d/mysql start

通過命令列登入管理MySQL伺服器
/opt/mysql/bin/mysql -uroot -p‘new-password‘

授權給從資料庫伺服器192.168.10.131
mysql> GRANT REPLICATION SLAVE ON *.* to ‘rep1‘@‘192.168.10.131‘ identified by ‘password’;

查詢主要資料庫狀態
Mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 261 | | |
+------------------+----------+--------------+------------------+

記錄下 FILE 及 Position 的值,在後面進行從伺服器操作的時候需要用到。

2.2 配置從伺服器
修改從伺服器的設定檔/opt/mysql/etc/my.cnf
將 server-id = 1修改為 server-id = 10,並確保這個ID沒有被別的MySQL服務所使用。

啟動mysql服務
/opt/mysql/init.d/mysql start

通過命令列登入管理MySQL伺服器
/opt/mysql/bin/mysql -uroot -p‘new-password‘

執行同步SQL語句
mysql> change master to
master_host=’192.168.10.130’,
master_user=’rep1’,
master_password=’password’,
master_log_file=’mysql-bin.000005’,
master_log_pos=261;

正確執行後啟動Slave同步進程
mysql> start slave;

主從同步檢查
mysql> show slave status\G
==============================================
**************** 1. row *******************
Slave_IO_State:
Master_Host: 192.168.10.130
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 415
Relay_Log_File: localhost-relay-bin.000008
Relay_Log_Pos: 561
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: YES
Slave_SQL_Running: YES
Replicate_Do_DB:
……………省略若干……………
Master_Server_Id: 1
1 row in set (0.01 sec)
==============================================

其中Slave_IO_Running 與 Slave_SQL_Running 的值都必須為YES,才表明狀態正常。

如果主伺服器已經存在應用資料,則在進行主從複製時,需要做以下處理:
(1)主要資料庫進行鎖表操作,不讓資料再進行寫入動作
mysql> FLUSH TABLES WITH READ LOCK;

(2)查看主要資料庫狀態
mysql> show master status;

(3)記錄下 FILE 及 Position 的值。
將主伺服器的資料檔案(整個/opt/mysql/data目錄)複製到從伺服器,建議通過tar歸檔壓縮後再傳到從伺服器解壓。

(4)取消主要資料庫鎖定
mysql> UNLOCK TABLES;

2.3 驗證主從複製效果

主伺服器上的操作
在主伺服器上建立資料庫first_db
mysql> create database first_db;
Query Ok, 1 row affected (0.01 sec)

在主伺服器上建立表first_tb
mysql> create table first_tb(id int(3),name char(10));
Query Ok, 1 row affected (0.00 sec)

在主伺服器上的表first_tb中插入記錄
mysql> insert into first_tb values (001,’myself’);
Query Ok, 1 row affected (0.00 sec)

在從伺服器上查看
mysql> show databases;
=============================
+--------------------+
| Database |
+--------------------+
| information_schema |
| first_db |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
=============================
資料庫first_db已經自動產生

mysql> use first_db
Database chaged

mysql> show tables;
=============================
+--------------------+
| Tables_in_first_db |
+--------------------+
| first_tb |
+--------------------+
1 row in set (0.02 sec)
=============================
資料庫表first_tb也已經自動建立

mysql> select * from first_tb;
=============================
+------+------+
| id | name |
+------+------+
| 1 | myself |
+------+------+
1 rows in set (0.00 sec)
=============================
記錄也已經存在

由此,整個MySQL主從複製的過程就完成了,接下來,我們進行MySQL讀寫分離的安裝與配置。

MySQL主從複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.