網(wǎng)站制作 網(wǎng)絡(luò)推廣武漢整站seo數(shù)據(jù)上云
環(huán)境
centos6.7 虛擬機(jī)兩臺
主:192.168.23.160
從:192.168.23.163
準(zhǔn)備
在兩臺機(jī)器上分別安裝mysql5.6.23,安裝完成后利用臨時(shí)密碼登錄mysql數(shù)據(jù)修改root的密碼;將my.cnf配置文件放至/etc/my.cnf,重啟mysql服務(wù)進(jìn)程。
my.cnf 初始內(nèi)容:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.[mysqld]lower_case_table_names=1
skip_name_resolve
character-set-server=utf8
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.innodb_buffer_pool_size = 2G
long_query_time=2
slow_query_log=0
log_queries_not_using_index=1
thread_concurrency=8
thread_cache_size=32
max_connections=500
max_connect_errors=800# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
配置主庫
在主庫的my.cnf配置文件的[mysqld]節(jié)點(diǎn)下增加
server_id=160
log_bin=mysql-bin
log_bin_trust_function_creators=1
重啟mysql服務(wù)后登錄mysql數(shù)據(jù)庫,執(zhí)行 how master status; 查看主庫的狀態(tài)
創(chuàng)建同步賬號:grant replication slave on . to ‘sync’@’%’ identified by ‘123456’;
配置從庫
在從庫的my.cnf配置文件的[mysqld]節(jié)點(diǎn)下增加
server_id=163
replicate-ignore-table=dbname.table1,dbname.table2 //需要忽略同步的表
重啟mysql服務(wù)后登錄mysql數(shù)據(jù)庫,執(zhí)行以下命令:
change master to master_host='192.168.23.160', master_port=3306, master_user='sync', master_password='123456', master_log_file='mysql-bin.000004', master_log_pos=425;
master_log_file、master_log_pos為主庫通過“how master status;”命令查詢出來的結(jié)果。
通過show slave status \G;查看從庫的連接狀態(tài)
Slave_IO_Running、Slave_SQL_Running狀態(tài)都為“Yes”表示成功。
注意:服務(wù)器直接要開放3306 端口,或者關(guān)閉防火墻!
初次配置可能會有Slave_IO_Running、Slave_SQL_Running狀態(tài)都為“No”的情況,重啟從庫可以解決(保證主、從庫配置正確、網(wǎng)絡(luò)連接正常)。
日志配置
隨著數(shù)據(jù)增多,日志文件會越來越多,需要定時(shí)清理日志,不然日志文件會占滿磁盤。
在my.cnf 添加:expire_logs_days=[days] – 天數(shù),表示在主數(shù)據(jù)庫日志切換的時(shí)候會刪除指定天數(shù)以前的日志;max_binlog_size 可以設(shè)置單個(gè)二進(jìn)制文件的大小。
日志文件目錄修改(主庫)
1、關(guān)閉mysql服務(wù)
2、將原目錄(/var/lib/mysql)下的mysql-bin.*文件移動(dòng)到新的目錄下(/home/mysql-logs),目錄要有可讀寫權(quán)限;修改mysql-bin.index文件中的文件目錄為絕對路徑,如:/home/mysql-logs/mysql-bin.000001。
3、修改配置文件,log_bin=home/mysql-logs/mysql-bin
4、啟動(dòng)mysql服務(wù)
讀寫分離(mysql-proxy)
windows 下配置
[mysql-proxy]
admin-lua-script=D:/tools/mysql-proxy/lib/mysql-proxy/lua/admin.lua
proxy-backend-addresses=192.168.23.160:3306 -- 主庫
proxy-read-only-backend-addresses=192.168.23.163:3306 -- 從庫
proxy-lua-script=D:/tools/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
log-file=D:/tools/mysql-proxy/log/mysql-proxy.log
log-level=debug
daemon=true
keepalive=true
Linux 下配置
安裝pkg-config
1、編譯mysql-proxy
**注:**0.8.5 不需要編譯,也不需要有安裝pkg-config
用mysql-proxy -V查看mysql-proxy的相關(guān)信息,這里留意下版本信息,將會在第三步配置lua路徑的時(shí)候用到;
2、使用vim /etc/mysql-proxy.cnf命令打開mysqk-proxy的配置文件進(jìn)行編輯:
在mysql-proxy.cnf中輸入如下內(nèi)容:
[mysql-proxy]
admin-username=user #admin用戶名
admin-password=password admin密碼
admin-lua-script=/usr/lib64/mysql-proxy/lua/admin.lua #lua位置,參見上面的版本信息
daemon=true # mysql-proxy以守護(hù)進(jìn)程方式運(yùn)行
keepalive=true #保持連接啟動(dòng)進(jìn)程會有2個(gè), 一號進(jìn)程用來監(jiān)視二號進(jìn)程
proxy-backend-addresses=10.17.6.210 #目標(biāo)地址,Indb內(nèi)網(wǎng)地址,默認(rèn)端口3306,10.17.6.210是目標(biāo)inDB的ip
log-file=/var/log/mysql-proxy.log #日志文件存儲路徑
log-level=debug
編輯完之后通過Esc退出編輯,然后用“:wq”保存mysql-proxy.cnf的編輯。
3、用chmod 0660 /etc/mysql-proxy.cnf命令來改變配置文件的權(quán)限,然后用 mysql-proxy --defaults-file=/etc/mysql-proxy.cnf啟動(dòng)mysql-proxy服務(wù):
chmod 0660 /etc/mysql-proxy.cnf
mysql-proxy --defaults-file=/etc/mysql-proxy.cnf
4、用mysql -h$uhost_ip -P4040 -u$User -p$Password測試:
mysql -h106.75.94.37 -P4040 -uroot -p123456
$uhost_ip:UHost的外網(wǎng)IP $User:用戶名 $Password:數(shù)據(jù)庫密碼
注意:打開防火墻的4040端口,MySQL-Proxy默認(rèn)端口為4040,通過訪問4040端口就可以訪問3306端口。