自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解MySQL+Apache集群模式的實(shí)現(xiàn)

數(shù)據(jù)庫 MySQL
本文將為大家介紹MySQL+Apache集群模式的實(shí)現(xiàn),希望對大家用好LAMP組合有所幫助。

環(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安全備份模式圖

apache_mysql_97521120826901.gif

見圖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)地址,這樣可以更安全。

1_20301120826973.gif

 

2_67471120826989.gif
 3_38211120827046.gif

 

我們假定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ù)并安裝

  1. # vncserver 

連入linux系統(tǒng),開一個終端

  1. # cd /home/xuchen  
  2. # tar xzvf RedHat_Cluster_Suite.tgz  
  3. # cd RedHat_Cluster_Suite  
  4. # ./autorun 

這將開啟安裝界面,沒什么好說,直接安裝就是了。

(3)配置前的準(zhǔn)備

修改集群服務(wù)器的IP轉(zhuǎn)發(fā)設(shè)置

  1. # /sbin/sysctl -w net.ipv4.ip_forward=1   

修改當(dāng)前會話的值

  1. # vi /etc/sysctl.conf   
  2. 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/  目錄中。

  1. # vi /etc/sysconfig/ha/conf/httpd.conf  

配置接口頁面站點(diǎn)的相關(guān)參數(shù)。缺省的監(jiān)聽端口是3636,你可以改成其他的端口

Listen 3636

訪問Piranha配置接口頁面需要有帳號和密碼,

帳號默認(rèn)為piranha,用以下命令配置密碼

  1. # /usr/sbin/piranha-passwd   //設(shè)置 piranha 的登錄口令。  
  2. # /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)容很簡單:

  1. #!/bin/sh  
  2. 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配置工具。

  1. # /sbin/service piranha-gui stop  //關(guān)閉配置界面,增強(qiáng)安全性  
  2. # /sbin/service pulse start  //啟動lvs服務(wù),此時進(jìn)程列表里應(yīng)含用 pulse 和 lvs 的進(jìn)程名。  
  3. # ipvsadm  //查看當(dāng)前的路由表  
  4. IP Virtual Server version 1.0.8 (size=65536)  
  5. Prot LocalAddressort Scheduler Flags  
  6.   ->; RemoteAddressort           Forward Weight ActiveConn InActConn  
  7. TCP  211.100.1.1:http wlc persistent 1440 mask 255.255.255.0  
  8.   ->; 192.168.1.2:http           Masq    100    128        24929       
  9.   ->; 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)限。

  1. #MySQL -h192.168.1.4 –uroot –p –D hello  
  2. 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)前的日志文件及日志位置。

  1. #MySQL -h192.168.1.4 –uroot –p –D hello  
  2. 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ù)之前要用

  1. # 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ù)

  1. # vi /etc/my.cnf  
  2. log-bin  
  3. server-id=1 
  4. sql-bin-update-same  
  5. binlog-do-db=hello 
  6. ******************************************************************************  
  7. # Example MySQL config file for very large systems.  
  8. #  
  9. # This is for large system with memory of 1G-2G where the system runs mainly  
  10. # MySQL.  
  11. #  
  12. # You can copy this file to  
  13. # /etc/my.cnf to set global options,  
  14. # MySQL-data-dir/my.cnf to set server-specific options (in this  
  15. # installation this directory is /usr/local/MySQL/var) or  
  16. # ~/.my.cnf to set user-specific options.  
  17. #  
  18. # One can in this file use all long options that the program supports.  
  19. # If you want to know which options a program support, run the program  
  20. # with --help option.  
  21.  
  22. # The following options will be passed to all MySQL clients  
  23. [client]  
  24. #password       = your_password 
  25. port            = 3306 
  26. socket          = /tmp/MySQL.sock  
  27.  
  28. # Here follows entries for some specific programs  
  29.  
  30. # The MySQL server  
  31. [MySQLd]  
  32. port            = 3306 
  33. socket          = /tmp/MySQL.sock  
  34. skip-locking  
  35. set-variable    = key_buffer=384M  
  36. set-variable    = max_allowed_packet=1M  
  37. set-variable    = table_cache=512  
  38. set-variable    = sort_buffer=2M  
  39. set-variable    = record_buffer=2M  
  40. set-variable    = thread_cache=8  
  41. # Try number of CPU's*2 for thread_concurrency  
  42. set-variable    = thread_concurrency=8  
  43. set-variable    = myisam_sort_buffer_size=64M  
  44.  
  45. log-bin  
  46. server-id       = 1 
  47. sql-bin-update-same  
  48. binlog-do-db=hello 
  49.  
  50. # Replication Master Server (default)  
  51. #log-bin             # required for replication  
  52. #server-id      = 1   # required unique id between 1 and 2^32 - 1  
  53.                     # defaults to 1 if master-host is not set  
  54.                     # but will not function as a master if omitted  
  55.  
  56. # Replication Slave Server (comment out master section to use this)  
  57. #master-host     =   # MUST BE SET  
  58. #master-user     =   # MUST BE SET  
  59. #master-password =   # MUST BE SET  
  60. #master-port     =   # optional--defaults to 3306  
  61. #log-bin             # not required for slaves, but recommended  
  62. #server-id       = 2 # required unique id between 2 and 2^32 - 1  
  63.                     # (and different from the master)  
  64.                     # defaults to 2 if master-host is set  
  65.                     # but will not function as a slave if omitted  
  66.  
  67. # Point the following paths to different dedicated disks  
  68. #tmpdir         = /tmp/  
  69. #log-update     = /path-to-dedicated-directory/hostname  
  70.  
  71. # Uncomment the following if you are using BDB tables  
  72. #set-variable   = bdb_cache_size=384M  
  73. #set-variable   = bdb_max_lock=100000  
  74.  
  75. # Uncomment the following if you are using InnoDB tables  
  76. #innodb_data_home_dir = /usr/local/MySQL/var/  
  77. #innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend  
  78. #innodb_log_group_home_dir = /usr/local/MySQL/var/  
  79. #innodb_log_arch_dir = /usr/local/MySQL/var/  
  80. # You can set .._buffer_pool_size up to 50 - 80 %  
  81. # of RAM but beware of setting memory usage too high  
  82. #set-variable = innodb_buffer_pool_size=384M  
  83. #set-variable = innodb_additional_mem_pool_size=20M  
  84. # Set .._log_file_size to 25 % of buffer pool size  
  85. #set-variable = innodb_log_file_size=100M  
  86. #set-variable = innodb_log_buffer_size=8M  
  87. #innodb_flush_log_at_trx_commit=1 
  88. #set-variable = innodb_lock_wait_timeout=50  
  89.  
  90. [MySQLdump]  
  91. quick  
  92. set-variable    = max_allowed_packet=16M  
  93.  
  94. [MySQL]  
  95. no-auto-rehash  
  96. # Remove the next comment character if you are not familiar with SQL  
  97. #safe-updates  
  98.  
  99. [isamchk]  
  100. set-variable    = key_buffer=256M  
  101. set-variable    = sort_buffer=256M  
  102. set-variable    = read_buffer=2M  
  103. set-variable    = write_buffer=2M  
  104.  
  105. [myisamchk]  
  106. set-variable    = key_buffer=256M  
  107. set-variable    = sort_buffer=256M  
  108. set-variable    = read_buffer=2M  
  109. set-variable    = write_buffer=2M  
  110.  
  111. [MySQLhotcopy]  
  112. interactive-timeout  
  113.  
  114. ********************************************************************* 

⑤修改Slave數(shù)據(jù)庫的配置文件

以192.168.1.5 Slave數(shù)據(jù)庫的配置文件為例

修改/etc/my.cnf ,在[MySQLd]區(qū)段內(nèi)加入?yún)?shù)

  1. # vi /etc/my.cnf  
  2. master-host=192.168.1.3  
  3. master-user=testbak 
  4. master-password=**** //自己指定  
  5. master-port=3306 
  6. server-id=2 
  7. master-connect-retry=60 預(yù)設(shè)重試間隔60秒   
  8. replicate-do-db=hello 告訴slave只做ephd數(shù)據(jù)庫的更新   
  9. log-slave-updates  
  10.  
  11. *******************************************************************************  
  12. # Example MySQL config file for very large systems.  
  13. #  
  14. # This is for large system with memory of 1G-2G where the system runs mainly  
  15. # MySQL.  
  16. #  
  17. # You can copy this file to  
  18. # /etc/my.cnf to set global options,  
  19. # MySQL-data-dir/my.cnf to set server-specific options (in this  
  20. # installation this directory is /usr/local/MySQL/var) or  
  21. # ~/.my.cnf to set user-specific options.  
  22. #  
  23. # One can in this file use all long options that the program supports.  
  24. # If you want to know which options a program support, run the program  
  25. # with --help option.  
  26.  
  27. # The following options will be passed to all MySQL clients  
  28. [client]  
  29. #password       = your_password 
  30. port            = 3306 
  31. socket          = /tmp/MySQL.sock  
  32.  
  33. # Here follows entries for some specific programs  
  34.  
  35. # The MySQL server  
  36. [MySQLd]  
  37. port            = 3306 
  38. socket          = /tmp/MySQL.sock  
  39. skip-locking  
  40. set-variable    = key_buffer=384M  
  41. set-variable    = max_allowed_packet=1M  
  42. set-variable    = table_cache=512  
  43. set-variable    = sort_buffer=2M  
  44. set-variable    = record_buffer=2M  
  45. set-variable    = thread_cache=8  
  46. # Try number of CPU's*2 for thread_concurrency  
  47. set-variable    = thread_concurrency=8  
  48. set-variable    = myisam_sort_buffer_size=64M  
  49. master-host=192.168.1.3  
  50. master-user=testbak 
  51. master-password=testbak 
  52. master-port=3306 
  53. server-id=2 
  54. master-connect-retry=60   
  55. replicate-do-db=hello 
  56. log-slave-updates  
  57.  
  58. # Point the following paths to different dedicated disks  
  59. #tmpdir         = /tmp/  
  60. #log-update     = /path-to-dedicated-directory/hostname  
  61.  
  62. # Uncomment the following if you are using BDB tables  
  63. #set-variable   = bdb_cache_size=384M  
  64. #set-variable   = bdb_max_lock=100000  
  65.  
  66. # Uncomment the following if you are using InnoDB tables  
  67. #innodb_data_home_dir = /usr/local/MySQL/var/  
  68. #innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend  
  69. #innodb_log_group_home_dir = /usr/local/MySQL/var/  
  70. #innodb_log_arch_dir = /usr/local/MySQL/var/  
  71. # You can set .._buffer_pool_size up to 50 - 80 %  
  72. # of RAM but beware of setting memory usage too high  
  73. #set-variable = innodb_buffer_pool_size=384M  
  74. #set-variable = innodb_additional_mem_pool_size=20M  
  75. # Set .._log_file_size to 25 % of buffer pool size  
  76. #set-variable = innodb_log_file_size=100M  
  77. #set-variable = innodb_log_buffer_size=8M  
  78. #innodb_flush_log_at_trx_commit=1 
  79. #set-variable = innodb_lock_wait_timeout=50  
  80.  
  81. [MySQLdump]  
  82. quick  
  83. set-variable    = max_allowed_packet=16M  
  84.  
  85. [MySQL]  
  86. no-auto-rehash  
  87. # Remove the next comment character if you are not familiar with SQL  
  88. #safe-updates  
  89.  
  90. [isamchk]  
  91. set-variable    = key_buffer=256M  
  92. set-variable    = sort_buffer=256M  
  93. set-variable    = read_buffer=2M  
  94. set-variable    = write_buffer=2M  
  95.  
  96. [myisamchk]  
  97. set-variable    = key_buffer=256M  
  98. set-variable    = sort_buffer=256M  
  99. set-variable    = read_buffer=2M  
  100. set-variable    = write_buffer=2M  
  101.  
  102. [MySQLhotcopy]  
  103. interactive-timeout  
  104.  
  105. ******************************************************************** 

配置完畢,重啟主數(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上

  1. #cd /var/lib/   
  2. #tar xzvf  MySQL.tar.gz   
  3. #chown –R MySQL:MySQL MySQL 

配置一般在MySQL命令行下進(jìn)行。

  1. #MySQL -h192.168.1.5 –uroot –p   
  2. MySQL>; change master to master_log_file='ephdb05-bin.003',master_log_pos=169

啟動Slave數(shù)據(jù)庫:

  1. MySQL>; start slave;  
  2. 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ù)無極限!!  

【編輯推薦】 

  1. MySQL數(shù)據(jù)庫集群進(jìn)行正確配置步驟
  2. MySQL 集群在Server1與Server2上如何安裝MySQL
  3. MySQL集群配置
  4. MySQL集群自動安裝腳本
  5. MySQL觸發(fā)器如何正確使用

責(zé)任編輯:彭凡 來源: PHPChina
相關(guān)推薦

2011-02-25 11:16:38

ApacheTomcat

2024-05-27 00:04:00

2015-05-06 13:34:14

MySQL集群搭建

2023-04-27 07:52:56

Redis集群模式

2019-09-16 16:05:13

Redis集群模式

2011-08-05 15:28:47

MySQL數(shù)據(jù)庫集群負(fù)載均衡

2010-08-12 13:15:26

MySQL集群

2010-08-12 15:30:10

MySQL集群

2009-07-07 15:01:58

Apache Serv

2010-01-20 09:23:38

jBPM高級交互模式jBPM四眼原則

2009-08-25 18:04:30

C#實(shí)現(xiàn)Singlet

2014-07-11 09:43:34

MySQL集群

2010-06-04 15:59:45

Hadoop完全分布模

2011-06-28 15:18:45

Qt 單例模式

2010-06-04 10:14:14

MySQL匹配模式

2012-05-30 10:09:57

ApacheTomcat

2013-11-29 15:41:08

解析漏洞ApacheApache解析漏洞

2013-01-29 10:20:06

MapReduceHadoop架構(gòu)

2011-07-07 13:21:26

DiscuzApacheMySQL

2017-04-01 14:01:50

Apache Spar內(nèi)存管理
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號