[轉自:linuxme.blog.51cto.com/1850814/383742]
1 . ? 主從 mysql server 的工作原理:(如圖及其過程分析)
過程:
??
Mysql
的復制(
replication
)是一個異步的復制,從一個
Mysql instace
(稱之為
Master
)復制到另一個
Mysql instance
(稱之
Slave
)。實現整個復制操作主要由三個進程完成的,其中兩個進程在
Slave
(
Sql
進程和
IO
進程),另外一個進程在
Master
(
IO
進程)上。
??
要實施復制,首先必須打開
Master
端的
binary log
(
bin-log
)功能,否則無法實現。因為整個復制過程實際上就是
Slave
從
Master
端獲取該日志然后再在自己身上完全順序的執行日志中所記錄的各種操作。
復制的基本過程如下:
(
1
)
Slave
上面的
IO
進程連接上
Master
,并請求從指定日志文件的指定位置(或者從最開始的日志)之后的日志內容;
(
2
)
Master
接收到來自
Slave
的
IO
進程的請求后,通過負責復制的
IO
進程根據請求信息讀取制定日志指定位置之后的日志信息,返回給
Slave
的
IO
進程。返回信息中除了日志所包含的信息之外,還包括本次返回的信息已經到
Master
端的
bin-log
文件的名稱以及
bin-log
的位置;
(
3
)
Slave
的
IO
進程接收到信息后,將接收到的日志內容依次添加到
Slave
端的
relay-log
文件的最末端,并將讀取到的
Master
端的
bin-log
的文件名和位置記錄到
master-info
文件中,以便在下一次讀取的時候能夠清楚的高速
Master
“我需要從某個
bin-log
的哪個位置開始往后的日志內容,請發給我”;
(
4
)
Slave
的
Sql
進程檢測到
relay-log
中新增加了內容后,會馬上解析
relay-log
的內容成為在
Master
端真實執行時候的那些可執行的內容,并在自身執行。
好了,了解了其原理后就讓我們來安裝 mysql 及配置主從 mysql 服務器吧
為了使用方便,和使 mysql 的功能更優一點我們使用二進制包安裝,下載地址(需要注冊,免費): http://www.mysql.com/downloads/mysql/
?
2 . 二進制安裝 mysql (過程不做詳細解釋):
# 解壓包及做鏈接
tar xvf mysql-5.1.50 -linux-i686-glibc23.tar.gz /usr/local
cd /usr/local
?
ln -sv mysql-5.1.50 -linux-i686-glibc23.tar.gz mysql
cd mysql
?
# 增加用戶及該權限( -r : 加一系統用戶)
groupadd mysql
useradd -g mysql -s /sbin/nologin -M -r mysql ??
?
mkdir /mysql/data
chown -R mysql.mysql /mysql/data
cd /usr/local/mysql
chown mysql:mysql . -R
?
# 初始化 mysql 配置
scripts/mysql_install_db --user=mysql --datadir=/mysql/data
?
chown root . -R
chown mysql data/ -R
?
cp support-files/my-large.cnf /etc/my.cnf
vim /etc/my.cnf
datadir = /mysql/data ??? # 加入這一行
?
# 啟動 mysql
bin/mysqld_safe --user=mysql &
netstat -nutlp | grep 3306
?
# 使其可以使用 mysql 命令
vim /etc/profile
#add
PATH=$PATH:/usr/local/mysql/bin
?
. /etc/profile ?? # 重讀配置文件
?
?
# 加載庫函數
vim /etc/ld.so.conf.d/mysql.conf
#add
/usr/local/mysql/lib
ldconfig -v
ln -sv /usr/local/mysql/include /usr/include/mysql
ls /usr/include/mysql/
?
# 把 mysql 加入開機啟動
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld restart
?
3 . ??? ? Mysql 裝好了我們就來實現 master 和 slave mysql server 架構
?
主:192.168.0.192? station192.example.com
從:192.168.0.193? station193.example.com
?
? Master 端的配置:
vim /etc/my.cnf
# 確保有一下兩行 , 并開啟
log_bin = mysql-bin
server_id = 24
?
授權可以來讀取日志文件的用戶:
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENTON *.*
-> TO tom@'192.168.0.%' IDENTIFIED BY 'password';
?
查看一下主 mysql 的狀態(結果能不一樣,已使用情況而定)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File ???????????? | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 108 ????? | ????????????? | ??????????? ?????? |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
?
從( slave )端的設置:
vim /etc/my.cnf
# 確保有一下幾行
log_bin = mysql-bin
server_id = 2
relay_log = mysql-relay-bin
log_slave_updates = 1
read_only = 1
?
定義怎樣連接 master mysql
mysql> CHANGE MASTER TO MASTER_HOST='station192.example.com',
-> MASTER_USER='tom',
-> MASTER_PASSWORD='password',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=0;
? 4. 查看狀態
# 查看從服務器的狀態:
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: station192.example.com
Master_User: tom
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 5
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 5
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
……………… .
mysql> START SLAVE;
注意:這個命令的不能有錯誤產生
?
# 再次查看狀態
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: station192.example.com
Master_User: tom
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 175
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 175
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
………………………
?5. 查看進程
查看 Master ( IO 進程):
mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
Id: 24
User: tom
Host: station193.example.com:54831
db: NULL
Command: Binlog Dump
Time: 610237
State: Has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
?
查看 Slave ( Sql 進程和 IO 進程):
mysql> SHOW PROCESSLIST\G
*************************** 1. row ***************************
Id: 12
User: system user
Host:
db: NULL
Command: Connect
Time: 611116
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 13
User: system user
Host:
db: NULL
Command: Connect
Time: 33
State: Has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
注意 1.row 是 I/O 進程 , 2.row 是 sql 進程,已經空閑 33 秒
好了到此簡單主從 MySQL Replication 就已經配置完成了,你學會了嗎???
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
