詳解MySQL+Apache集群模式的實(shí)現(xiàn)
環(huán)境:RedHat AS3 U5
說明:如果不做說明,一般安裝命令均使用ROOT權(quán)限,用 # 表示;一般命令用$表示。
特別說明:無
以下文章介紹Liunx 環(huán)境下的MySQL+Apache安全備份模式的實(shí)現(xiàn)方法
一、前言
目前,Linux系統(tǒng)+MySQL數(shù)據(jù)庫已成為眾多中小企業(yè)及個人搭建網(wǎng)站環(huán)境的首選,因?yàn)樗峭耆赓M(fèi)的,而且性能相當(dāng)不錯。不過我見過的很多網(wǎng)站都是在單機(jī)上搭建此環(huán)境,然后購買價格相對較貴的備份產(chǎn)品及設(shè)備,其實(shí)如果我們熟悉一下apache和MySQL,把購買備份產(chǎn)品及設(shè)備的錢省下再買幾臺服務(wù)器的話,完全可以解決問題。(服務(wù)器只是個代詞,也可以根據(jù)你網(wǎng)站的規(guī)模選購性能好一點(diǎn)的PC機(jī),都是沒有問題的)
二、MySQL+Apache安全備份模式圖
見圖1
從上圖我們可以看出,用戶通過訪問http LVS服務(wù)器,獲取Web服務(wù),其實(shí)這個LVS服務(wù)器是個虛擬機(jī),真正的Web服務(wù)是由這個虛擬機(jī)底下的兩個http服務(wù)器提供的,這樣,如果這兩個http服務(wù)器有一個有問題,那么可以改變Lvs模式,只讓好的那臺提供服務(wù),為我們恢復(fù)故障節(jié)省了很多時間;而MySQL數(shù)據(jù)庫采用Master及Slave模式,避免由于單點(diǎn)故障而全盤皆輸?shù)木狡染置妫ㄗh訪問量較大的網(wǎng)站采用全服務(wù)器配置或至少LVS及MySQL服務(wù)器采用全服務(wù)器配置,而訪問量屬于中等偏下網(wǎng)站全部采用PC機(jī)都是沒有問題的。LVS服務(wù)器使用雙網(wǎng)卡(eth0和eth1)。eth0為 虛擬服務(wù)器的IP地址(公網(wǎng)IP),eth1為內(nèi)網(wǎng)IP地址,其他均為內(nèi)網(wǎng)地址,這樣可以更安全。


我們假定MySQL及http服務(wù)器均已安裝完成,并且在http服務(wù)器上跑的站點(diǎn)可以調(diào)用MySQL數(shù)據(jù)庫,MySQL主數(shù)據(jù)庫(Master)為192.168.1.4,從數(shù)據(jù)庫(slave)為192.168.1.5,站點(diǎn)調(diào)用的數(shù)據(jù)庫名稱為hello。如果你不知道怎么安裝apache及MySQL,請參閱我寫的《MySQL環(huán)境下安裝PHP504及apache2054》
三、實(shí)現(xiàn)方法
(1)下載RedHat_Cluster_Suite.tgz軟件包,地址如下:
http://www.d-1701.com/tools/RedHat_Cluster_Suite.tgz
這個其實(shí)是redhat AS3自帶的拓展服務(wù)包,用RPM安裝也可以,但我們一般都只有前四張,后七張很少有人有,那我就把我手頭上這個安裝包給大家共享一下 :)
(2)開啟系統(tǒng)vnc 服務(wù)并安裝
- # vncserver
連入linux系統(tǒng),開一個終端
- # cd /home/xuchen
- # tar xzvf RedHat_Cluster_Suite.tgz
- # cd RedHat_Cluster_Suite
- # ./autorun
這將開啟安裝界面,沒什么好說,直接安裝就是了。
(3)配置前的準(zhǔn)備
修改集群服務(wù)器的IP轉(zhuǎn)發(fā)設(shè)置
- # /sbin/sysctl -w net.ipv4.ip_forward=1
修改當(dāng)前會話的值
- # vi /etc/sysctl.conf
- net.ipv4.ip_forward = 1
這樣下次重啟時設(shè)置可以自動生效
系統(tǒng)安裝上Red hat cluster suite 集群軟件后,使用一個名為Piranha的配置工具配置集群服務(wù)器,此配置工具使用 apache 作web Server 來提供基于web頁面的配置接口。此配置接口的web頁面站點(diǎn)的配置文件網(wǎng)頁文件及其他相關(guān)文件存放在/etc/sysconfig/ha/ 目錄中。
- # vi /etc/sysconfig/ha/conf/httpd.conf
配置接口頁面站點(diǎn)的相關(guān)參數(shù)。缺省的監(jiān)聽端口是3636,你可以改成其他的端口
Listen 3636
訪問Piranha配置接口頁面需要有帳號和密碼,
帳號默認(rèn)為piranha,用以下命令配置密碼
- # /usr/sbin/piranha-passwd //設(shè)置 piranha 的登錄口令。
- # /sbin/service piranha-gui start //啟動Piranha配置接口頁面站點(diǎn)
訪問http://Your’s IP:3636/ 登錄 Piranha。利用此頁面直接配置接口配置集群服務(wù)。
配置之前要明白的幾個概念:
虛擬服務(wù)器:用戶訪問http服務(wù)器(LVS服務(wù)器IP),感覺上就是訪問一臺高效的服務(wù)器。此臺“高效的服務(wù)器”,我們稱之為“虛擬服務(wù)器”,實(shí)際上就是這里的集群服務(wù)器。
真實(shí)服務(wù)器:即真實(shí)提供服務(wù)的服務(wù)器,此類服務(wù)器可以有多臺,而且可以增加。
(4)配置LVS
選擇GLOBAL SETTINGS 選項(xiàng)卡。進(jìn)行全局設(shè)置。
Primary server public IP: 211.100.1.1 集群服務(wù)器的外網(wǎng)ip
Primary server private IP::為空
本例中采用LVS NAT模式的集群,以下是集群的不同模式區(qū)別:
1. Virtual Server via Network Address Translation(VS/NAT)
通過網(wǎng)絡(luò)地址轉(zhuǎn)換,調(diào)度器重寫請求報(bào)文的目標(biāo)地址,根據(jù)預(yù)設(shè)的調(diào)度算法,將請求分派給后端的真實(shí)服務(wù)器;真實(shí)服務(wù)器的響應(yīng)報(bào)文通過調(diào)度器時,報(bào)文的源地址被重寫,再返回給客戶,完成整個負(fù)載調(diào)度過程。
2. Virtual Server via IP Tunneling(VS/TUN)
采用NAT技術(shù)時,由于請求和響應(yīng)報(bào)文都必須經(jīng)過調(diào)度器地址重寫,當(dāng)客戶請求越來越多時,調(diào)度器的處理能力將成為瓶頸。為了解決這個問題,調(diào)度器把請求報(bào)文通過IP隧道轉(zhuǎn)發(fā)至真實(shí)服務(wù)器,而真實(shí)服務(wù)器將響應(yīng)直接返回給客戶,所以調(diào)度器只處理請求報(bào)文。由于一般網(wǎng)絡(luò)服務(wù)應(yīng)答比請求報(bào)文大許多,采用VS/TUN技術(shù)后,集群系統(tǒng)的最大吞吐量可以提高10倍。
3. Virtual Server via Direct Routing(VS/DR)
VS/DR通過改寫請求報(bào)文的MAC地址,將請求發(fā)送到真實(shí)服務(wù)器,而真實(shí)服務(wù)器將響應(yīng)直接返回給客戶。同VS/TUN技術(shù)一樣,VS/DR技術(shù)可極大地提高集群系統(tǒng)的伸縮性。這種方法沒有IP隧道的開銷,對集群中的真實(shí)服務(wù)器也沒有必須支持IP隧道協(xié)議的要求,但是要求調(diào)度器與真實(shí)服務(wù)器都有一塊網(wǎng)卡連在同一物理網(wǎng)段上。
點(diǎn)擊NAT按鈕,出現(xiàn)NAT網(wǎng)絡(luò)設(shè)置:
NAT Router IP:192.168.1.2 NAT路徑名稱
NAT Router netmask:255.255.255.0 NAT掩碼
NAT Router device:eth1 NAT 路徑裝置
選擇VIRTUAL SERVERS 選項(xiàng)卡。
點(diǎn)擊ADD按鈕,添加虛擬服務(wù)器:
點(diǎn)擊EDIT按鈕,配置該虛擬服務(wù)器:
Name:http 此虛擬服務(wù)器的名,可以隨意,建議以提供的服務(wù)命名。
Application port:80 應(yīng)用監(jiān)聽端口。Web服務(wù)為80端口
Protocol:tcp 采用tcp協(xié)議通信
Virtual IP Address:211.100.1.1 此虛擬服務(wù)器的有效外網(wǎng)ip地址,供用戶訪問
Virtual IP Network Mask:255.255.255.0 此虛擬服務(wù)器的子網(wǎng)掩碼,請查看網(wǎng)卡設(shè)置。
Firewall Mask:(空)
Device:eth0 外網(wǎng)通信網(wǎng)卡
Re-entry timeout:15 重入時間(S)
Service timeoute:6 服務(wù)延時(S)
Quiesce server:NO
Load monitoring tool:none 不加載監(jiān)控工具
Scheduling:Weighted least-connections 調(diào)度策略(權(quán)重最小連接)
Persistence:1440 持續(xù)性(S)
Persistence Network Mask:255.255.255.0 持續(xù)性屏蔽
配置完畢,點(diǎn)擊ACCEPT將配置寫入配置文檔 /etc/sysconfig/ha/lvs.cf
如果想要讓同一個IP在一段時間內(nèi)只訪問同一個真實(shí)服務(wù)器,就要設(shè)置Persistence 值,在這指定時間內(nèi),無論采用何種負(fù)載均衡算法,同一個IP都會訪問同一個真實(shí)服務(wù)器。
虛擬服務(wù)器配置完后,為此虛擬服務(wù)器添加真實(shí)服務(wù)器。
點(diǎn)擊子選項(xiàng)卡REAL SERVER:
點(diǎn)擊ADD按鈕添加真實(shí)服務(wù)器:
點(diǎn)擊EDIT按鈕配置此真實(shí)服務(wù)器:
Weight 為該真實(shí)服務(wù)器的權(quán)重。虛擬服務(wù)器按調(diào)度策略根據(jù)此設(shè)置值進(jìn)行調(diào)度。
配置完點(diǎn)擊ACCEPT按鈕將配置寫入配置文檔 /etc/sysconfig/ha/lvs.cf 。
重新點(diǎn)擊子選項(xiàng)卡REAL SERVER可添加多個真實(shí)服務(wù)器。
點(diǎn)擊(DE)ACTIVATE按鈕可使真實(shí)服務(wù)器生效(UP)。不過建議在完成所有配置后再啟動。
點(diǎn)擊子選項(xiàng)卡MONITORING SCRIPTS :
配置發(fā)送和接收字符串,讓lvs確定real server是否存在。因?yàn)楫?dāng)前l(fā)vs模式有弊端,確定包文在網(wǎng)絡(luò)流量大的時候,會丟包,從而造成real server掉線的假象,所以現(xiàn)在采取腳本send.sh發(fā)送和確認(rèn),保證每次都能返回確認(rèn)字符串。
/root/send.sh 的內(nèi)容很簡單:
- #!/bin/sh
- echo "http"
echo 的內(nèi)容就是期待的內(nèi)容。
至此,已經(jīng)為虛擬服務(wù)器添加完畢真實(shí)服務(wù)器。啟動真實(shí)服務(wù)器的apache服務(wù)。進(jìn)入LVS配置的相關(guān)界面,點(diǎn)擊(DE)ACTIVATE按鈕使設(shè)備UP起來。
以上所有的配置結(jié)果都ACCEPT后,將寫入配置文件/etc/sysconfig/ha/lvs.cf 中。如果你熟悉了LVS 的配置,可以直接修改此配置文件,而不需要使用piranha配置工具。
- # /sbin/service piranha-gui stop //關(guān)閉配置界面,增強(qiáng)安全性
- # /sbin/service pulse start //啟動lvs服務(wù),此時進(jìn)程列表里應(yīng)含用 pulse 和 lvs 的進(jìn)程名。
- # ipvsadm //查看當(dāng)前的路由表
- IP Virtual Server version 1.0.8 (size=65536)
- Prot LocalAddressort Scheduler Flags
- ->; RemoteAddressort Forward Weight ActiveConn InActConn
- TCP 211.100.1.1:http wlc persistent 1440 mask 255.255.255.0
- ->; 192.168.1.2:http Masq 100 128 24929
- ->; 192.168.1.3:http Masq 100 137 21351
如果上述的192.168.1.2:http真實(shí)服務(wù)器顯示的是主機(jī)名而不是ip地址,為了加快路由轉(zhuǎn)發(fā),把它們的主機(jī)名對應(yīng)的ip解析加在了負(fù)載均衡器的/etc/hosts中。最好把每一個真實(shí)服務(wù)器的域名解析都放到此文件中。
當(dāng)然你要把所有機(jī)器的apache都起來,如果有一個沒有起來,上面的列表中就會少一行。Lvs每隔一段時間會檢測一下真實(shí)服務(wù)器(即上述的發(fā)包監(jiān)控)。
注意?。。?/p>
如果修改了/etc/sysconfig/ha/lvs.cf(無論是手工編輯還是通過Piranha)只有重新啟動pulse服務(wù)才會起作用。
實(shí)踐操作中,pulse服務(wù)起來之后如果被停止掉,它會把外網(wǎng)和內(nèi)網(wǎng)的兩個網(wǎng)卡全部down掉,只好通過控制臺再啟動兩個網(wǎng)卡和pulse服務(wù)。
所以在遠(yuǎn)程控制lvs集群負(fù)載機(jī)的時候,如果要重新啟動pulse,應(yīng)該直接reboot機(jī)器,以防止網(wǎng)卡down掉,無法訪問遠(yuǎn)程機(jī)器。
改動當(dāng)前的配置,還可以使用 ipvsadm直接修改內(nèi)核路由表,相關(guān)操作命令請查閱ipvsadm的命令行幫助。
例如:假設(shè)當(dāng)前已經(jīng)有了websvr1(192.168.1.2)和websvr2(192.168.1.3)兩個真實(shí)服務(wù)器,擴(kuò)容需要新增另外一臺websvr3(192.168.1.6)真實(shí)服務(wù)器。直接在/etc/sysconfig/ha/lvs.cf 中加入了websvr3的信息,但是無法立即生效,這時可以用以下命令:
ipvsadm -a -t 211.100.1.1:http -r 192.168.1.3:http -m -w 100
這樣就可以加入到內(nèi)核路由表并立即生效。
以上就是一個典型的集群服務(wù)器的安裝和配置過程,大家可以參考配置自己的集群服務(wù)器。
(5)配置MySQL數(shù)據(jù)庫的Master-Slave 模式
MySQL數(shù)據(jù)庫的Master-Slave機(jī)制
MySQL數(shù)據(jù)庫支持Master-Slave主從數(shù)據(jù)庫的集群模式。Slave (從)數(shù)據(jù)庫根據(jù)Master(主)數(shù)據(jù)庫的日志文件(master_log_file)及日志位置(master_log_pos)實(shí)時從Master數(shù)據(jù)庫讀取數(shù)據(jù),自動實(shí)現(xiàn)數(shù)據(jù)同步。
MySQL數(shù)據(jù)庫Master-Slave實(shí)現(xiàn)
①Slave數(shù)據(jù)庫服務(wù)器要擁有訪問Master資源的權(quán)限,因此首先要在Master上為Slave創(chuàng)建訪問帳號并賦予文檔復(fù)制權(quán)限。
- #MySQL -h192.168.1.4 –uroot –p –D hello
- MySQL>;GRANT REPLICATION SLAVE ON *.* TO 'testbak'@'%' IDENTIFIED BY 'testbak';
②根據(jù)MySQL數(shù)據(jù)庫的Master-Slave機(jī)制,Slave數(shù)據(jù)庫實(shí)施數(shù)據(jù)同步操作的依據(jù)是Master數(shù)據(jù)庫的日志文件及該日志文件的日志位置。因此,在為Master新增一臺Slave服務(wù)器的時候要確保為Slave配置的參數(shù)(master_log_file及master_log_pos)就是Master當(dāng)前使用的日志文件及當(dāng)前的操作節(jié)點(diǎn)。如果Master數(shù)據(jù)庫的數(shù)據(jù)一直處于運(yùn)營狀態(tài)(即有插入、修改、刪除等操作),將不能滿足上述要求。所以目前的維護(hù)操作是:系統(tǒng)擴(kuò)容為Master增加一臺Slave的時候,要停止整個業(yè)務(wù)系統(tǒng),以停止對Master數(shù)據(jù)庫數(shù)據(jù)的讀寫操作,然后查看Master當(dāng)前的日志文件及日志位置。
- #MySQL -h192.168.1.4 –uroot –p –D hello
- MySQL>; show master status;
有個改進(jìn)的操作,可以將Master數(shù)據(jù)庫鎖住為只讀,防止數(shù)據(jù)的更新。此時不用停止業(yè)務(wù)系統(tǒng),但用戶將不能進(jìn)行信息修改、注冊等操作。(待測試驗(yàn)證)
③將與當(dāng)前Master數(shù)據(jù)庫master_log_file、master_log_pos的值匹配的數(shù)據(jù)從Master拷貝到Slave上??梢圆捎肕ySQLdump方法,也可以直接拷貝數(shù)據(jù)文件。注意,拷貝數(shù)據(jù)之前要用
- # MySQLadmin –u root –p shutdown
關(guān)閉主數(shù)據(jù)庫。這里我們直接把數(shù)據(jù)文件壓縮為MySQL.tar.gz
④修改Master數(shù)據(jù)庫的配置文件
一般在/etc/my.cnf
以192.168.1.4 Master數(shù)據(jù)庫的配置文件為例:
修改/etc/my.cnf ,在[MySQLd]區(qū)段內(nèi)加入?yún)?shù)
- # vi /etc/my.cnf
- log-bin
- server-id=1
- sql-bin-update-same
- binlog-do-db=hello
- ******************************************************************************
- # Example MySQL config file for very large systems.
- #
- # This is for large system with memory of 1G-2G where the system runs mainly
- # MySQL.
- #
- # You can copy this file to
- # /etc/my.cnf to set global options,
- # MySQL-data-dir/my.cnf to set server-specific options (in this
- # installation this directory is /usr/local/MySQL/var) or
- # ~/.my.cnf to set user-specific options.
- #
- # One can in this file use all long options that the program supports.
- # If you want to know which options a program support, run the program
- # with --help option.
- # The following options will be passed to all MySQL clients
- [client]
- #password = your_password
- port = 3306
- socket = /tmp/MySQL.sock
- # Here follows entries for some specific programs
- # The MySQL server
- [MySQLd]
- port = 3306
- socket = /tmp/MySQL.sock
- skip-locking
- set-variable = key_buffer=384M
- set-variable = max_allowed_packet=1M
- set-variable = table_cache=512
- set-variable = sort_buffer=2M
- set-variable = record_buffer=2M
- set-variable = thread_cache=8
- # Try number of CPU's*2 for thread_concurrency
- set-variable = thread_concurrency=8
- set-variable = myisam_sort_buffer_size=64M
- log-bin
- server-id = 1
- sql-bin-update-same
- binlog-do-db=hello
- # Replication Master Server (default)
- #log-bin # required for replication
- #server-id = 1 # required unique id between 1 and 2^32 - 1
- # defaults to 1 if master-host is not set
- # but will not function as a master if omitted
- # Replication Slave Server (comment out master section to use this)
- #master-host = # MUST BE SET
- #master-user = # MUST BE SET
- #master-password = # MUST BE SET
- #master-port = # optional--defaults to 3306
- #log-bin # not required for slaves, but recommended
- #server-id = 2 # required unique id between 2 and 2^32 - 1
- # (and different from the master)
- # defaults to 2 if master-host is set
- # but will not function as a slave if omitted
- # Point the following paths to different dedicated disks
- #tmpdir = /tmp/
- #log-update = /path-to-dedicated-directory/hostname
- # Uncomment the following if you are using BDB tables
- #set-variable = bdb_cache_size=384M
- #set-variable = bdb_max_lock=100000
- # Uncomment the following if you are using InnoDB tables
- #innodb_data_home_dir = /usr/local/MySQL/var/
- #innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
- #innodb_log_group_home_dir = /usr/local/MySQL/var/
- #innodb_log_arch_dir = /usr/local/MySQL/var/
- # You can set .._buffer_pool_size up to 50 - 80 %
- # of RAM but beware of setting memory usage too high
- #set-variable = innodb_buffer_pool_size=384M
- #set-variable = innodb_additional_mem_pool_size=20M
- # Set .._log_file_size to 25 % of buffer pool size
- #set-variable = innodb_log_file_size=100M
- #set-variable = innodb_log_buffer_size=8M
- #innodb_flush_log_at_trx_commit=1
- #set-variable = innodb_lock_wait_timeout=50
- [MySQLdump]
- quick
- set-variable = max_allowed_packet=16M
- [MySQL]
- no-auto-rehash
- # Remove the next comment character if you are not familiar with SQL
- #safe-updates
- [isamchk]
- set-variable = key_buffer=256M
- set-variable = sort_buffer=256M
- set-variable = read_buffer=2M
- set-variable = write_buffer=2M
- [myisamchk]
- set-variable = key_buffer=256M
- set-variable = sort_buffer=256M
- set-variable = read_buffer=2M
- set-variable = write_buffer=2M
- [MySQLhotcopy]
- interactive-timeout
- *********************************************************************
⑤修改Slave數(shù)據(jù)庫的配置文件
以192.168.1.5 Slave數(shù)據(jù)庫的配置文件為例
修改/etc/my.cnf ,在[MySQLd]區(qū)段內(nèi)加入?yún)?shù)
- # vi /etc/my.cnf
- master-host=192.168.1.3
- master-user=testbak
- master-password=**** //自己指定
- master-port=3306
- server-id=2
- master-connect-retry=60 預(yù)設(shè)重試間隔60秒
- replicate-do-db=hello 告訴slave只做ephd數(shù)據(jù)庫的更新
- log-slave-updates
- *******************************************************************************
- # Example MySQL config file for very large systems.
- #
- # This is for large system with memory of 1G-2G where the system runs mainly
- # MySQL.
- #
- # You can copy this file to
- # /etc/my.cnf to set global options,
- # MySQL-data-dir/my.cnf to set server-specific options (in this
- # installation this directory is /usr/local/MySQL/var) or
- # ~/.my.cnf to set user-specific options.
- #
- # One can in this file use all long options that the program supports.
- # If you want to know which options a program support, run the program
- # with --help option.
- # The following options will be passed to all MySQL clients
- [client]
- #password = your_password
- port = 3306
- socket = /tmp/MySQL.sock
- # Here follows entries for some specific programs
- # The MySQL server
- [MySQLd]
- port = 3306
- socket = /tmp/MySQL.sock
- skip-locking
- set-variable = key_buffer=384M
- set-variable = max_allowed_packet=1M
- set-variable = table_cache=512
- set-variable = sort_buffer=2M
- set-variable = record_buffer=2M
- set-variable = thread_cache=8
- # Try number of CPU's*2 for thread_concurrency
- set-variable = thread_concurrency=8
- set-variable = myisam_sort_buffer_size=64M
- master-host=192.168.1.3
- master-user=testbak
- master-password=testbak
- master-port=3306
- server-id=2
- master-connect-retry=60
- replicate-do-db=hello
- log-slave-updates
- # Point the following paths to different dedicated disks
- #tmpdir = /tmp/
- #log-update = /path-to-dedicated-directory/hostname
- # Uncomment the following if you are using BDB tables
- #set-variable = bdb_cache_size=384M
- #set-variable = bdb_max_lock=100000
- # Uncomment the following if you are using InnoDB tables
- #innodb_data_home_dir = /usr/local/MySQL/var/
- #innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
- #innodb_log_group_home_dir = /usr/local/MySQL/var/
- #innodb_log_arch_dir = /usr/local/MySQL/var/
- # You can set .._buffer_pool_size up to 50 - 80 %
- # of RAM but beware of setting memory usage too high
- #set-variable = innodb_buffer_pool_size=384M
- #set-variable = innodb_additional_mem_pool_size=20M
- # Set .._log_file_size to 25 % of buffer pool size
- #set-variable = innodb_log_file_size=100M
- #set-variable = innodb_log_buffer_size=8M
- #innodb_flush_log_at_trx_commit=1
- #set-variable = innodb_lock_wait_timeout=50
- [MySQLdump]
- quick
- set-variable = max_allowed_packet=16M
- [MySQL]
- no-auto-rehash
- # Remove the next comment character if you are not familiar with SQL
- #safe-updates
- [isamchk]
- set-variable = key_buffer=256M
- set-variable = sort_buffer=256M
- set-variable = read_buffer=2M
- set-variable = write_buffer=2M
- [myisamchk]
- set-variable = key_buffer=256M
- set-variable = sort_buffer=256M
- set-variable = read_buffer=2M
- set-variable = write_buffer=2M
- [MySQLhotcopy]
- interactive-timeout
- ********************************************************************
配置完畢,重啟主數(shù)據(jù)庫,由于配置文件中加入了log-bin參數(shù),因此開始有index產(chǎn)生,在/var/lib/MySQL目錄下有.index檔案紀(jì)錄數(shù)據(jù)庫的異常log。
配置Slave數(shù)據(jù)庫
將192.168.1.4 master 的備份MySQL.tar.gz復(fù)制到192.168.1.5 slave上
- #cd /var/lib/
- #tar xzvf MySQL.tar.gz
- #chown –R MySQL:MySQL MySQL
配置一般在MySQL命令行下進(jìn)行。
- #MySQL -h192.168.1.5 –uroot –p
- MySQL>; change master to master_log_file='ephdb05-bin.003',master_log_pos=169;
啟動Slave數(shù)據(jù)庫:
- MySQL>; start slave;
- MySQL>; show slave status;
查看Slave數(shù)據(jù)同步操作的依據(jù)Master_Log_File,Read_Master_Log_Pos是否與當(dāng)前Master的一致。這時在/var/lib/MySQL目錄會出現(xiàn)master.info,此檔案紀(jì)錄了Master MySQL server的信息。
數(shù)據(jù)庫優(yōu)化
采用了blob的數(shù)據(jù)類型,長時間運(yùn)行會造成數(shù)據(jù)碎片,需要進(jìn)行整理。
在處理數(shù)據(jù)優(yōu)化前,需要關(guān)閉MySQL服務(wù)。
/usr/local/MySQL/bin/MySQLadmin shutdown
修復(fù)和整理數(shù)據(jù)庫
/usr/local/MySQL/bin/myisamchk -r /usr/local/MySQL/var/hello/1_tbl
對第一個索引進(jìn)行重索引
/usr/local/MySQL/bin/myisamchk -R 1 /usr/local/MySQL/var/hello/1_tbl
將索引按照倒序排序,加快檢索速度
/usr/local/MySQL/bin/myisamchk -S /usr/local/MySQL/var/hello/1_tbl
對數(shù)據(jù)庫關(guān)聯(lián)優(yōu)化
/usr/local/MySQL/bin/myisamchk -a /usr/local/MySQL/var/hello/1_tbl
(其他數(shù)據(jù)庫、表的操作類似)
四、總結(jié)
Ppache+PHP+MySQL越來越多被應(yīng)用于網(wǎng)站建設(shè),如何才能更安全,更有效的保護(hù)好數(shù)據(jù)是系統(tǒng)管理員的頭等大事,希望能有越來越多的好工具,好方法,好思路來協(xié)助我們做好這個工作,而且我堅(jiān)信,以后的路是越走越寬的,套用老話結(jié)束這個文章------技術(shù)無極限!!
【編輯推薦】