[轉(zhuǎn)自:linuxme.blog.51cto.com/1850814/383742]
1 . ? 主從 mysql server 的工作原理:(如圖及其過程分析)
過程:
??
Mysql
的復(fù)制(
replication
)是一個(gè)異步的復(fù)制,從一個(gè)
Mysql instace
(稱之為
Master
)復(fù)制到另一個(gè)
Mysql instance
(稱之
Slave
)。實(shí)現(xiàn)整個(gè)復(fù)制操作主要由三個(gè)進(jìn)程完成的,其中兩個(gè)進(jìn)程在
Slave
(
Sql
進(jìn)程和
IO
進(jìn)程),另外一個(gè)進(jìn)程在
Master
(
IO
進(jìn)程)上。
??
要實(shí)施復(fù)制,首先必須打開
Master
端的
binary log
(
bin-log
)功能,否則無法實(shí)現(xiàn)。因?yàn)檎麄€(gè)復(fù)制過程實(shí)際上就是
Slave
從
Master
端獲取該日志然后再在自己身上完全順序的執(zhí)行日志中所記錄的各種操作。
復(fù)制的基本過程如下:
(
1
)
Slave
上面的
IO
進(jìn)程連接上
Master
,并請(qǐng)求從指定日志文件的指定位置(或者從最開始的日志)之后的日志內(nèi)容;
(
2
)
Master
接收到來自
Slave
的
IO
進(jìn)程的請(qǐng)求后,通過負(fù)責(zé)復(fù)制的
IO
進(jìn)程根據(jù)請(qǐng)求信息讀取制定日志指定位置之后的日志信息,返回給
Slave
的
IO
進(jìn)程。返回信息中除了日志所包含的信息之外,還包括本次返回的信息已經(jīng)到
Master
端的
bin-log
文件的名稱以及
bin-log
的位置;
(
3
)
Slave
的
IO
進(jìn)程接收到信息后,將接收到的日志內(nèi)容依次添加到
Slave
端的
relay-log
文件的最末端,并將讀取到的
Master
端的
bin-log
的文件名和位置記錄到
master-info
文件中,以便在下一次讀取的時(shí)候能夠清楚的高速
Master
“我需要從某個(gè)
bin-log
的哪個(gè)位置開始往后的日志內(nèi)容,請(qǐng)發(fā)給我”;
(
4
)
Slave
的
Sql
進(jìn)程檢測(cè)到
relay-log
中新增加了內(nèi)容后,會(huì)馬上解析
relay-log
的內(nèi)容成為在
Master
端真實(shí)執(zhí)行時(shí)候的那些可執(zhí)行的內(nèi)容,并在自身執(zhí)行。
好了,了解了其原理后就讓我們來安裝 mysql 及配置主從 mysql 服務(wù)器吧
為了使用方便,和使 mysql 的功能更優(yōu)一點(diǎn)我們使用二進(jìn)制包安裝,下載地址(需要注冊(cè),免費(fèi)): http://www.mysql.com/downloads/mysql/
?
2 . 二進(jìn)制安裝 mysql (過程不做詳細(xì)解釋):
# 解壓包及做鏈接
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
?
# 增加用戶及該權(quán)限( -r : 加一系統(tǒng)用戶)
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 ??? # 加入這一行
?
# 啟動(dòng) mysql
bin/mysqld_safe --user=mysql &
netstat -nutlp | grep 3306
?
# 使其可以使用 mysql 命令
vim /etc/profile
#add
PATH=$PATH:/usr/local/mysql/bin
?
. /etc/profile ?? # 重讀配置文件
?
?
# 加載庫(kù)函數(shù)
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 加入開機(jī)啟動(dòng)
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld restart
?
3 . ??? ? Mysql 裝好了我們就來實(shí)現(xiàn) master 和 slave mysql server 架構(gòu)
?
主: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
?
授權(quán)可以來讀取日志文件的用戶:
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENTON *.*
-> TO tom@'192.168.0.%' IDENTIFIED BY 'password';
?
查看一下主 mysql 的狀態(tài)(結(jié)果能不一樣,已使用情況而定)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File ???????????? | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 108 ????? | ????????????? | ??????????? ?????? |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
?
從( slave )端的設(shè)置:
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. 查看狀態(tài)
# 查看從服務(wù)器的狀態(tài):
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;
注意:這個(gè)命令的不能有錯(cuò)誤產(chǎn)生
?
# 再次查看狀態(tài)
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. 查看進(jìn)程
查看 Master ( IO 進(jìn)程):
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 進(jìn)程和 IO 進(jìn)程):
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 進(jìn)程 , 2.row 是 sql 進(jìn)程,已經(jīng)空閑 33 秒
好了到此簡(jiǎn)單主從 MySQL Replication 就已經(jīng)配置完成了,你學(xué)會(huì)了嗎???
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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